Saturday, July 23, 2011

輸入學生成績並計算平均成績

// AverageScore.cpp : 定義主控台應用程式的進入點。
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

using namespace std;

struct score
{
       char name[30];   //名字
       float chi;   //國文
       float eng;   //英文
       float math;  //數學
       float total; //總分
       float avg;   //平均
};

int main(int argc, char *argv[])
{
    score stu[100];
    int n;


    cout << "請輸入學生人數\n";
    cin >> n;


  
    //輸入n位學生的資料
    for (int i=0;i<n;i++)
    {
        cout << "請輸入第 "<< i+1 << "位學生的名字: ";
        cin >> stu[i].name;

        cout << "請輸入第 "<< i+1 << "位學生的國文成績: ";
        cin >> stu[i].chi;

        cout << "請輸入第 "<< i+1 << "位學生的數學成績: ";
        cin >> stu[i].math;
        cout << "請輸入第 "<< i+1 << "位學生的英文成績: ";
        cin >> stu[i].eng;
       
        stu[i].total = stu[i].chi + stu[i].math + stu[i].eng;
        stu[i].avg = stu[i].total / 3;
    }

    for (int i=0;i<n;i++)
    {
        cout <<  stu[i].name  << "的總成績為: " << stu[i].total << endl;
        cout <<  stu[i].name << "的平均成績為: " << stu[i].avg << endl;
    }


    system("pause");
    return 0;

}

No comments:

Post a Comment