学生成绩管理系统 C语言编程

利用文件、结构体等实现成绩的录入、查询、修改、排序。

//用C++编的,基本能运行,可能有些冗余,你可以自己改一下

#include<iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include<iomanip>
using namespace std;

class student
{
public:
char *GET_name(){return name;}
char *GET_sex(){return sex;}
int GET_num(){return number;}
int GET_mat(){return Math;}
int GET_eng(){return English;}
protected:
int number,Math,English;
char name[20],sex[5];
};

class student_computer:public student
{
public:
void assign();
int GET_dat() {return datastruct;}
int GET_ele() {return electric;}
int GET_c() {return C;}
float GET_total() {return English+Math+C+datastruct+electric;}
void display();
protected:
int C,datastruct,electric;
float total;

};

typedef struct point_pc
{
student_computer *node;
point_pc *next;
}PT_pc;

void student_computer::display()
{
cout<<"学号:"<<setw(2)<<number<<" 姓名:"<<setw(4)<<name<<" 性别:"<<setw(2)<<sex<<"\n 数学:"<<setw(3)<<Math<<" 英语:"<<setw(3)<<English<<" C语言:"<<setw(3)<<C<<" 数据结构:"<<setw(3)<<datastruct<<" 电子电路:"<<setw(3)<<electric<<" 总分:"<<setw(4)<<GET_total()<<endl;

}

void student_computer::assign()
{
cout<<"输入姓名:";
getchar();
if(strcmp(gets(name),"#")!=0)
{
cout<<"学号:";
cin>>number;
cout<<"性别:";
getchar();
gets(sex);
cout<<"英语:";
cin>>English;
cout<<"数学:";
cin>>Math;
cout<<"C语言:";
cin>>C;
cout<<"数据结构:";
cin>>datastruct;
cout<<"电子电路:";
cin>>electric;

}
}

PT_pc *build_pc()
{int n=0;
student_computer *ppc=NULL;

PT_pc *head,*pm1,*pm2;
ppc=(student_computer *)malloc(sizeof(student_computer));
ppc->assign();
head=pm1=pm2=(PT_pc *)malloc(sizeof(PT_pc));
pm1->node=ppc;
cout<<"输入#为结束!"<<endl;
while(strcmp(ppc->GET_name(),"#"))
{if(n++!=0)pm2->next=pm1;
pm2=pm1;
ppc=(student_computer *)malloc(sizeof(student_computer));
ppc->assign();
pm1=(PT_pc*)malloc(sizeof(PT_pc));
pm1->node=ppc;
}
pm2->next=NULL;
if(strcmp(head->node->GET_name(),"#")==0)
head=NULL;
return head;
}

PT_pc *rank_pc(PT_pc *head)
{
PT_pc *p,*q,*temp;
temp=(PT_pc *)malloc(sizeof(PT_pc));
p=head;
for(;p->next!=NULL;p=p->next)
for(q=p->next;q!=NULL;q=q->next)
{
if((p->node->GET_total())<(q->node->GET_total()))
{
temp->node=p->node;
p->node=q->node;
q->node=temp->node;

}
}
return head;
}

PT_pc *add_pc(PT_pc *head)
{
PT_pc *pi;
pi=(PT_pc *)malloc(sizeof(PT_pc));
student_computer *p;
p=(student_computer *)malloc(sizeof(student_computer));
p->assign();
pi->node=p;
pi->next=head;
head=pi;
return head;
}

PT_pc *del_pc(PT_pc *head)
{
PT_pc *p_move=head;
int number;
cout<<"输入删除者学号:";
cin>>number;
if(head==NULL) {cout<<"without message to delete or rework!"<<endl; return head;}
if(head->node->GET_num()==number) {head=NULL;return head;}
while(p_move->next!=NULL)
{
if((p_move->next)->node->GET_num()==number) break;
p_move=p_move->next;
}
if(p_move->next==NULL) {cout<<"\n\n没有此信息!\n\n";return head;}
p_move->next=(p_move->next)->next;
return head;
}

PT_pc *rework_pc(PT_pc *head)
{
PT_pc *p_move;
student_computer *pi;
pi=(student_computer*)malloc(sizeof(student_computer));
int number;
cout<<"输入要修改者学号:"<<endl;
cin>>number;
p_move=head;
while(p_move!=NULL)
{
if(p_move->node->GET_num()==number)
{pi->assign(); break;}
else p_move=p_move->next;
}
if(p_move==NULL) {cout<<"没有此信息!"<<endl;}
else p_move->node=pi;
return head;
}

