日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++第15周(春)项目2 - 用文件保存的学生名单

發(fā)布時間:2025/3/8 c/c++ 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++第15周(春)项目2 - 用文件保存的学生名单 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

課程首頁在:http://blog.csdn.net/sxhelijian/article/details/11890759。內(nèi)有完整教學方案及資源鏈接


本程序中須要的相關(guān)文件。請到http://pan.baidu.com/s/1qW59HTi下載。

【項目2-用文件保存的學生名單】

  文件score.dat中保存的是若干名學生的姓名和C++課、高數(shù)和英語成績。
  (1)定義學生類,當中包括姓名、C++課、高數(shù)和英語成績及總分數(shù)據(jù)成員,成員函數(shù)依據(jù)須要確定。


//定義學生類 class Student { public://聲明必要的成員函數(shù) private:string name;double cpp;double math;double english;double total;static int stu_num; //學生人數(shù),處理為類的靜態(tài)成員合適static double total_sum; //學生總分和 };
  (2)用對象數(shù)組進行存儲學生的成績。讀入成績并計算總分。將總分高于平均總分且沒掛科的同學的信息保存到文件pass_score.dat中。
int main( ) {Student stud[200],t; //stud[200]為保存數(shù)據(jù)的對象數(shù)組string sname;double total_avg;int i=0;//從文件score.dat中讀入數(shù)據(jù),保存到對象數(shù)組中//總分高于平均總分且沒掛科的同學的信息保存到文件pass_score.dat中return 0; }

  討論:學生人數(shù)和總分的第二種解決方法是用全局變量。

但這兩種信息與學生有關(guān),是學生的“屬性”。成為學生類的數(shù)據(jù)成員合適。這兩種信息由學生總體決定,用作靜態(tài)數(shù)據(jù)成員合適。

查看教材中的相關(guān)部分,復習怎樣進行處理。


參考解答:

#include <fstream> #include<iostream> #include<string> #include<cstdlib> using namespace std;//定義學生類 class Student { public:Student() {};~Student();double get_total();static int get_stu_num();static double get_total_sum();friend istream& operator>>(istream &in, Student &s); //能夠定義input函數(shù)替代friend ostream& operator<<(ostream &out, Student &s); //能夠定義display函數(shù)替代bool pass(); private:string name;double cpp;double math;double english;double total;static int stu_num; //學生人數(shù)。處理為類的靜態(tài)成員合適static double total_sum; //學生總分和 };int Student::stu_num = 0; double Student::total_sum = 0; Student::~Student() {total_sum-=total;stu_num--; }double Student::get_total() {return total; }int Student::get_stu_num() {return stu_num; }double Student::get_total_sum() {return total_sum; }istream& operator>>(istream &in, Student &s) {in>>s.name>>s.cpp>>s.math>>s.english;s.total=s.cpp+s.math+s.english;Student::stu_num++; //在讀入數(shù)據(jù)過程中,用靜態(tài)成員記錄下來詳細的學生人數(shù)和總分和Student::total_sum+=s.total;return in; }ostream &operator<<(ostream &out, Student &s) {out<<s.name<<"\t";out<<s.cpp<<"\t";out<<s.math<<"\t";out<<s.english<<"\t";out<<s.total;return out; }//返回是否全部課程全過了 bool Student::pass() {return cpp>=60 && math>=60 && english>=60; }int main( ) {Student stud[200],t; //stud[200]為保存數(shù)據(jù)的對象數(shù)組string sname;double total_avg;int i=0;//將文件里的數(shù)據(jù)讀入到對象數(shù)組中ifstream infile("score.dat",ios::in); //以輸入的方式打開文件if(!infile) //測試是否成功打開{cerr<<"open error!"<<endl;exit(1);}while(!infile.eof()){infile>>stud[i++]; //讀數(shù)據(jù)中。人數(shù)等信息自己主動地記錄到靜態(tài)成員中。見運算符重載的實現(xiàn)}infile.close();//求總分平均并輸出if(Student::get_stu_num()>0){total_avg = Student::get_total_sum() / Student::get_stu_num();ofstream outfile("pass_score.dat",ios::out);if(!outfile){cerr<<"open error!"<<endl;exit(1);}for(i=0; i<Student::get_stu_num(); i++){if(stud[i].get_total()>total_avg&&stud[i].pass()){outfile<<stud[i]<<endl;}}outfile.close();cout<<"請到文件pass_score.dat中查看名單:"<<endl;}return 0; }



=================== 迂者 賀利堅 CSDN博客專欄================= |== IT學子成長指導專欄 專欄文章的分類文件夾(不定期更新) ==| |== C++ 課堂在線專欄  賀利堅課程教學鏈接(分課程年級) ==| |== 我寫的書——《逆襲大學——傳給IT學子的正能量》    ==| =====?為IT菜鳥起飛鋪跑道。和學生一起享受快樂和激情的大學 =====




總結(jié)

以上是生活随笔為你收集整理的C++第15周(春)项目2 - 用文件保存的学生名单的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。