各路大神 小弟编的有点简单 能帮我改一下吗?
#include<string>#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<iostream>
#include<fstream>
using namespace std;
struct Course
{
string subject; // 课程
double cdt; // 学分
double score; // 成绩
string quality; // 成绩性质
double GPA; // 绩点
Course *next;
Course(string sub="",double cre=0,double s=0,string Q="");
void output();
};
Course::Course(string sub,double cre,double s,string q)
{
subject=sub;
cdt=cre;
score=s;
quality=q;
if(score-50>=1e-5) //将GPA 的计算直接包含在构造函数内,而不是单独拿出去
GPA=(score-50)*0.1;
else
GPA=0.0;
if(quality=="you") GPA=4.0;
if(quality=="liang") GPA=3.0;
if(quality=="zhong") GPA=2.0;
if(quality=="jige") GPA=1.0;
if(quality=="bujige") GPA=0.0;
}
void Course::output()
{
cout<<setw(20)<<subject<<setw(20)<<cdt<<setw(20)<<GPA<<endl;
}
struct Project
{
Course *head;
double ave;
int n;
Project()
{
head=new Course;
head->next=NULL;
n=0;
}
void add(string sub="",double cre=0,double s=0,string Q="");
void Add();
void Sequence();
void output();
void aver();
};
void Project::add(string sub,double cre,double s,string q)
{
Course *array=new Course(sub,cre,s,q);
if(head->next==NULL)
{
head->next=array;
array->next=NULL;
}
else
{
Course *Q=head->next;
head->next=array;
array->next=Q;
}
n++;
}
void Project::Add()
{
string sub;
double cre;
char a[20];
cin>>sub>>cre>>a;
if(a[0]>='0' && a[0]<='9')
{
double s=atoi(a);
add(sub,cre,s);
}
else
{
string ss=a;
add(sub,cre,0,ss);
}
}
void Project::Sequence()
{
for(int i=1;i<=n;i++)
{
Course *array=head->next,*Q=array->next,*temp=head; ///temp,array,q 三个指针连续
int ok=0;
while(array->next!=NULL)
{
if((array->GPA) < (Q->GPA)) //节点的交换需保存前后两个节点
{
temp->next=Q;
array->next=Q->next;
Q->next=array;
temp=Q;
Q=array->next;
ok=1;
}
else
{
temp=temp->next; ////不用交换,三个指针同时后移
array=array->next;
Q=Q->next;
}
}
if(ok==0)
break;
}
}
void Project::output()
{
cout<<setw(20)<<"科目"<<setw(20)<<"学分"<<setw(20)<<"绩点"<<endl;
Course *array=head->next;
while(array!=NULL)
{
array->output();
array=array->next;
}
}
void Project::aver()
{
Course *array=head->next;
double sum1=0,sum2=0;
while(array!=NULL)
{
sum1+=array->cdt*array->GPA;
sum2+=array->cdt;
array=array->next;
}
ave=sum1/sum2;
cout<<ave<<endl;
if(ave>1.8)
cout<<"Congratulations!您的绩点合格,好样的"<<endl;
else
cout<<"绩点偏低,努力"<<endl;
}
int main()
{
Project project;
int menu,sonmenu=1;
repeat1:
cout<<"请选择:"<<endl;
cout<<" 0.退出"<<endl;
cout<<" 1.进入系统 输入分数查阅绩点"<<endl;
cin>>menu;
if(menu==0)
return 0;
cout<<" "<<setw(50)<<"请按如下顺序输入信息:"<<endl;
cout<<setw(20)<<"科目"<<setw(20)<<"学分"<<setw(20)<<"成绩/等级"<<endl;
while(sonmenu)
{
project.Add();
cout<<"继续,添加:1"<<endl;
cout<<"输入结束:0"<<endl;
cin>>sonmenu;
}
//菜单
cout<<"请选择:"<<endl;
cout<<" A.我要查看 降序排列的 学科总绩点(= 学分 x 绩点);\n\n B.我要查看 平均绩点。\n"<<endl<<endl;
cout<<"请选择 (大写): ";
char Menu = '0';
cin>>Menu;
switch(Menu)
{
case 'A':
project.Sequence();project.output();break;
case 'B':
{
cout<<"您的平均绩点为: ";
project.aver();
// project.OUTAG();
cout<<endl;break;
}
default: cout<<"对不起,您的输入有误!"<<endl<<endl;
}
cout<<"继续查询:3"<<endl;
cin>>Menu;
if(Menu=='3')
goto repeat1;
return 0;
}