我怎样才能把一个月的产出增加到某一部分

How can I add a month to a certain part of my output?

本文关键字:增加 一部分 一个      更新时间:2023-10-16

对于我的作业,我必须显示平均降雨量和与之相关的月份。这就是输出的样子。

  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 mm)

最高降水量为(190 mm)

我希望它显示与预期输出相关联的月份,但我不知道具体如何做到这一点。有什么想法吗?这是与它相关的部分代码。我还编辑了代码,使其可以自己工作。

#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文件的内容就是输出中rain项下的数字。

假设文件是以下格式:

13323190…第一个数字是一月,最后一个是十二月……你只需要加上这个:

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中添加月份名称的数组,然后使用monthHigh和monthLow对其进行索引。

您可以创建一个包含月份名称的字符串数组,并使用月份整数来索引它。您有一个名为"months"的数组,但在代码的任何地方都没有使用它,请查看需要从文件中读取月份的位置并执行该操作。

最好的方法是使用容器,最好是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)   Classificationn" << left;
for(auto i = 0; i < size(vec); ++i) {
    if(vec[i] <= 100) {
        cout << setw(9) << i + 1 << setw(15) << vec[i] << "Dryn";
    } else if(vec[i] <= 159) {
        cout << setw(9) << i + 1 << setw(15) << vec[i] << "Averagen";
    } else {
        cout << setw(9) << i + 1 << setw(15) << vec[i] << "Rainyn";
    }
}

我写了一个生动的例子,这里有一点花哨的格式:http://ideone.com/QQZBoM