十七週任務1

* 程序的版權和版本聲明部分                   
* Copyright (c) 2011, 煙臺大學計算機學院學生                    
* All rights reserved.                   
* 文件名稱:                                                 
* 作    者:   王明星                                  
* 完成日期:   2012   年  5 月 22日                   
* 版 本 號:                             
* 對任務及求解方法的描述部分                   
* 輸入描述:                   
* 問題描述:                 
* 程序頭部的註釋結束                   
*/     
#include<iostream>   
#include <fstream>   
#include<string>
using namespace std;  
class Student  
{
public:  
   Student(void){}  
   Student(string nam, double c, double m, double e, double all, double avs):name(nam),cpp(c), math(m), english(e), total(all), averagescore(avs){total=c+m+e;}
   void show();  
  
   friend void cin_score(Student stud[]);  //此處使用友元函數的作用是爲了後面對私有數據成員的操作簡便,不必多次調用get和set函數了
  
   friend void out_score(Student stud[]);  
  
private:  
    string name;  
    double cpp;  
    double math;  
    double english;  
    double total;  //總分
    double averagescore;  //平均分
};  
  
void Student::show()  
{  
    cout << name << '\t' << cpp << '\t' << math << '\t' << english << '\t' <<total<< '\t' << averagescore << endl;   
}  
  
void cin_score(Student stud[])  //進行讀入操作
{  
    ifstream infile("score.dat",ios::in);    
  
    if(!infile)  
    {
		cerr<<"open score.dat error!"<<endl;  
        abort( );  
    }  
  
    for(int i = 0; i < 101; i++)  
    {      
        if(i == 100)  
        {  
            stud[i].name= "臧鵬";  
            stud[i].cpp = 100;  
            stud[i].math = 100;  
            stud[i].english = 100;  
        }  
  
        else  //並對其求總分和平均值
        {  
            infile >> stud[i].name >> stud[i].cpp >> stud[i].math >> stud[i].english;  
        }  
  
        stud[i].total = stud[i].cpp + stud[i].math + stud[i].english;  
        stud[i].averagescore = stud[i].total / 3;  
    }  
    infile.close( );  
}  
  
void out_score(Student stud[])  //讀出操作
{  
    ofstream outfile("binary_score.dat",ios::binary);  
  
    if(!outfile)  
    { 
		cerr<<"open binary_score.dat error!"<<endl;  
        abort( );  
    }  
  
    for(int i = 0; i < 101; ++ i)  
    {  
        outfile.write((char *) &stud[i], sizeof(stud[i]));  
        stud[i].show();  
    }  
  
    outfile.close( );  
}  
  
  
int main( )  
{     
    Student stud[101];  
      
    cin_score(stud);  
  
    out_score(stud);  
  
    system("pause");  
  
    return 0;  
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章