| 4 | 1/1 | 返回列表 |
| 查看: 686 | 回復: 3 | |||
[交流]
【求助】C++ 作業(yè)題求助 已有3人參與
|
|
我不是計算機專業(yè)的,現(xiàn)在在修計算機系的一門課,F(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; } |
|
本帖內容被屏蔽 |
| 4 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 南昌大學材料專碩311分求調劑 +6 | 77chaselx 2026-03-20 | 6/300 |
|
|---|---|---|---|---|
|
[考研] 求調劑 +3 | Ma_xt 2026-03-17 | 3/150 |
|
|
[考研] 317求調劑 +8 | 申子申申 2026-03-19 | 13/650 |
|
|
[考研] 材料專碩英一數(shù)二306 +7 | z1z2z3879 2026-03-18 | 7/350 |
|
|
[考研] 321求調劑 +9 | 何潤采123 2026-03-18 | 11/550 |
|
|
[考研] 一志愿中海洋材料工程專碩330分求調劑 +8 | 小材化本科 2026-03-18 | 8/400 |
|
|
[考研] 330求調劑 +4 | 小材化本科 2026-03-18 | 4/200 |
|
|
[考研] 一志愿蘇州大學材料求調劑,總分315(英一) +5 | sbdksD 2026-03-19 | 5/250 |
|
|
[考研] 北科281學碩材料求調劑 +5 | tcxiaoxx 2026-03-20 | 5/250 |
|
|
[考研] A區(qū)線材料學調劑 +5 | 周周無極 2026-03-20 | 5/250 |
|
|
[考研] 求調劑 +3 | @taotao 2026-03-20 | 3/150 |
|
|
[考研] 298-一志愿中國農(nóng)業(yè)大學-求調劑 +9 | 手機用戶 2026-03-17 | 9/450 |
|
|
[考研] 材料學碩318求調劑 +5 | February_Feb 2026-03-19 | 5/250 |
|
|
[考研] 材料與化工求調劑 +7 | 為學666 2026-03-16 | 7/350 |
|
|
[考研] 0703化學 305求調劑 +4 | FY_yy 2026-03-14 | 4/200 |
|
|
[考研] 生物學071000 329分求調劑 +3 | 我愛生物生物愛?/a> 2026-03-17 | 3/150 |
|
|
[考研] 考研化學學碩調劑,一志愿985 +4 | 張vvvv 2026-03-15 | 6/300 |
|
|
[考研] 304求調劑 +3 | 曼殊2266 2026-03-14 | 3/150 |
|
|
[考研] 0856求調劑 +3 | 劉夢微 2026-03-15 | 3/150 |
|
|
[考研] 326求調劑 +3 | mlpqaz03 2026-03-15 | 3/150 |
|