我有这段代码来平均文件中的分数。 这是代码:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("Quizzes.dat");
ofstream fout("readout2.dat");
int main(void)
{
fout.setf(ios::showpoint);
fout.setf(ios::fixed);
fout.precision(2);
int id, newid, count = 0;
float score, maxscore, minscore, scoretotal = 0, testavg;
fin >> id >> score;
while (!fin.fail()) {
maxscore = 0;
minscore = 100;
scoretotal = 0;
count = 0;
testavg = 0;
newid = id;
while (id == newid && !fin.fail()) {
if (score <= minscore && score >= maxscore) {
minscore = score;
maxscore = score;
}//end of if(score < minscore && score > maxscore)
else if (minscore == maxscore && score == minscore) {
scoretotal = scoretotal + score;
count++;
} else if (score <= minscore && minscore == maxscore) {
minscore = score;
}//end of else if(score < minscore && minscore == maxscore)
else if (score >= maxscore && minscore == maxscore) {
maxscore = score;
}//End of else if(score > maxscore && minscore == maxscore)
else if (score <= minscore) {
scoretotal = minscore + scoretotal;
count++;
minscore = score;
}//end of else if(score < minscore)
else if (score >= maxscore) {
scoretotal = maxscore + scoretotal;
count++;
maxscore = score;
}//End of else if (score > maxscore)
else {
scoretotal = scoretotal + score;
count++;
}
fin >> id >> score;
}//end of while(id==newid)
testavg = (scoretotal / count);
fout << newid << setw(20) << testavg << endl;
}//end of while(!fin.fail())
}
这是文件:
10234 67
10234 100
10234 53
10234 91
11245 89
11245 46
11245 99
14652 100
14652 56
14652 99
14652 100
14652 96
14652 78
19832 92
19832 78
19832 51
19832 76
19832 89
20014 100
20014 100
20014 100
20014 100
21140 43
21140 56
21140 90
21140 78
21140 63
21140 67
21140 89
22256 10
22256 7
22256 7
22256 2
22256 11
22256 2
22256 2
22256 13
22256 9
27654 83
27654 83
27654 83
27654 83
30021 78
30021 67
30021 92
30021 59
30021 82
30021 58
我遇到的问题是在文件的各个部分中,得分保持不变,并且不会添加任何内容。
答案 0 :(得分:1)
尽量简化事情,你的程序很难阅读
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
int main()
{
std::map<int, std::vector<int>> map;
for (int id, score; std::cin >> id >> score;)
map[id].push_back(score);
for (auto const& id : map) {
int total = std::accumulate(id.second.begin(), id.second.end(), 0);
std::cout << id.first << ": "
<< total / static_cast<double>(id.second.size()) << '\n';
}
}
如果您需要查找最低和最高分数,还可以使用min_element
和max_element
等算法。
答案 1 :(得分:0)
我添加了一些诊断信息,因此您可以跟踪逻辑流程并找到问题所在。在我看来,你的7阶段if {} else {}结构过于复杂且逻辑上不正确。
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("input.txt");
ostream& fout = cout;
int main (void)
{
fout.setf(ios::showpoint);
fout.setf(ios::fixed);
fout.precision (2);
int id, newid, count=0;
float score, maxscore, minscore, scoretotal=0, testavg;
fin >> id >> score;
while(!fin.fail())
{
maxscore = 0;
minscore = 100;
scoretotal = 0;
count=0;
testavg=0;
newid=id;
while(id==newid && !fin.fail())
{
if(score <= minscore && score >= maxscore)
{
cout << "1" << endl;
minscore = score;
maxscore = score;
}//end of if(score < minscore && score > maxscore)
else if (minscore == maxscore && score == minscore)
{
cout << "2" << endl;
scoretotal = scoretotal + score;
count++;
}
else if(score <= minscore && minscore == maxscore)
{
cout << "3" << endl;
minscore = score;
}//end of else if(score < minscore && minscore == maxscore)
else if(score >= maxscore && minscore == maxscore)
{
cout << "4" << endl;
maxscore = score;
}//End of else if(score > maxscore && minscore == maxscore)
else if(score <= minscore)
{
cout << "5" << endl;
scoretotal = minscore + scoretotal;
count++;
minscore = score;
}//end of else if(score < minscore)
else if (score >= maxscore)
{
cout << "6" << endl;
scoretotal = maxscore + scoretotal;
count++;
maxscore=score;
}//End of else if (score > maxscore)
else
{
cout << "7" << endl;
scoretotal = scoretotal + score;
count++;
}
fin >> id >> score;
}//end of while(id==newid)
testavg = (scoretotal/count);
fout << newid << setw(20) << testavg << endl;
}//end of while(!fin.fail())
}//End of int main(void)