C++實(shí)現(xiàn)班級成績管理系統(tǒng)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)班級成績管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
本文定義了一個(gè)學(xué)生類的結(jié)構(gòu)體,通過vector 數(shù)組來存儲這個(gè)結(jié)構(gòu)體。通過運(yùn)算求出每個(gè)同學(xué)的總分,平均分。計(jì)算得到班級中各科的最高分和最低分,總成績的最高分和最低分。
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
//引入頭文件
using namespace std;
// 命名空間
//定義學(xué)生結(jié)構(gòu)體
typedef struct Student
{
? ? ?string name;
? ? ?float chinese_score;
? ? ?float math_score;
? ? ?float eglish_score;
? ? ?float sum_score;
? ? ?float avg_score;
}student;
//比較函數(shù)實(shí)現(xiàn)
bool compare_chines(student a,student b)
{
?return a.chinese_score <b.chinese_score ;//通過語文成績排序 < 為升序 >為降序
}
bool compare_math(student a,student b)
{
?return a.math_score <b.math_score ;
}
bool compare_eglish(student a,student b)
{
?return a.eglish_score <b.eglish_score ;
}
bool compare_sum(student a,student b)
{
?return a.sum_score <b.sum_score ;
}
//計(jì)算班級語文,數(shù)學(xué),英語,總成績平均分
void ?class_avg_number(vector <student> s)
{
? ? ?int n=s.size();
? ? ?float sum_avg=0;
? ? ?float chines_avg=0;
? ? ?float math_avg=0;
? ? ?float eglish_avg=0;
? ? ?for(int i=0;i<n;i++)
? ? ? ? ? {
? ? ? ? ? ? ? ?sum_avg+=s[i].sum_score;
? ? ? ? ? ? ? ?chines_avg+=s[i].chinese_score;
? ? ? ? ? ? ? ?math_avg+=s[i].math_score;
? ? ? ? ? ? ? ?eglish_avg+=s[i].eglish_score;
? ? ? ? ? }
? ? ?cout<<"語文平均分 "<<chines_avg/n<<endl;
? ? ?cout<<"數(shù)學(xué)平均分 "<<math_avg/n<<endl;
? ? ?cout<<"英語平均分 "<<eglish_avg/n<<endl;
? ? ?cout<<"總成績平均分 "<<sum_avg/n<<endl;
}
// 計(jì)算語文最高分和最低分
void class_chinses(vector <student> s)
{
? ? ?int n=s.size();
? ? ?sort(s.begin(),s.end(),compare_chines);
? ? ?cout<<"語文最低分"<<s[0].name<<" ?"<<s[0].chinese_score<<endl;
? ? ?cout<<"語文最高分"<<s[n-1].name<<" ?"<<s[n-1].chinese_score<<endl;
}
void class_math(vector <student> s)
{
? ? ?int n=s.size();
? ? ?sort(s.begin(),s.end(),compare_math);
? ? ?cout<<"語文最低分"<<s[0].name<<" ?"<<s[0].math_score<<endl;
? ? ?cout<<"語文最高分"<<s[n-1].name<<" ?"<<s[n-1].math_score<<endl;
}
void class_eglish(vector <student> s)
{
? ? ?int n=s.size();
? ? ?sort(s.begin(),s.end(),compare_eglish);
? ? ?cout<<"語文最低分"<<s[0].name<<" ?"<<s[0].eglish_score<<endl;
? ? ?cout<<"語文最高分"<<s[n-1].name<<" ?"<<s[n-1].eglish_score<<endl;
}
void class_sum(vector <student> s)
{
? ? ?int n=s.size();
? ? ?sort(s.begin(),s.end(),compare_sum);
? ? ?cout<<"語文最低分"<<s[0].name<<" ?"<<s[0].sum_score<<endl;
? ? ?cout<<"語文最高分"<<s[n-1].name<<" ?"<<s[n-1].sum_score<<endl;
}
int main()
{
? ? ?int n;
? ? ?cout<<"請輸入學(xué)生人數(shù)"<<endl;
? ? ?cin>>n;
? ? ?vector<student> v;// 定義結(jié)構(gòu)體student 類型的數(shù)組
? ? ?cout<<"請輸入學(xué)生的姓名,語文,數(shù)學(xué),英語成績(用空格隔開)"<<endl;
? ? ?student aa;
? ? ?for(int i=0;i<n;i++)
? ? ?{
? ? ? ? ? cin>>aa.name>>aa.chinese_score>>aa.math_score>>aa.eglish_score;
? ? ? ? ? v.push_back(aa);//將其aa 添加到數(shù)組中
? ? ?}
? ? ?for(int i=0;i<n;i++)
? ? ?{
? ? ? ? ? v[i].sum_score=v[i].chinese_score+v[i].math_score+v[i].eglish_score;//計(jì)算總分
? ? ? ? ? v[i].avg_score=(v[i].chinese_score+v[i].math_score+v[i].eglish_score)/3;//計(jì)算平均分
? ? ?}
? ? ?//計(jì)算班級平均分
? ? ?cout<<"學(xué)生姓名 ?三門課總分 平均分"<<endl;
? ? ?for(int i=0;i<n;i++)
? ? ? ? ? cout<<v[i].name<<" "<<v[i].sum_score<<" "<<v[i].avg_score<<endl;
? ? ?//計(jì)算班級平均分
? ? ?cout<<"班級各類平均分如下"<<endl;
? ? ?class_avg_number(v);
? ? ?cout<<"班級語文最高分和最低分為"<<endl;
? ? ?class_chinses(v);
? ? ?cout<<"班級英語最高分和最低分為"<<endl;
? ? ?class_eglish(v);
? ? ?cout<<"班級數(shù)學(xué)最高分和最低分為"<<endl;
? ? ?class_math(v);
? ? ?cout<<"班級總分最高分和最低分為"<<endl;
? ? ?class_sum(v);
? ? ?return 1;
}測試樣例
7
張三 78 89 45
庫里 99 45 89
詹姆斯 85 66 90
格林 45 85 92
湯普森 78 92 90
歐文 78 89 45
杜蘭特 89 94 99
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++詳細(xì)講解內(nèi)存管理工具primitives
文章向大家介紹C++內(nèi)存管理primitives,主要包括primitives使用實(shí)例、應(yīng)用技巧、基本知識點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下2022-06-06
C++設(shè)計(jì)一個(gè)簡單內(nèi)存池的全過程
利用C/C++開發(fā)大型應(yīng)用程序中,內(nèi)存的管理與分配是一個(gè)需要認(rèn)真考慮的部分,下面這篇文章主要給大家介紹了關(guān)于C++設(shè)計(jì)一個(gè)簡單內(nèi)存池的全過程,需要的朋友可以參考下2021-09-09
C++?中如何結(jié)束?while?(cin>>str)?的輸入
這篇文章主要介紹了C++?中如何結(jié)束?while?(cin>>str)?的輸入,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
解決Microsoft?Visual?C++?2010?Express?運(yùn)行及調(diào)試問題
這篇文章主要介紹了解決Microsoft?Visual?C++?2010?Express?運(yùn)行及調(diào)試問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
cocos2dx實(shí)現(xiàn)橡皮擦效果以及判斷是否擦除完畢
這篇文章主要為大家詳細(xì)介紹了cocos2dx實(shí)現(xiàn)橡皮擦效果以及判斷是否擦除完畢,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

