好的,对于我的任务,我必须显示平均降雨量和与之相关的月份。这就是输出的样子。
The year's average monthly rainfall was 139 mm.
September has the highest rainfall (190 mm).
January has the lowes rainfall (95 mm)
Month Rainfall(mm) Classification
1 95 Dry
2 100 Dry
3 120 Average
4 130 Average
5 135 Average
6 145 Average
7 155 Average
8 185 Rainy
9 190 Rainy
10 160 Average
11 130 Average
12 120 Average
这就是我的实际情况。
The year's average monthly rainfall was 139mm
The lowest rainfall was (95 mm)
The highest rainfall was (190 mm)
Months Rainfall(mm) Classification
1 95 Dry
2 100 Dry
3 120 Average
4 130 Average
5 135 Average
6 145 Average
7 155 Average
8 185 Rainy
9 190 Rainy
10 160 Average
11 130 Average
12 120 Average
所以是的,我让它像我想要的那样工作除了一部分。
我的输出有这个
降雨量最低(95毫米)
最高降雨量为(190毫米)
我希望它能够显示与预期输出相关的测量数据,但我并不确切知道如何做到这一点。有关如何的任何想法?这是我的代码中与之相关的部分。我还编辑了代码,以便它自己工作。
#include <iostream>
#include <iomanip>
#include<fstream>
#include <limits>
using namespace std;
int main ()
{
int months[12] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int rainFall;
string Classification[12];
ifstream inputFile;
inputFile.open("rainfall.txt");
int n=0;
int sum=0,total=0;
fstream file("rainfall.txt");
while(file >> n)
{
sum += n;
total++;
}
int average = (float) sum/total;
if( (average + 0.5) >= (int(average) + 1) )
{
cout << "The year's average monthly rainfall was " << average << "mm" << endl;
}
else
{
cout << "The year's average monthly rainfall was " << average+1 << "mm" << endl;
}
{
int low = numeric_limits<int>::max();
int high = numeric_limits<int>::min();
ifstream fin("rainfall.txt");
if(!fin)
return 1;
int n;
while(fin >> n)
{
if(n > high)
high = n;
if(n < low)
low = n;
}
cout << "The lowest rainfall was (" << low << " mm)" << '\n';
cout << "The highest rainfall was (" << high << " mm)" << '\n';
}
}
很抱歉,如果我想问的话很难理解。 txt文件的内容只是输出中Rainfall下的数字。
答案 0 :(得分:1)
假设文件是这种格式:
133 231 90 ... 第一个数字是jaunary,最后一个是12月...你只需要添加:
int low = numeric_limits<int>::max();
int high = numeric_limits<int>::min();
ifstream fin("rainfall.txt");
if(!fin)
return 1;
int n;
int monthCounter = 0;
int monthHigh;
int monthLow;
while(fin >> n)
{
monthCounter++;
if(n > high) {
high = n;
monthHigh = monthCounter;
}
if(n < low) {
monthLow = monthCounter;
low = n;
}
}
cout << "The lowest rainfall was (" << low << " mm)" << '\n';
cout << "The highest rainfall was (" << high << " mm)" << '\n';
在代码的末尾,您将拥有月份中的月份和月份,只需添加一个包含月份名称的数组,然后使用monthHigh和monthLow对其进行索引。
答案 1 :(得分:0)
您可以创建一个包含月份名称的字符串数组,并使用月份整数来索引它。你有一个名为“months”的数组,但是没有在代码中的任何地方使用它,看看你需要从文件中读取月份并执行它。
答案 2 :(得分:0)
执行此操作的最佳方法是使用容器,最好是vector
。您应该读入容器,然后使用它而不是一遍又一遍地重新打开文件。你可以像这样啜饮到容器中:
ifstream fin("rainfall.txt");
const vector<int> vec{ istream_iterator<int>(fin), istream_iterator<int>() };
现在,您可以使用accumulate
来计算平均值,minmax_element
来查找最小值和最大值:
cout << "The year's average monthly rainfall was " << accumulate(cbegin(vec), cend(vec), 0.0) / size(vec) << endl;
const auto its = minmax_element(cbegin(vec), cend(vec));
cout << distance(cbegin(vec), its.first) + 1 << " has the highest rainfall (" << *its.first << "mm)\n" << distance(cbegin(vec), its.second) + 1 << " has the lowest rainfall (" << *its.second << "mm)\n";
您甚至可以简化表格:
cout << "Month Rainfall(mm) Classification\n" << left;
for(auto i = 0; i < size(vec); ++i) {
if(vec[i] <= 100) {
cout << setw(9) << i + 1 << setw(15) << vec[i] << "Dry\n";
} else if(vec[i] <= 159) {
cout << setw(9) << i + 1 << setw(15) << vec[i] << "Average\n";
} else {
cout << setw(9) << i + 1 << setw(15) << vec[i] << "Rainy\n";
}
}
我已经在这里写了一个带有一点点格式的实况示例:http://ideone.com/QQZBoM