• 3

C++求救 這個問題已經卡了我三天的時間了...

先給背景一下, 我是在職人士, 剛學習C++大概3個月
我正在做一個assignment, 而有一些問題讓我這幾天花了很多時間也做不出來


內容是造樣

我要寫一個程式來計算學生的成績, 檔案來源是這樣子的

檔案名 lab6.in1
mary_smith 10 9 10 3 100 8 7.5 10 73 9 10 5 9 87 -1
george_washington 8 3 7 10 92 6.5 10 6 83 9 11 8 8 77 -1
KEY 10 10 10 10 100 10 10 10 100 10 10 10 10 100 -1


檔案名 lab6.in2
jose_quervo 10 9 10 19 9 99 20 10 19 20 10 96 8 20 10 19 100 -1
larry_ellison 8 10 10 18 10 98 9 10 19 17 9 99 10 17 10 19 88 -1
jack_daniels 10 3 3 6 0 93 10 1 20 19 7 84 9 12 3 19 87 -1
KEY 10 10 10 20 10 100 20 10 20 20 10 100 10 20 10 20 100 -1

加起每一個學生的得分 (不懂處理超過一個學生, 特別是用array(?))

每當去到 -1的時候, 就跳去下一個學生 (這個我不懂)

每當去到學生名 KEY的時候, 後邊的分數加起來就是總分 (這個我也不懂)

最後就是分等級 A,B,C,D,F (這個我懂)

最後就是輸出成這樣子

max_headroom: 8.00 6.00 9.00 19.00 8.00 99.00 19.00 7.00 13.00 15.00 9.00 91.00 10.00 6.00 6.00 9.00 93.00 427.00/500.00 85 = B
bill_gates: 10.00 8.00 1.00 8.00 8.00 84.00 10.00 8.00 2.00 9.00 5.00 84.00 10.00 15.00 10.00 12.00 86.00 370.00/500.00 74 = C

最主要的問題就是
1 我不懂前面的ARRAY(?)
2 如何處理由filein之後的學生資料 (如何一個個學生分開來處理, 現在的我只會處理一個學生,多於一個我就gg了)

各位高手可以給小弟一些指導嗎, 感恩感恩!!

2014-04-14 10:27 發佈
文章關鍵字 C++ 問題
迴圈(loop)
矩陣(array)

先學會這兩個再來吧
你還差太遠
不要急著越級打怪
lyre1710 wrote:
迴圈(loop)矩陣...(恕刪)

就是老師給的...我也想不到一下子就難成這樣.
前幾份功課也只是用array跟loop做一些簡單的cin cout
我的頭也痛了好幾天, 難怪大學時電腦系的朋友都是沒有一天有得睡
hugo0821 wrote:
mary_smith 10 9 10 3 100 8 7.5 10 73 9 10 5 9 87 -1
george_washington 8 3 7 10 92 6.5 10 6 83 9 11 8 8 77 -1
KEY 10 10 10 10 100 10 10 10 100 10 10 10 10 100 -1

看你的資料格式在行尾是有放斷行符號的
那每讀一行就是一個學生的資料了,-1 根本是多餘的
巨乳は夢が詰まってるから大きい、貧乳はみんなに夢を与えているから小さいんだ!
kaworucloud wrote:
看你的資料格式在行尾...(恕刪)

我也沒辨法, 老師就預設這樣給我們, 叫我們用-1來設定跳進下一個學生
(可能比較簡單吧....不解)

PS Two Dimensional Arrays好像有幫助? 正在努力查書中...

hugo0821 wrote:
最主要的問題就是
1 我不懂前面的ARRAY(?)
2 如何處理由filein之後的學生資料 (如何一個個學生分開來處理, 現在的我只會處理一個學生,多於一個我就gg了)


先宣告一個二維陣列 例 array[I][J]

I=學生數-1
j=資料個數-1

第一列的資料-> array[0][0],array[0][1],array[0][2],array[0][3]....
第二列的資料-> array[1][0],array[1][1],array[1][2],array[1][3]....
每讀到 -1 代表資料到底了~要換下一筆資料,這時 I +1,J從0開始繼續讀資料!

大致上的思考方向~讓您參考一下~!



