我怎样才能把一个月的产出增加到某一部分
How can I add a month to a certain part of my output?
对于我的作业,我必须显示平均降雨量和与之相关的月份。这就是输出的样子。
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
相关文章:
- 数组索引的值没有增加
- 为什么我的代码在输出中增加了93天
- 有效地使用std::unordered_map来插入或增加键的值
- C++:如何使函数只返回作为列表一部分的字符串
- std::sort()函数无法对向量的一部分进行排序
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 为什么要增加导致崩溃的指针
- 将类型声明为类型模板参数的模板参数的一部分是否合法?
- 增加传递地址的值
- 反转整数的一部分(一半)的函数
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- C 指针和对象作为不同类的一部分
- 为什么"i"在循环的每次迭代中都没有增加?(C++)
- 如何分析代码的哪一部分创建了线程?
- std::thread 增加 DLL 引用计数,从而防止卸载 DLL
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 如何使用接口指针调用方法,该指针是其具体类的一部分,而不是接口的一部分
- 我怎样才能把一个月的产出增加到某一部分
- 代码的哪一部分增加了内存使用