学分和绩点计算器
(1) 输入课程名、学分和成绩,通过绩点计算公式计算相应的绩点,满足如下要求:根据南京邮电大学的本科生《南京邮电大学学生学籍管理办法》规定:为反映学生学习的质量,采用学分绩点制的学习质量评定方法。学生学分绩点以平均学分绩点(GPA) 表示,其计算方法如下:
低于60分折合为0绩点,不及格折合为0绩点
60—69 分折合为1.0—1.9绩点,及格折合为1.5绩点;
70—79 分折合为2.0—2.9绩点,中等折合为2.5绩点;
80—89 分折合为3.0—3.9绩点,良好折合为3.5绩点;
90—100分折合为4.0—5.0绩点,优秀折合为4.5绩点。
平均学分绩点(GPA) = ∑(课程学分数×绩点数×K)
________________________________________ ∑课程学分数
平均学分绩点分为学年平均学分绩点和累积平均学分绩点:即按学年结算的平均学分绩点称为学年平均学分绩点;从入学后累积结算的平均学分绩点积为累积平均学分绩点。
(2) 判断所获得的绩点是否合格,能给出相应的结果提示;
(3) 程序操作友好、界面美观。
2. 扩展要求。
(1) 从文件中读取课程的学分与成绩,并保存绩点计算的结果;并根据排序算法排序,给出每门课程所获得的绩点。
(2) 实现绩点的预警功能。如按照班级(或者专业)的学分统计查询功能,从文件读入多个学生的学分绩点,按照总的绩点排序。
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
class Course{
private:
int no;//序号
string year;//学年学期
int code;//课程代码
string name;//课程名称
float credit;//学分
int score;//成绩
string quality;//成绩性质
string courseQua;//课程性质
float gpa;//绩点,(非绩点和)
public:
//构造与析构 及赋值运算符
Course(){}
Course(int noV,string yearV,int codeV,string nameV,float creditV,int scoreV,string qualityV,string courseQuaV="#")
:no(noV),year(yearV),code(codeV),name(nameV),credit(creditV),score(scoreV),quality(qualityV),courseQua(courseQuaV){}
Course(const Course& temp)
{
no = temp.no;
year = temp.year;
code = temp.code;
name = temp.name;
credit = temp.credit;
score = temp.score;
quality = temp.quality;
courseQua = temp.courseQua;
gpa = temp.gpa;
}
~Course(){}
Course& operator = (const Course& temp)
{
no = temp.no;
year = temp.year;
code = temp.code;
name = temp.name;
credit = temp.credit;
score = temp.score;
quality = temp.quality;
courseQua = temp.courseQua;
gpa = temp.gpa;
return *this;
}
//访问器成员函数
string getYear() const {return year;}
string getName() const {return name;}
float getCredit() const {return credit;}
float getGpa() const {return gpa;}
int getScore() const {return score;}
//mutator 函数
void set(int noV,string yearV,int codeV,string nameV,float creditV,int scoreV,string qualityV,string courseQuaV="#")
{
no=noV;
year=yearV;
code=codeV;
name=nameV;
credit=creditV;
score=scoreV;
quality=qualityV;
courseQua=courseQuaV;
}
void setGpa()
{
if(score<60) gpa = 0;
if(score>=60&&score<=62) gpa = 1.0;
if(score>=63&&score<=65) gpa = 1.5;
if(score>=66&&score<=69) gpa = 1.8;
if(score>=70&&score<=72) gpa = 2.0;
if(score>=73&&score<=75) gpa = 2.5;
if(score>=76&&score<=79) gpa = 2.8;
if(score>=80&&score<=82) gpa = 3.0;
if(score>=83&&score<=85) gpa = 3.5;
if(score>=86&&score<=89) gpa = 3.8;
if(score>=90&&score<=92) gpa = 4.0;
if(score>=93&&score<=95) gpa = 4.5;
if(score>=96&&score<=100) gpa = 4.8;
}
void show()
{
cout<<left<<setw(10)<<year<<setw(36)<<name;
cout<<setw(5)<<credit<<setw(5)<<score;
cout<<setw(6)<<quality<<setw(6)<<gpa<<endl;
}
//运算符重载
bool operator < (const Course& temp) const
{
if(score < temp.score) return true;
else return false;
}
bool operator > (const Course& temp) const
{
if(score > temp.score) return true;
else return false;
}
friend class List;
};
class CourseSigma: public Course
{
private:
float creditPlusGpa;
public:
CourseSigma(const Course& temp):Course(temp),creditPlusGpa(temp.getCredit() * temp.getGpa()){}
bool operator < (const CourseSigma& temp) const
{
return creditPlusGpa < temp.creditPlusGpa? true : false ;
}
bool operator > (const CourseSigma& temp) const
{
return creditPlusGpa > temp.creditPlusGpa? true : false ;
}
void show()
{
cout<<left<<setw(10)<<getYear()<<setw(36)<<getName();
cout<<setw(5)<<getCredit()<<setw(5)<<getScore();
cout<<setw(6)<<getGpa()<<setw(5)<<creditPlusGpa<<endl;
}
};
class List{
public:
vector<Course> data;
public:
List(){}
~List(){}
//
void add(const Course& temp){data.push_back(temp);}//在矢量尾添加一个元素
void sort_des(){sort( data.begin() , data.end() , less<Course>() );}//升序排序
void sort_asc(){sort( data.begin() , data.end() , greater<Course>() );}//降序排序
void clear_list(){ data.clear(); }
int getAmount()
{
int count = 0 ;
vector<Course>::iterator iter = data.begin();
while( iter != data.end() )
{
count++;
iter++;
}
return count;
}
void show()
{ int i=0;
vector<Course>::iterator iter = data.begin();
while( iter != data.end() )
{cout<<left<<setw(3)<<i++; iter++->show();}
}
//用于输出运算后的内容
float getCreditSum()//获得学分和
{
float sum = 0;
vector<Course>::iterator iter = data.begin();
while( iter != data.end() )
{
sum += iter->getCredit();
iter ++;
}
return sum;
}
float getGpaSum()//获得绩点和(Σ学分*绩点)
{
float sum = 0;
vector<Course>::iterator iter = data.begin();
while( iter != data.end() )
{
sum += ( (iter->getCredit()) * (iter->getGpa()) );
iter++;
}
return sum;
}
float getAverageGpa(){return getGpaSum()/getCreditSum(); }
};
void declaration();//用于在程序之前输出内容声明
void stat_a();
void stat_b();
void stat_c();
void stat_d();
void stat_e();
List list;//数据矢量作为全局对象
float gpa_sum = 0;
float credit_sum = 0;
float average_gpa = 0;
List list2;
void function1_b();
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
int main()
{
declaration();
//功能实现
//打开文件,载入数据,生成数据库, 显示数据
ifstream infile;
infile.open("1.txt");
if(!infile) cout<<"文件打开失败! @_@# 请确认您的工作目录下包含文件\"1.txt\" !"<<endl;
else
{
cout<<"\a数据开始载入!"<<endl<<endl;
int no;//序号 0
string year;//学年学期 1
int code;//课程代码 2
string name;//课程名称 3
float credit;//学分 4
int score;//成绩 5
string quality;//成绩性质 6
string courseQua;//课程性质 7
Course courseTemp;
string::size_type pos[10]={0};
string data[8];
string buf;//存储从文件中读来的一行数据
while( getline( infile , buf ) )
{
istringstream inStr;
istringstream inStr2,inStr4,inStr5;
pos[0] = buf.find_first_not_of(" ");
pos[1] = buf.find_first_of(" " , pos[0] );
data[0] = buf.substr(pos[0] , pos[1]-pos[0]);
//cout<<data[0]<<"x"<<endl;
if(data[0] != "序号") {inStr.str(data[0]); inStr>>no;inStr.sync();}
else continue;
for(int i = 1 ; i <= 6 ; i ++ )
{
pos[i] = buf.find_first_not_of(" ", pos[i] );
pos[i+1] = buf.find_first_of(" " , pos[i] );
data[i] = buf.substr(pos[i] , pos[i+1]-pos[i]);
//cout<<data[i]<<endl;
/*switch(i)
{
case 1:year = data[1]; break;
case 2:{istringstream inStr2(data[2]); inStr>>code; break;}
case 3:name = data[3]; break;
case 4:{istringstream inStr4(data[4]); inStr>>credit; break;}
case 5:{istringstream inStr5(data[5]); inStr>>score; break;}
case 6:quality = data[6]; break;
}*/
}
year = data[1];
inStr2.str( data[2] ); inStr2>>code;//cout<<inStr2.str()<<" "<<code<<endl;
name = data[3];
inStr4.str( data[4] ); inStr4>>credit;//cout<<inStr4.str()<<" "<<credit<<endl;
if( data[5] == "优" ) score = 95;
else if( data[5] == "良" ) score = 85;
else if( data[5] == "中" ) score = 75;
else if( data[5] == "及格" ) score = 65;
else if( data[5] == "不及格" ) score = 0;
else
{inStr5.str( data[5] ); inStr5>>score;}//cout<<inStr5.str()<<" "<<score<<endl;}
quality = data[6];
//cout<<left<<setw(2)<<no<<left<<setw(10)<<year<<left<<setw(30)<<name<<left<<setw(5)<<credit<<left<<setw(5)<<score<<left<<setw(6)<<quality<<endl;
courseTemp.set(no , year , code , name , credit , score , quality);
courseTemp.setGpa();
list.add(courseTemp);
}
}
//进行各门课程绩点的计算与设置
cout<<endl<<"数据载入成功!可以进行运算了。"<<endl<<endl;
list.show();cout<<endl;
system("pause");
cout<<"--------------------------------\a我是华丽的分割线------------------------------"<<endl<<endl;
//进入功能菜单
cout<<"请选择:"<<endl;
cout<<" a.我要查看 降序排列的成绩;\n\n b.我要查看 降序排列的 学科总绩点(= 学分 x 绩点);\n\n c.我要查看 平均绩点。\n"<<endl<<endl;
cout<<"请选择 (不区分大小写): ";
char choice = '0';
cin>>choice;
cout<<endl;
bool end=false;
while( 1 )
{
switch(choice)
{
case 'a': case 'A': cout<<"-----------------------------------------------------------------------------\n"<<endl;cout<<"\a按成绩从高到低: \n"<<endl; list.sort_asc();list.show();cout<<endl;break;
case 'b': case 'B': cout<<"-----------------------------------------------------------------------------\n"<<endl;function1_b();cout<<'\a'<<endl;break;
case 'c': case 'C':
{
cout<<"-----------------------------------------------------------------------------\n"<<endl;
cout<<"\a您的 "<<list.getAmount()<<" 门成绩: 总学分为: "<<list.getCreditSum()<<" ; 平均绩点为: "<<list.getAverageGpa()<<" 。"<<endl;
end=true;cout<<endl;break;
}
default: cout<<"对不起,您的输入有误!"<<endl<<endl;cout<<"\a-----------------------------------------------------------------------------\n"<<endl;
}
system("pause");
cin.sync();//【】刷新cin缓冲区??
if( end == true ) {cout<<"--------------------------------\a我是华丽的分割线------------------------------"<<endl;break;}
cout<<"--------------------------------\a我是华丽的分割线------------------------------\n"<<endl;
cout<<" a.我要查看 降序排列的成绩;\n\n b.我要查看 降序排列的 学科总绩点(= 学分 x 绩点);\n\n c.我要查看 平均绩点。\n"<<endl<<endl;
cout<<"请选择 (不区分大小写): ";
cin>>choice;
cout<<endl;
}
::gpa_sum = list.getGpaSum();
::credit_sum = list.getCreditSum();
::average_gpa = list.getAverageGpa();
ofstream out;
out.open("成绩结果输出.txt");
out<<"您的总学分为: "<< credit_sum <<endl;
out<<"您的所有课程的平均绩点为:"<< average_gpa <<endl;
out<<"\n"<< endl <<endl;
//out.close();
//统计功能
/*
1.按学期。学年 统计学分和绩点
2.低于平均绩点的学科,对绩点贡献
3.高于平均绩点的学科,
4.最不淡定的成绩,高一分则增加多少绩点
*/
end=false;
while(true)
{
cout<<endl<<endl;
cout<<"【统计功能菜单】\n"<<endl;
cout<<"请 选 择 :\n"<<endl;
cout<<"a.查看【低于平均绩点】的学科;\n"<<endl;
cout<<"b.查看【高于平均绩点】的学科;\n"<<endl;
cout<<"c.查看各【学期】获得的学分、绩点;\n"<<endl;
cout<<"d.查看各【学年】获得的学分、绩点;\n"<<endl;
cout<<"e.查看让你【不淡定】的学科。\n"<<endl;
cout<<"f.退出。\n"<<endl;
cout<<"请 选 择 :";
cin>>choice;
cout<<"\n"<<endl;
cin.sync();
cout<<endl;
switch(choice)
{
case 'a': case 'A': stat_a(); break;
case 'b': case 'B': stat_b(); break;
case 'c': case 'C': stat_c(); break;
case 'd': case 'D': stat_d(); break;
case 'e': case 'E': stat_e(); break;
case 'f': case 'F': cout<<"感谢您的使用!\n"<<endl; end=true; break;
default:
cout<<"对不起,您的输入有误!"<<endl<<endl;
cout<<"\a-----------------------------------------------------------------------------\n"<<endl;
}
if( end ) break;
}
cout<<"\a\a\a\a\aBy 张哲 [仪器科学与工程学院] 2010级\n"<<endl;
cout<<"程序终止。\n"<<endl;
system("pause");
system("pause");
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void function1_b()
{
vector<CourseSigma> list3;
vector<Course>::iterator iter = ::list.data.begin();
while( iter != ::list.data.end() )
{
//(*iter).show();
list3.push_back(CourseSigma(*iter));
iter++;
}
sort(list3.begin(), list3.end(), greater<CourseSigma>() );
cout<<left<<setw(10)<<"学年:"<<setw(36)<<"学科:"<<setw(5)<<"学分:"<<setw(5)<<"成绩:"<<setw(6)<<"绩点:"<<setw(5)<<"学分 x 绩点"<<endl;
vector<CourseSigma>::iterator iter2 = list3.begin();
while( iter2 != list3.end())
(*iter2++).show();
cout<<endl<<endl;
}
void stat_a()//【低于平均绩点】
{
cout<<"-----------------------------------------------------------------------------\n"<<endl;
list2.clear_list();
for(vector<Course>::iterator iter = list.data.begin() ; iter < list.data.end() ; iter ++ )
{
if( iter->getGpa() < ::average_gpa ) list2.add(*iter);
}
cout<<"您的【低于】平均绩点的课程如下:"<<endl<<endl;
list2.show();
cout<<endl<<endl;
cout<<"--------------------------------\a我是华丽的分割线------------------------------"<<endl<<endl;
system("pause");
}
void stat_b()//【高于平均绩点】
{
cout<<"-----------------------------------------------------------------------------\n"<<endl;
list2.clear_list();
for(vector<Course>::iterator iter = list.data.begin() ; iter < list.data.end() ; iter ++ )
{
if( iter->getGpa() > ::average_gpa ) list2.add(*iter);
}
cout<<"您的【高于】平均绩点的课程如下:"<<endl<<endl;
list2.show();
cout<<endl<<endl;
cout<<"--------------------------------\a我是华丽的分割线------------------------------"<<endl<<endl;
system("pause");
}
void stat_c()//【学期】
{
cout<<"-----------------------------------------------------------------------------"<<endl;
vector<string> semester_list;
semester_list.push_back(list.data.front().getYear());
bool is_inList = false;
for(vector<Course>::iterator iter = list.data.begin()+1; iter != list.data.end(); iter++)
{
for(vector<string>::iterator iter2 = semester_list.begin(); iter2 != semester_list.end(); iter2++)
{
if((*iter).getYear() == *iter2) { is_inList = true ; break;}
}
if(is_inList == false)
{
//cout<<(*iter).getYear()<<endl;
semester_list.push_back((*iter).getYear());
}
is_inList = false ;
}
int count_years = semester_list.size();
vector< vector<Course> > list_by_year(count_years);
vector< vector<Course> >::iterator iter_complex = list_by_year.begin();
vector<Course>::iterator iter_Course;
vector<string>::iterator iter_string;
for(iter_string = semester_list.begin(); iter_string != semester_list.end(); iter_string++)
{
for(iter_Course = ::list.data.begin(); iter_Course != ::list.data.end(); iter_Course++)
{
if((*iter_string) == (*iter_Course).getYear() ) (*iter_complex).push_back(*iter_Course);
}
iter_complex++;
}
for(iter_complex = list_by_year.begin(); iter_complex != list_by_year.end(); iter_complex++)
{
cout<<endl;
cout<<(*iter_complex).front().getYear()<<"学期 :\n "<<endl;
/*
for(iter_Course = (*iter_complex).begin(); iter_Course != (*iter_complex).end(); iter_Course++)
{
(*iter_Course).show();
}
*/
List list_temp;
list_temp.data.assign((*iter_complex).begin(),(*iter_complex).end());
list_temp.show();
cout<<endl;
cout<<"该学期您取得的总学分为:"<<list_temp.getCreditSum() <<" 平均绩点为:"<<list_temp.getAverageGpa() <<endl;
cout<<endl;
cout<<"-----------------------------------------------------------------------------"<<endl;
}
cout<<'\a'<<endl;
system("pause");
}
void stat_d()//【学年】
{
cout<<"-----------------------------------------------------------------------------"<<endl;
vector<string> semester_list;
semester_list.push_back(list.data.front().getYear().substr(0,5));
bool is_inList = false;
for(vector<Course>::iterator iter = list.data.begin()+1; iter != list.data.end(); iter++)
{
for(vector<string>::iterator iter2 = semester_list.begin(); iter2 != semester_list.end(); iter2++)
{
if((*iter).getYear().substr(0,5) == *iter2) { is_inList = true ; break;}
}
if(is_inList == false)
{
//cout<<(*iter).getYear()<<endl;
semester_list.push_back((*iter).getYear().substr(0,5));
}
is_inList = false ;
}
int count_years = semester_list.size();
vector< vector<Course> > list_by_year(count_years);
vector< vector<Course> >::iterator iter_complex = list_by_year.begin();
vector<Course>::iterator iter_Course;
vector<string>::iterator iter_string;
for(iter_string = semester_list.begin(); iter_string != semester_list.end(); iter_string++)
{
for(iter_Course = ::list.data.begin(); iter_Course != ::list.data.end(); iter_Course++)
{
if((*iter_string) == (*iter_Course).getYear().substr(0,5) ) (*iter_complex).push_back(*iter_Course);
}
iter_complex++;
}
for(iter_complex = list_by_year.begin(); iter_complex != list_by_year.end(); iter_complex++)
{
cout<<endl;
cout<<(*iter_complex).front().getYear().substr(0,5)<<"学年 :\n "<<endl;
/*
for(iter_Course = (*iter_complex).begin(); iter_Course != (*iter_complex).end(); iter_Course++)
{
(*iter_Course).show();
}
*/
List list_temp;
list_temp.data.assign((*iter_complex).begin(),(*iter_complex).end());
list_temp.show();
cout<<endl;
cout<<"该学年您取得的总学分为:"<<list_temp.getCreditSum() <<" 平均绩点为:"<<list_temp.getAverageGpa() <<endl;
cout<<endl;
cout<<"-----------------------------------------------------------------------------"<<endl;
}
cout<<'\a'<<endl;
system("pause");
}
void stat_e()//【不淡定】2,5,9
{
cout<<"-----------------------------------------------------------------------------\n"<<endl;
cout<<"功能简介:\n"<<endl;
cout<<"此功能将统计您的成绩的个位为[2,5,9]的学科\n\n在此情况下,当您的成绩再增加1分,您的绩点会相应的上升一个等级.\n\n"<<endl;
cout<<"友情提示:\n\n 老师必定也不会故意将您的成绩绩点降低等级的,要相信老师已经努力帮您提分了\n\n 所以当您看到结果时,就请不要在心中默默问候老师了.....囧rz\n\n"<<endl;
vector<Course> list_NotCalm;
int mod = 0;
for(vector<Course>::iterator iter = list.data.begin(); iter != list.data.end(); iter++)
{
mod = (*iter).getScore()%10;
if(mod == 2 || mod == 5 || mod == 9)
list_NotCalm.push_back(*iter);
}
if(list_NotCalm.size() == 0) cout<<"恭喜您!老师都很给力啊!"<<endl;
else
{
cout<<left<<setw(10)<<"学年:"<<setw(36)<<"学科:"<<setw(5)<<"学分:"<<setw(5)<<"成绩:"<<setw(6)<<"性质:"<<setw(6)<<"绩点:"<<endl;
for(vector<Course>::iterator iter2 = list_NotCalm.begin(); iter2 != list_NotCalm.end(); iter2++)
(*iter2).show();
cout<<endl;
cout<<"-----------------------------------------------------------------------------"<<endl;
cout<<'\a'<<endl;
system("pause");
}
void declaration()//用于在程序之前输出内容声明
{ //cout<<""<<endl; cout<<endl;
cout<<"********************************************"<<endl;//再版声明
cout<<"*【东南大学】 学分绩点计算器 1.0.2 升级版 *"<<endl;
cout<<"********************************************"<<endl;
cout<<" -----By 张哲 "<<endl;
cout<<" -----From 【220仪科学院】"<<endl;
cout<<" 2011/7/5 发布"<<endl;
cout<<""<<endl;//程序使用说明
cout<<"What's NEW !(新版本更新了什么功能)"<<endl;//what's new
cout<<"1.美化输出格式;\n\n2.使用STL库,优化内存管理;\n\n3.新增计算总学分功能;\n\n4.新增多种绩点分析功能;\n\n5.优化了数据读入,无需手动修改\"优良中及格\"形式的成绩。\n"<<endl;
cout<<"请按任意键,进入 “使用说明” 界面。\n"<<endl;
system("pause");
cout<<"--------------------------------\a我是华丽的分割线------------------------------"<<endl;
cout<<"\n\n【使 用 说 明 :】"<<endl<<endl;
cout<<"1.打开校园网的【学生登录】网页,并登入【成绩服务】--【成绩查询】;\n"<<endl;
cout<<"2.将成绩表格全部复制下来(【注意】:最下面的【学年】和【总学分】两行不要!);\n"<<endl;
cout<<"3.将刚刚复制的表格粘贴在 ”1.txt“。【 请保证文件与该程序在【同一目录下】 】;\n"<<endl;
cout<<"4.请自行删去【不计算绩点】的诸如人文,经管,自然科学等课程;\n"<<endl;
cout<<"5.如果您确保以上操作已经正确执行,\n"<<endl;
system("pause");
//cout<<"\n"<<endl;
//cout<<"\n"<<endl;
cout<<endl;
cout<<endl;
cout<<"--------------------------------\a我是华丽的分割线------------------------------"<<endl;
}
大一新生.啥都不懂啊!有一个错误,求解答!万分感谢!!!