| 5 | 1/1 | 返回列表 |
| 查看: 688 | 回復: 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; } |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研]
|
酥酥魚.. 2026-03-21 | 3/150 |
|
|---|---|---|---|---|
|
[考研] 化學調劑 +5 | yzysaa 2026-03-21 | 5/250 |
|
|
[考研] 333求調劑 +5 | 87639 2026-03-21 | 7/350 |
|
|
[考研] 一志愿南大,0703化學,分數(shù)336,求調劑 +3 | 收到VS 2026-03-21 | 3/150 |
|
|
[考研] 297求調劑 +3 | 喜歡還是不甘心 2026-03-20 | 3/150 |
|
|
[考研] 工科0856求調劑 +3 | 沐析汀汀 2026-03-21 | 3/150 |
|
|
[考研] 336求調劑 +5 | rmc8866 2026-03-21 | 5/250 |
|
|
[考研] 330求調劑0854 +3 | assdll 2026-03-21 | 3/150 |
|
|
[考研] 310求調劑 +3 | baibai1314 2026-03-16 | 3/150 |
|
|
[考研] 307求調劑 +3 | wyyyqx 2026-03-17 | 3/150 |
|
|
[考研] 一志愿華南師大 070300(化學)304分求調劑 +3 | 0703武芊慧雪304 2026-03-18 | 3/150 |
|
|
[考研] 295求調劑 +4 | 一志愿京區(qū)211 2026-03-18 | 6/300 |
|
|
[考研] 317求調劑 +5 | 申子申申 2026-03-19 | 9/450 |
|
|
[考研] 287求調劑 +7 | 晨昏線與星海 2026-03-19 | 8/400 |
|
|
[考研] A區(qū)線材料學調劑 +5 | 周周無極 2026-03-20 | 5/250 |
|
|
[考研] 353求調劑 +3 | 拉鉤不許變 2026-03-20 | 3/150 |
|
|
[考研] 【同濟軟件】軟件(085405)考研求調劑 +3 | 2026eternal 2026-03-18 | 3/150 |
|
|
[考研] 308求調劑 +4 | 是Lupa啊 2026-03-16 | 4/200 |
|
|
[考研] 一志愿南京大學,080500材料科學與工程,調劑 +4 | Jy? 2026-03-16 | 4/200 |
|
|
[考研] 318求調劑 +3 | Yanyali 2026-03-15 | 3/150 |
|