| 5 | 1/1 | 返回列表 |
| 查看: 687 | 回復: 3 | |||
| 當前只顯示滿足指定條件的回帖,點擊這里查看本話題的所有回帖 | |||
[交流]
【求助】C++ 作業(yè)題求助 已有3人參與
|
|||
|
我不是計算機專業(yè)的,現(xiàn)在在修計算機系的一門課。現(xiàn)在要寫一段程序,要求是打開并讀取一個文件,文件有四行五列(有點像excel表格),第一列是人名,后四列是每個人的四次考試成績,F(xiàn)在要求我們在輸出文件中另加入兩列,一列是每個人的總成績,另一列式把這個數(shù)字成績換成字母(A,B,C,D). 我現(xiàn)在的問題是我不知道該怎么對原問價進行修改,獲取四次成績,計算綜合,再在輸出文件中加入這兩列。 輸入文件: 王 25 25 23 25 李 15 8 6 0 張 24 21 19 23 趙 17 16 21 22 輸出文件: 王 25.0 25.0 23.5 25.0 98.5 A 李 15.0 8.5 6.5 0.0 30.0 F 張 24.5 21.0 19.5 23.5 88.5 B+ 趙 17.9 16.6 21.3 22.0 77.8 B- 希望能得到大俠的幫助,謝謝!! |
|
下面的code編譯好像沒問題了,可運行的時候說“segmentation fault"。能不能麻煩幫我看看哪出問題了。 #include #include #include #include #include #include #include #include #include #include using namespace std; int main(){ ifstream fin; ofstream fout; string filename; string temp; cout << "Enter input file: "; cin >> filename; fin.open(filename.c_str()); if(!fin){ cerr << "Input file can not be opened!" << endl; exit (-1); } fout.open ("output_file.txt" ;fout << "Name Score1 Score2 Score3 Score4 Total Grade"<< endl; int lineCount = 0; while (!fin.eof()){ getline(fin,temp); ++lineCount; string buf; stringstream ss(temp); vector while (ss >> buf) tokens.push_back(buf); double score[4]; double total; string grade; int Total; for (int i=0; i<4; i++){ score = atof (tokens[i+1].c_str()); total += score; } Total = int (total); if(Total>=95 && Total<=100){ grade = "A"; } else if(Total>=90 && Total<95){ grade = "A-"; } else if(Total>=85 && Total<90){ grade = "B+"; } else if(Total>=80 && Total<85){ grade = "B"; } else if(Total>=75 && Total<80){ grade = "B-"; } else if(Total>=70 && Total<75){ grade = "C+"; } else if(Total>=65 && Total<70){ grade = "C"; } else if(Total>=60 && Total<65){ grade = "C-"; } else if(Total>=50 && Total<60){ grade = "D"; } else if(Total>=0 && Total<50){ grade = "F"; } fout << tokens[0] << "\t" << score[0] << "\t" << score[1] << "\t" << score[2] << "\t" << score[3] << "\t" << total << "\t" << grade << endl; } return 0; } |
銅蟲 (初入文壇)

|
本帖內容被屏蔽 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 化學工程321分求調劑 +16 | 大米飯! 2026-03-15 | 20/1000 |
|
|---|---|---|---|---|
|
[考研] 336求調劑 +5 | rmc8866 2026-03-21 | 5/250 |
|
|
[考研] 265求調劑 +12 | 梁梁校校 2026-03-19 | 14/700 |
|
|
[考研] 材料學學碩080502 337求調劑-一志愿華中科技大學 +4 | 順順順mr 2026-03-18 | 5/250 |
|
|
[考研] 299求調劑 +6 | △小透明* 2026-03-17 | 6/300 |
|
|
[考研] 294求調劑材料與化工專碩 +15 | 陌の森林 2026-03-18 | 15/750 |
|
|
[考研] 323求調劑 +3 | 洼小桶 2026-03-18 | 3/150 |
|
|
[考研] 329求調劑 +9 | 想上學吖吖 2026-03-19 | 9/450 |
|
|
[考研] 一志愿西南交通 專碩 材料355 本科雙非 求調劑 +5 | 西南交通專材355 2026-03-19 | 5/250 |
|
|
[考研] 一志愿北京化工大學0703化學318分,有科研經歷,求調劑 +4 | 一瓶苯甲酸 2026-03-14 | 4/200 |
|
|
[考研] 工科材料085601 279求調劑 +7 | 困于星晨 2026-03-17 | 9/450 |
|
|
[考研] 288求調劑,一志愿華南理工大學071005 +5 | ioodiiij 2026-03-17 | 5/250 |
|
|
[考研] 312求調劑 +8 | 陌宸希 2026-03-16 | 9/450 |
|
|
[考研] 0703化學調劑 +3 | 妮妮ninicgb 2026-03-17 | 3/150 |
|
|
[考研] 材料,紡織,生物(0856、0710),化學招生啦 +3 | Eember. 2026-03-17 | 9/450 |
|
|
[考博] 26博士申請 +3 | 1042136743 2026-03-17 | 3/150 |
|
|
[考博] 26申博 +4 | 八6八68 2026-03-16 | 4/200 |
|
|
[考研] 一志愿211 0703方向310分求調劑 +3 | 努力奮斗112 2026-03-15 | 3/150 |
|
|
[考研] 070300化學學碩求調劑 +6 | 太想進步了0608 2026-03-16 | 6/300 |
|
|
[考研] 中科院材料273求調劑 +4 | yzydy 2026-03-15 | 4/200 |
|