輸入學生姓名成績

統計並輸出學生得A有幾人得B有幾人得C有幾人
得D有幾人不及格有幾人

還有第一名跟最後一名同學的名字並輸出她們的成績

輸出格式:
A有: 人
B有: 人
C有: 人
D有: 人
第一名: 同學
成績:

最後一名: 同學
成績:

#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

int main(int argc, char *argv[])
{
    int A=0,B=0,C=0,D=0,E=0;
    char level;
    char name[20],maxname[20],minname[20];
    double max=0,min=100;
    double grade;
    int i;   
    for(i=0;i<5;i++){
    scanf("%s",&name);
    cin >> grade;
   
    if(grade>=90){
    level= 'A';
    A++;
    }
    else if(grade>=80){
         level='B';
         B++;
         }
    else if(grade>=70){
         level='C';
         C++;
         }
    else if(grade>=60)
    {
         level = 'D';
         D++;
         }
    else {
         level='E';
         E++;
         }
   
    if(grade>max){
    max = grade;
    strcpy(maxname,name);
    }
    if(grade<min) {
    min = grade;
    strcpy(minname,name);
    }
    cout<< name << "的成績是" << grade <<"分, 得到了 "<< level <<endl;
    }
   
    cout << " A有 " << A << "人" << endl;
    cout << " B有 " << B << "人" << endl;
    cout << " C有 " << C << "人" << endl;
    cout << " D有 " << D << "人" << endl;
    cout << "第一名 :" << maxname <<"同學" << endl;
    cout << "成績:" << max << endl;
    cout << "最後一名 :" << minname <<"同學" << endl;
    cout << "成績:" << min << endl;
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


---------------------------------------------------------------------------------------------------------------------------
解釋 :
我忘記在 c++要怎麼輸入字串 , 所以用c 的scanf ,
for loop 控制 input 只有 5 筆
if 判斷成績的等第 , level 是拿來紀當下的等弟 , 因為我看錯題目才加的
跟這題無關
下面印出 誰得到多少分是什麼等第也是多做的
在上面的 if 裡有 5 個變數 , A,B,C,D,E分別紀錄不同分數的等第情況
max 和 min 的意義
max 會先給他零分 , 如果輸入的分數比零分大 就是目前的最大值
到最後 max就會是全部裡面最大的那個分數
在記分數的同時也會把人名存到 maxname這個字串裡
strcpy 是string copy的意思
所以要記得去 include string.h檔
同理也可以求得 最低分與最低分的同學姓名

然後再印出來即可

創作者介紹
創作者 itest6的部落格 的頭像
itest6

itest6的部落格

itest6 發表在 痞客邦 留言(10) 人氣( 3 )