我需要创建一个程序,从文本文件中获取整数,并输出它们,包括数字、最小数字、最大数字、平均值、总数、N 个数字等。我可以用下面的代码很好地做到这一点,但我还需要处理每行的文本。我的示例文件有 7 个数字,每行用制表符分隔,总共 8 行,但我假设我不知道每行有多少数字,每个文件有多少行等。
另外,对于它的价值,即使我知道如何使用向量和数组,我所在的特定类也没有得到它们,所以我宁愿不使用它们。
谢谢。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int num;
int count = 0;
int total = 0;
int average = 0;
string str = "";
int numLines = 0;
int lowNum = 1000000;
int highNum = -1000000;
ifstream fileIn;
fileIn.open("File2.txt");
if (!fileIn) {
cout << "nError opening file...Closing program.n";
fileIn.close();
}
else {
while (!fileIn.eof()) {
fileIn >> num;
cout << num << " ";
total += num;
count++;
if (num < lowNum) {
lowNum = num;
}
if (num > highNum) {
highNum = num;
}
}
average = total / count;
cout << "nnTotal is " << total << "." << endl;
cout << "Total amount of numbers is " << count << "." << endl;
cout << "Average is " << average << "." << endl;
cout << "Lowest number is " << lowNum << endl;
cout << "Highest number is " << highNum << endl;
fileIn.close();
return 0;
}
}