C++第十七週【任務一】 學生成績處理

 /*
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生 
* All rights reserved.
* 文件名稱:C++第十七週【任務一】                              
* 作    者:   李洪懸                              
* 完成日期:   2012  年  6 月 12 日
* 版本號:T  1.0
* 對任務及求解方法的描述部分
* 輸入描述:
* 問題描述:程序解析
* 程序輸出:
*/

【任務1】學生成績處理:保存爲二進制文件

ASCII 文件score.dat 中保存的是100 名學生的姓名和C++課、高
數和英語成績。
(1)定義學生類,其中包含姓名、C++課、高數和英語成績及總
分、均分數據成員,成員函數根據需要確定。
(2)讀入學生的成績,並求出總分,用對象數組進行存儲。
(3)將所有數據保存到一個二進制文件binary_score.dat 中,最
後在文件中寫入你自己的各科成績(咱不謙虛,也求個好運,全100
分)。
(4)爲驗證輸出文件正確,再將binary_score.dat 中的記錄逐一

讀出到學生對象中並輸出查 

#include <fstream>  
#include <iostream>  
#include "iomanip"  
#include "string"  
  
using namespace std;  
  
class Student  
{  
public:  
    Student(){}  
    Student(string na, double cpp, double math, double en):name(na),cpp_score(cpp), math_score(math), en_score(en)  
    {total_score = cpp + math + en;aver_score = total_score / 3;}  
    void set_name(string na);  
    string get_name();  
    void setcpp_score(double sc);  
    double getcpp_score();  
    void setmath_score(double sc);  
    double getmath_score();  
    void seten_score(double sc);  
    double geten_score();  
    void settotal_score(double sc);  
    double gettotal_score();  
    void setaver_score(double sc);  
    double getaver_score();  
    //  
    void show_score();//輸出成績;  
private:  
    string name;  
    double cpp_score;  
    double math_score;  
    double en_score;  
    double total_score;  
    double aver_score;  
};  
void Student::set_name(string na)  
{  
    name = na;  
}  
string Student::get_name()  
{  
    return name;  
}  
void Student::setcpp_score(double sc)  
{  
    cpp_score = sc;  
}  
double Student::getcpp_score()  
{  
    return cpp_score;  
}  
void Student::setmath_score(double sc)  
{  
    math_score = sc;  
}  
double Student::getmath_score()  
{  
    return math_score;  
}  
void Student::seten_score(double sc)  
{  
    en_score = sc;  
}  
double Student::geten_score()  
{  
    return en_score;  
}  
void Student::settotal_score(double sc)  
{  
    total_score = sc;  
}  
double Student::gettotal_score()  
{  
    return total_score;  
}  
void Student::setaver_score(double sc)  
{  
    aver_score = sc;  
}  
double Student::getaver_score()  
{  
    return aver_score;  
}  
//定義void show_score函數;  
void Student::show_score()  
{  
    cout << setw(6) << setiosflags(ios::left) << name << " "   
        << cpp_score << '\t' << math_score << '\t' << en_score << '\t' << total_score << '\t' << aver_score << '\t';          
    cout<<endl;  
}  
 
void cin_score(int num, Student st[]);//從文件得到成績;  
void write_score(int num, Student st[]);//儲存成績  
void write_myscore();//寫入我的成績  
void read_score(int num, Student st[]);//讀取儲存的成績  
int main( )  
{     
    Student st[101];  
    //從文件得到學生成績並作相應的處理  
    cin_score(100, st);  
    //保存成績  
    write_score(100, st);  
    //寫入我的成績  
    write_myscore();  
    //讀取處理後的成績  
    read_score(101, st);  
    //顯示處理後的成績  
    for (int i = 0; i < 101; i++)  
    {  
        st[i].show_score();  
    }  
  
    system("PAUSE");  
    return 0;  
}  
//定義從文件輸入函數;  
void cin_score(int num, Student st[])  
{  
    string name;  
    double cpp_score;  
    double math_score;  
    double en_score;  
  
    ifstream infile("score.dat", ios::in);  
    if (!infile)  
    {  
        cerr << "open error!" << endl;  
        exit(1);  
    }  
    for (int i = 0; i < num; i++)  
    {  
        infile >> name >> cpp_score >> math_score >> en_score;//讀入  
        st[i].set_name(name);   
        st[i].setcpp_score(cpp_score);   
        st[i].setmath_score(math_score);   
        st[i].seten_score(en_score);  
        st[i].settotal_score(cpp_score+math_score+en_score);  
        st[i].setaver_score((cpp_score+math_score+en_score)/3);  
    }  
    infile.close();  
}  
//定義寫出數據函數  
void write_score(int num, Student st[])  
{  
    fstream outfile("binary_score.dat", ios::out|ios::binary);  
    if (!outfile)                          
    {  
        cerr << "open error!" << endl;  
        exit(1);  
    }  
    else  
    {  
        for (int i = 0; i < num; i++)  
        {  
            outfile.write((char *) &st[i], sizeof(st[i]));  
        }  
        outfile.close();  
  
        cout<<"成績已保存!"<<endl;  
    }  
}  
//定義讀取函數  
void read_score(int num, Student st[])  
{  
    fstream infile("binary_score.dat", ios::in|ios::binary);  
    if (!infile)                          
    {  
        cerr << "open error!" << endl;  
        exit(1);  
    }  
    else  
    {  
        for (int i = 0; i < num; i++)  
        {  
            infile.read((char *) &st[i], sizeof(st[i]));  
        }  
        infile.close();  
          
        cout<<"成績已讀取!"<<endl;  
    }  
}  
void write_myscore()  
{  
    fstream writems("binary_score.dat", ios::out|ios::app|ios::binary);//定義寫入文件的方式  
    if (!writems)                          
    {  
        cerr << "open error!" << endl;  
        exit(1);  
    }  
    else  
    {  
        Student st("liqing", 100, 100, 100);  
        writems.write((char *) &st, sizeof(st));      
        cout<<"個人成績已寫入!"<<endl;  
    }  
  
}

感言:
     看了同學的代碼,感覺差距太大了!繼續努力!

發佈了122 篇原創文章 · 獲贊 80 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章