如何编写能录入学生成绩和修改并且能查询学生平均成绩和总成绩及班级排名的程序。
答案:2 悬赏:70
解决时间 2021-01-30 02:28
- 提问者网友:伴他一生,无悔
- 2021-01-29 13:43
相关是C语言编写代码
最佳答案
- 二级知识专家网友:抱不住太阳的深海
- 2021-01-29 15:06
这个是 链表的 你知道的~~~
全部回答
- 1楼网友:堕落奶泡
- 2021-01-29 16:11
#include
# include
# include
# include
# include
# include
//
struct stuatom
{
char *name;
int id;
char sex;
float math, eng, comp, totll, aver;
void show();
void setup();
};
//
class student
{
private:
stuatom ob[100];
int stulen;
public:
student();
void input();
void order();
void save();
void check();
void clean();
void query();
void read();
void add();
void del();
void modify();
};
//
student::student()
{
//用for循环对全部数组中的数据初始化
for(int i=0;i <100;i++)
{
ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;
ob[i].id =0;
ob[i].sex =' ';
ob[i].name =null;
}
this->stulen =0;
}
//
void stuatom::setup()
{
char n[20]; char s;
int b;
//如果输入学号在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
cout<<" 学号(1001与1020之间): ";
cin>>b;
if(b>1020||b <1001)
cout<<" 数据有误!!重新输入.."<1020);
id=b;
//如果输入姓名在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
do
{
name=new char[strlen(n)+1];
cout<<" 姓名: ";
cin>>n;
}while ( strlen(n)>6 && strlen(n) <4 ); //等于没判断
strcpy(name,n);
cout<<" 性别(m/f):" ;
cin>>s;
//如果输入性别在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (s!='m' && s!='f')
{
cout<<" 数据有误!!重新输入.."<>s;
}
sex=s;
float m, e, co;
cout<<" 数学(0-100): ";
cin>>m;
//如果输入成绩在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (m <0 || m>100)
{
cout<<" 数据有误!!重新输入.."<>m;
}
math=m;
cout<<" 英语(0-100): ";
cin>>e;
while (e <0 || e>100)
{
cout<<" 数据有误!!重新输入.."<>e;
}
eng=e;
cout<<" c++(0-100): ";
cin>>co;
while (co <0 || co>100)
{
cout<<" 数据有误!!重新输入.."<>co;
}
comp=co;
totll=math+eng+comp;
aver=(math+eng+comp)/3;
}
//
void stuatom::show()
{
cout.setf(ios::left);
cout.width(6);
cout<<""<n;
int j,i;
//通过循环输入要求输入学生个数的学生的数据。
for(j=(this->stulen+1) ; j <=(this->stulen+n); j++)
{
cout <<" 输入学生信息 " <stulen!=0)
{
for (i=0;istulen ;i++)
{
for(j=this->stulen ;j<(this->stulen+n);j++)
{
if (ob[i].id==ob[j].id)
{
if (c==0) //c只是为了美观而已。。
{
cout<<" 错误!系统检测到第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
a[b]=j;
b++;c++;
}
else
{
cout<<" 第"<<(j+1 )<<"位学生学号与第"<<(i+1)<<"位学生学号相同\n";
a[b]=j;
b++;c++;
}
}
}
}
for(c=0;c>y;
while(y!='y'&& y!='y'&& y!='n'&& y!='n')
{
cout<<" 无效的命令,请重新输入..\n";
cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(y/n--选n则第"<<(a[c]+1)<<"位学生成绩将不保存)";
cin>>y;
}
if(y=='y'||y=='y')
{
d=a[c];
ob[d].setup();
}
else
{
n--;
for(d=a[c];d<(this->stulen+n);d++)
ob[d]=ob[d+1];
for(d=(c+1);d>y;
while(y!='y'&& y!='y'&& y!='n'&& y!='n')
{
cout<<" 无效的命令,请重新输入..\n";
cout<<" 是否重新输入第"<<(a[c]+1)<<"位学生成绩?(y/n--选n则第"<<(a[c]+1)<<"位学生成绩将不保存)";
cin>>y;
}
if(y=='y'||y=='y')
{
d=a[c];
ob[d].setup();
}
else
{
n--;
for(d=a[c];dstulen ; j <(this->stulen+n); j++)
{
ob[j].show();
}
this->stulen+=n; //学生个数赋值
}
//
void student::query()
{
int x;
cout <>x;
int i;
for(i=0;i stulen ;i++)
{
if (x==ob[i].id)
break;
}
if(istulen )
{
cout <<" 学号 姓名 性别 数学 英语 c++ 总分 平均分" <stulen; i++)
{
outfile <stulen ;
outfile1.close();
}
//
void student::clean()
{
int i;
char b[20];
ifstream infile;
infile.open("list.txt",ios::in);
if(!infile)
{
for(int i=(this->stulen-1);i <100;i++)
{
ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;
ob[i].id =0;
ob[i].sex =' ';
ob[i].name =null;
}
this->stulen=0;
ifstream infile1;
infile1.open("stulen.txt",ios::in);
infile1>>this->stulen ;
infile1.close();
}
else
{
ifstream infile1;
infile1.open("stulen.txt",ios::in);
infile1>>this->stulen ;
infile1.close();
for(i=0;istulen ;i++)
{
infile>>ob[i].id;
ob[i].name =new char[strlen(b)+1];
infile>>b;
strcpy(ob[i].name,b);
infile>>ob[i].sex ;
infile>>ob[i].math ;
infile>>ob[i].eng;
infile>>ob[i].comp;
infile>>ob[i].totll;
infile>>ob[i].aver ;
}
infile.close ();
}
}
//
void student::read()
{
int i;
float averm,avere,averc;
averm=avere=averc=0;
//system("cls");
cout <stulen; i++)
{
ob[i].show();
}
cout <<"\n\n\n";
for(i=0; i stulen; i++)
{
averm +=ob[i].math;
}
if(i!=0)
cout <<" 数学平均成绩为" stulen++;
ofstream fout("list.txt",ios::app);
if(!fout)
{
cout <<" 无法打开数据文件!\n,";
}
//把添加的学生数据添加到list.txt里边去。
fout <stulen ;
outfile1.close();
_getch();
}
//
void student::del()
{
int i,p;
int x;
p=-1;
if(this->stulen==0)
{
cout<<" 目前没有学生成绩!";
_getch();
}
else
{
cout <stulen; i++)
{
ob[i].show();
}
char a;
cout <<"\n\n 输入要删除的学生学号:" ;
cin>>x;
//通过for循环查找要删除学生的学号
for(i=0; i ";
cin>> a;
if (a=='y'|| a=='y')
{
cout <<" 删除成功!" <stulen; i++)
{
outfile <stulen ;
outfile1.close();
this->read ();
//_getch();
}
else
{
cout<<" 回到主界面...";
_getch();
}
}
}
}
//
void student::order()
{
int k,j;
float t; char n[20];
cout <<" 成绩排名:" <stulen; i++)
{
ob[i].show();
}
cout <<"\n\n 请输入要修改学生的学号:";
cin>>a;
for(i=0; i >b;
while(b!=1 &&b!=2 &&b!=3)
{
cout<<" 无效输入,请重新输入!\n";
cout<<"\n\n 请输入要修改的课程名的编号(1.数学, 2.英语,3.c++):";
cin>>b;
}
switch(b)
{
case 1:
{
float m;
cout<<" 数学(0-100): ";
cin>>m;
//如果输入成绩在数据域内,跳出循环并且赋值。
//如果不在数据域内,一直循环到输入数据符合数据域为止
while (m <0 || m>100)
{
cout<<" 数据有误!!重新输入.."<>m;
}
ob[p].math=m;break;
}
case 2:
{
float e;
cout<<" 英语(0-100): ";
cin>>e;
while (e <0 || e>100)
{
cout<<" 数据有误!!重新输入.."<>e;
}
ob[p].eng=e; break;
}
case 3:
{
float co;
cout<<" c++(0-100): ";
cin>>co;
while (co <0 || co>100)
{
cout<<" 数据有误!!重新输入.."<>co;
}
ob[p].comp=co;
}
}
ob[p].totll=ob[p].math+ob[p].eng+ob[p].comp;
ob[p].aver=(ob[p].math+ob[p].eng+ob[p].comp)/3;
ofstream outfile;
outfile.open("list.txt",ios::trunc);
for(i=0; i stulen; i++)
{
outfile <>this->stulen ;
infile1.close();
for(i=0;istulen ;i++)
{
infile>>ob[i].id;
ob[i].name =new char[strlen(b)+1];
infile>>b;
strcpy(ob[i].name,b);
infile>>ob[i].sex ;
infile>>ob[i].math ;
infile>>ob[i].eng;
infile>>ob[i].comp;
infile>>ob[i].totll;
infile>>ob[i].aver ;
}
infile.close ();
//学生数据显示格式
system("cls");
cout<<" 系统检测到以前输入过数据,数据内容如下:\n\n";
cout <stulen; i++)
{
ob[i].show();
}
cout<<"\n\n\n";
cout<<" 原始数据是否覆盖?(y/n)";
char q;
cin>>q;
while(q!='y'&& q!='y'&& q!='n'&& q!='n')
{
cout<<" 无效的命令,请重新输入..\n";
cout<<" 原始数据是否覆盖?(y/n)";
cin>>q;
}
if(q=='y'||q=='y')
{
for(int i=0;i <100;i++)
{
ob[i].math=ob[i].eng=ob[i].comp =ob[i].totll =ob[i].aver =0;
ob[i].id =0;
ob[i].sex =' ';
ob[i].name =null;
}
this->stulen =0;
ofstream outfile1;
outfile1.open("stulen.txt",ios::trunc);
outfile1<stulen ;
outfile1.close();
system("del list.txt");
}
}
}
//
void menu()
{
cout <<"\n\n";
cout <<"------------------ 学生成绩系统 -----------------" <>sel;
switch(sel)
{
case 1:
system("cls");
a.input();
cout <>y;
while(y!='y'&& y!='y'&& y!='n'&& y!='n')
{
cout<<" 无效的命令,请重新输入..\n";
cout<<" 是否保存? (y/n)";
cin>>y;
}
system("cls");
if(y=='y'||y=='y')
a.save();
else
a.clean();
break;
case 2:
system("cls"); a.read(); break;
case 3:
system("cls"); a.del(); break;
case 4:
system("cls"); a.add();break;
case 5:
system("cls"); a.query();break;
case 6:
system("cls"); a.modify();break;
case 7:
system("cls"); a.order();break;
case 8:
exit(0);
default:
cout <<" 无效的命令!";
_getch();
break;
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