二維陣列應該是我暫時需要看的, 先謝各位,
先給小弟一些時間寫點code, 等一下回來報告!
hugo0821 wrote:
就是老師給的...我...(恕刪)


原來已經有教了啊
那作這個差不多
只是你好像還沒有辨法應用而已

1.
加起每一個學生的得分 (不懂處理超過一個學生, 特別是用array(?))

看你要用一個多維array或開很多個一維array
反正就是把從檔案裡讀到的數字全存起來就好
然後就用迴圈把同一個學生的數字都加起來呀

2.
每當去到 -1的時候, 就跳去下一個學生 (這個我不懂)

只是用來判斷二個學生的資料的分斷點而已
加分迴圈跳到分數是-1時不用加可以跳出了

3.
每當去到學生名 KEY的時候, 後邊的分數加起來就是總分 (這個我也不懂)

key那一行的數字
就是每一次評分的最大分數的意思
用來給你算滿分再來給學生分級用的


一開始你要先搞定檔案io的部份
要做個簡單的parser切出人名和數字
我記得剛開始的人這裡會搞一段時間 XD

如果作業沒要求要讀檔案
或想先跳過這部份
就把資料先全內建在程式碼裡面吧 XD
明天繼續, 暫時就寫成這樣...應該有很多錯處....
還有一天半的時間!! 加油!!!

#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
void inputfile ()
void getdata ()
void studentgrade()
void showgrade ()


int main()

{

const int size = 1000;
const int column = 1000;
string name[size];
float point;
double gad[1000];
float total = 0;
int score;
int i;
double maxpoint;
double totalpoint;
double percentage;
const char grade;
double studentpoints[size][column];
string filename;
string key = "KEY";
int row;
int col;


void inputfile (string filename)
void getdata (ifstream & infile, string name[], double studentpoints[], const int size, const int column)
infile.close();
void studentgrade(double studentpoints[], double totalpoint[], const char grade, const int size, const int column)
void showgrade (const string name, double studentpoints[], double maxpoint, const char grade)
return 0;

}


void inputfile (string filename;)
{
cout << "Please enter input file name:";
cin >> filename;
ifstream infile;
infile.open (filename);
}


void getdata (ifstream & infile, string name[], double studentpoints[], const int size, const int column, double maxpoint, int row)
{
do

for (row = 0; row < size; row++)
{
cin>> name[row];

for (col = 0; col <column; col++)

cin>> studentpoints[row][col];

[studentpoints++] = totalpoint[row];
}
while (studentpoints[row][col] != -1)

If (name[row] = "key" and studentpoints[row][col] == -1);

for (col = 0; col <column; col++)

cin>> maxpoint;
[studentpoints++] = maxpoint;
}


void studentgrade(double maxpoint, double studentpoints[], double totalpoint[], char grade, const int size, const int column, int row, double percentage)
{

for (row = 0; row < size; row++)

percentage = totalpoint[row] / maxpoint;


if (percentage >= 0.9)
grade[row] = 'A';

else if (percentage >= 0.8)
grade[row] = 'B';

else if (percentage >= 0.7)
grade[row] = 'C';

else if (percentage < 0.7)
grade[row] = 'F';
}


void showgrade (const string name, double studentpoints[], double maxpoint, const char grade, double totalpoint, const int size, int i)

{
for (i=0; i<size; i++)

cout << name[i] << studentpoints[i] << totalpoint << "/" << maxpoint << grade <<endl;

}
老實說這樣瞄上去,我覺得錯的地方是真的不少...
另外我建議貼程式碼可以利用codepad之類的網站,其他幫忙的人也會比較方便。

又,那個KEY的意思是對印到每次小考的總分,我覺得應該是這樣。



關於讀取檔案的部分:http://www.programmer-club.com.tw/ShowSameTitleN/c/14291.html
記得,在函數裏面的變數出去後就活不成了,你的fstream記得丟出去。

切割的部分:http://ericsilverblade.blogspot.tw/2013/03/csplit.html
老實說能用這種方法我也第一次看到XD

接著那堆vector你就可以自己查查用法。以後會碰上,實際上用起來也不難的。


不過我總覺得這用C++新增加的東西來搞這些是麻煩了些@@
  • 3
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?