void find_pc(PT_pc *head)
{int number;
PT_pc *p_move;
cout<<"输入学号:";
cin>>number;
p_move=head;
while(p_move!=NULL)
{if(p_move->node->GET_num()==number)
{p_move->node->display();
break;
}
p_move=p_move->next;
}
if(p_move==NULL) cout<<"没有此信息!"<<endl;
}

void save_pc(PT_pc *head)
{
PT_pc *p;
p=head;
ofstream outfile("student_computer.txt",ios::binary);
if(!outfile)
{cerr<<"open file fail!"<<endl;
exit(1);
}
while(p!=NULL)
{outfile.write((char*)(p->node),sizeof(student_computer));
p=p->next;
}
outfile.close();
cout<<"保存成功!"<<endl;
}

PT_pc *read_pc()
{
int n=0, i=0;
student_computer *ppc;
PT_pc *head,*p_move1,*p_move2;
ppc=(student_computer *)malloc(sizeof(student_computer));
head=p_move1=p_move2=(PT_pc *)malloc(sizeof(PT_pc));
ifstream infile("student_computer.txt",ios::binary);
if(!infile)
{
cerr<<"打开文件失败!"<<endl;
getchar();

}
while(!infile.eof())
{if((i++)!=0) p_move2->next=p_move1;
p_move2=p_move1;
infile.read((char*)ppc,sizeof(student_computer));
p_move1->node=ppc;
ppc=(student_computer *)malloc(sizeof(student_computer));
p_move1=(PT_pc*)malloc(sizeof(PT_pc));
}
infile.close();
p_move1=NULL;
ppc=NULL;
p_move2->next=NULL;

{
i=0;
p_move1=p_move2=head;
while(p_move1->next!=NULL)
{if((i++)!=0) {p_move2->next=p_move1;p_move2=p_move1;}
p_move1=p_move1->next;
}
p_move2->next=NULL;
}
return head;
}

void display_pc(PT_pc *head)
{PT_pc *p=head;
int i=0;
while(p!=NULL)
{cout<<++i<<" ";
p->node->display();
p=p->next;
}
}

void main()
{char c='0',t='0';
PT_pc *headpc;

system("cls");
ifstream infile("student_computer.txt",ios::binary);
if(!infile)
{cerr<<"请建立新信息!"<<endl;
headpc=build_pc();
}
else headpc=read_pc();
while(t!='8')
{
while(t<'1'||t>'8')
{cout<<"请选择菜单:\n1.打印所有。\n2.添加部分数据.\n3.删除部分数据.\n4.修改数据.\n5.查找数据.\n6.成绩排名.\n7.建立新数据.\n8.退出.\n\n 选择:";
cin>>t;
}
switch(t)
{
case '1': system("cls"); display_pc(headpc); break;
case '2': system("cls"); headpc=add_pc(headpc); display_pc(headpc); break;
case '3': system("cls"); headpc=del_pc(headpc); display_pc(headpc); break;
case '4': system("cls"); headpc=rework_pc(headpc); display_pc(headpc); break;
case '5': system("cls"); find_pc(headpc); break;
case '6': system("cls"); headpc=rank_pc(headpc); display_pc(headpc); break;
case '7': system("cls"); headpc=build_pc(); break;

}
if(t!='8') t='0';
}

while(c!='y'&&c!='n')
{cout<<"是否保存修改后的信息(y or n):"<<endl;
cin>>c;
}
if(c=='y') save_pc(headpc);

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-03-29
啥意思?
第2个回答  2020-03-28
这是一点,别的我发到你邮箱
cout<<"请输入您要增加的学生的姓名:"<
>name;
cout<<"请输入数学成绩:"<
>math;
cout<<"请输入语文成绩:"<
>chinese;
cout<<"请输入外语成绩:"<
>english;
file[j]=new
ofstream("d:\\document",ios::ate);
*file[j]<<"姓名"<
>c;
if(c!='y'&&c!='n')
{
cout<<"指令错误!请重新输入!"<
评论
0
0
加载更多
相似回答