c++ - 如何在输出的特定部分添加一个月?

标签 c++ arrays

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

  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下的数字。

最佳答案

假设文件是​​这样的格式:

133 231 90后 ... 第一个数字是 jaunary,最后一个是 december...你只需要添加这个:

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 对其进行索引。

关于c++ - 如何在输出的特定部分添加一个月?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40126605/

相关文章:

c++ - 子类的模板实例化是多态的,具有接口(interface)的特化

c++ - C++ 中的方法覆盖

arrays - SKSpritenode数组展示

C++:C 字符串、指针和一个非常有趣的 while 循环

c++ - 如何在其值可用于常量表达式的类中初始化数组?

c++ - 为什么这会重复打印换行符?

c++ - 如何为类成员重载运算符<<?

c++ - Visual Studio 关于多个类的荒谬错误

php - Paypal api 将产品插入数组

android - 解析数据时出错 org.json.JSONException : Unterminated array at character