编辑这里是文件上的内容
76 89 150 135 200 76 12 100 150 28 178 189 167 200 175 150 87 99 129 149 176 200 87 35 157 189
我想要一些非常简单的帮助。下面是一个程序,它读取包含多个等级的文本文件。程序获取这些数字,将它们存储在一个数组中,然后确定它们属于哪个类别,因此我编写了setCategories函数。设置类别后,类别中每个数字的编号将存储在一个数组中,该数组将打印到另一个文本文件中。或者至少它应该 - 现在我没有定义getData和printData函数,这两个我已经定位做所有腿部工作/我需要一些帮助编写这些函数,由于某种原因我可以似乎把我的头包裹起来。
#include <iostream>
#include <fstream>
using namespace std;
void getData(istream & in, int A[], int & count)
{
while(!in.eof())
{
in >> A[count++];
}
}
void printData(ostream & out, int ctd[], int count)
{
for (int i=0; i < count; i++)
{
out << ctd[i] << endl;
}
}
void setCategories(int c[], int g[], int size) {
int i;
for (i = 0; i < size; i++) {
if (g[i] > 174)
c[7]++;
else if (g[i] > 149)
c[6]++;
else if (g[i] > 124)
c[5]++;
else if (g[i] > 99)
c[4]++;
else if (g[i] > 74)
c[3]++;
else if (g[i] > 49)
c[2]++;
else if (g[i] > 24)
c[1]++;
else c[0]++;
}
}
int main() {
ofstream outFile;
ifstream inFile;
int categories[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int grades[40];
int count = 0;
inFile.open("/Users/holdentatlow/Desktop/computer_science_projects/test_scores.txt");
outFile.open("/Users/holdentatlow/Desktop/computer_science_projects/results.txt");
outFile << "Matthew Holden Tatlow" << endl;
getData(inFile, grades, count);
setCategories(categories, grades, count);
printData(outFile, categories, count);
return 0;
}
现在,我的输出文件纯粹是废话:
Matthew Holden Tatlow
1
2
0
6
1
3
5
8
262169208
1
0
0
262169416
1
-1196213792
32767
-520085504
32712
-520085410
32712
-520085410
32712
0
0
0
0
答案 0 :(得分:0)
这是两个功能的快速和肮脏的镜头:
void getData(istream & in, int A[], int & count)
{
while(!in.eof())
{
in >> A[count++];
}
}
void printData(ostream & out, int ctd[], int count)
{
for (int i=0; i < count; i++)
{
out << ctd[i] << endl;
}
}
请注意,我已将printData的签名更改为包含计数。我也只是想猜你想要什么,所以.. ymmv。
答案 1 :(得分:-1)
这对我有用:
#include <iostream>
#include <fstream>
using namespace std;
void getData(istream & in, int A[], int & count)
{
while(!in.eof())
{
in >> A[count++];
}
}
void printData(ostream & out, int ctd[], int count)
{
for (int i=0; i < 8; i++)
{
out << ctd[i] << endl;
}
}
void setCategories(int c[], int g[], int size) {
int i;
for (i = 0; i < size; i++) {
if (g[i] > 174)
c[7]++;
else if (g[i] > 149)
c[6]++;
else if (g[i] > 124)
c[5]++;
else if (g[i] > 99)
c[4]++;
else if (g[i] > 74)
c[3]++;
else if (g[i] > 49)
c[2]++;
else if (g[i] > 24)
c[1]++;
else c[0]++;
}
}
int main() {
ofstream outFile;
ifstream inFile;
int categories[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int grades[26];
int count = 0;
inFile.open("/Users/holdentatlow/Desktop/computer_science_projects/test_scores.txt");
outFile.open("/Users/holdentatlow/Desktop/computer_science_projects/results.txt");
outFile << "Matthew Holden Tatlow" << endl;
getData(inFile, grades, count);
setCategories(categories, grades, count);
printData(outFile, categories, count);
return 0;
}