求怎么用C语言做一个学生成绩管理系统

2024-11-13 15:22:40
推荐回答(1个)
回答1:

去年写的,你稍微改下加个成绩就行

#include
#include
#include
FILE *fp;
struct student{
char name[10];
char number[12];
char age[3];
struct student *next;
};
struct student *creat(); //手动创建链表
struct student *build_list(); //从文件读取信息创建链表
void read_list(struct student *head); //打印链表
void search(struct student *head); //查找相应信息
struct student *delete_(struct student *head); //删除相应信息
void ad_list(struct student *head); //增加信息
int control(struct student *head); //控制函数
void head_print(); //打印界面函数
void change_list(struct student *head); //更改链表信息函数
void save(struct student *head); //保存文件
void remove(struct student *head); //递归释放链表空间
char name[10],number[12],age[3]; //学生姓名、学号、年龄全局变量
int main(){
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
control(head);
free(head);
fclose(fp);
return 0;
}
struct student *creat(){
struct student *head;
char name[10],number[12],age[3];
struct student *p,*r,*p1;
head=(struct student *)malloc(sizeof(struct student));
p=head->next;
r=head;
printf("依次输入姓名学号年龄(按q退出):\n");
scanf("%s",name);
while(name[0]!='q'){
p1=(struct student*)malloc(sizeof(struct student));
scanf("%s",number);
scanf("%s",age);
strcpy(p1->name,name);
strcpy(p1->number,number);
strcpy(p1->age,age);
p1->next=NULL;
r->next=p1;
r=p1;
scanf("%s",name);
}
printf("建立成功\n");

return head;
}
/*若无文件或文件为空,用链表创建函数,为防止占用多余内存在循环入口处进行判断,函数返回头指针地址*/
struct student *build_list(){
char ch;
struct student *head;
struct student *p1,*r;
rewind(fp);
head=(struct student*)malloc(sizeof(struct student));
r=head;
while(!feof(fp)){
p1=(struct student*)malloc(sizeof(struct student));
fscanf(fp,"%s%s%s",p1->name,p1->number,p1->age);
p1->next=NULL;
r->next=p1;
r=p1;
fseek(fp,2L,SEEK_CUR);
if((ch=getc(fp))==EOF)
break;
else
fseek(fp,-3L,SEEK_CUR);
}
return head;
}
/*文件内有信息时按行读取文件信息并创建链表,为防止误读结尾换行符,用fseek函数在每次循环后移两单位判断是否为空
,若为空结束循环,若不为空则用fseek函数前移三个单位*/
void read_list(struct student *head){
struct student *p;
p=head->next;
if(p==NULL){
head=creat();
}
printf("*******************************************\n");
while(p!=NULL){
printf("%-20s%-12s%-3s\n",p->name,p->number,p->age);
p=p->next;
}
printf("*******************************************\n");
}
/*输出链表信息,如果进行删除操作后链表为空则转到链表创建函数;按行读取文件信息并输出。*/
void search(struct student *head){
char s[20];
struct student *p;
int k=0;
printf("请输入要查询的字符串:\n");
p=head->next;
scanf("%s",s);
printf("查询结果:\n");
while(p!=NULL){
if((strcmp(s,p->age)&&strcmp(s,p->name)&&strcmp(s,p->number))==0){
printf("%-20s%-12s%-3s\n",p->name,p->number,p->age);
k=1;
}
p=p->next;
}
if(k==0)
printf("无记录\n");
}
/*查找函数,*/
struct student *delete_(struct student *head){
char s[20];
char ch;
int v=0;
struct student *p,*q;
printf("请输入要删除的学生的学号:\n");
scanf("%s",s);
p=head->next;
q=head;
while(p!=NULL){
if(strcmp(s,p->number)==0){
q->next=p->next;
v=1;
free(p);
break;
}
q=p;
p=p->next;
}
if(v==1)
printf("删除成功\n");
else{
printf("未删除成功\n");
return q->next;
}
if(head->next==NULL){
printf("已无信息,是否输入新的(Y/N):\n");
scanf("%c",&ch);
ch=getchar();
if(ch=='y')
head=creat();
return head;
}
return q->next;
}
void ad_list(struct student *head){
char s[20];
int v=0;
struct student *p,*q;
q=(struct student *)malloc(sizeof(struct student));
p=head->next;
printf("插在学号为多少的学生后?\n");
scanf("%s",s);
while(p!=NULL){
if(strcmp(s,p->number)==0){
printf("请输入学生信息(姓名 学号 年龄):\n");
scanf("%s%s%s",q->name,q->number,q->age);
q->next=p->next;
p->next=q;
v=1;
break;
}
p=p->next;
}
if(v==0)
printf("插入失败\n");
}
void change_list(struct student *head){
char s1[12];
char ch;
struct student *p;
printf("请输入要更改的学生的学号:\n");
scanf("%s",s1);
p=head->next;
while(p!=NULL){
if(strcmp(s1,p->number)==0)
break;
p=p->next;
}
printf("请输入要更改的内容:\na.姓名\nb.学号\nc.年龄\n");
scanf("%c",&ch);
ch=getchar();
printf("请输入更改后结果:\n");
scanf("%s",s1);
switch(ch){
case'a':strcpy(p->name,s1);break;
case'b':strcpy(p->number,s1);break;
case'c':strcpy(p->age,s1);break;
}
}
void save(struct student *head){
struct student *p;
p=head->next;
fp=fopen("stu.out","w+");
rewind(fp);
while(p!=NULL){
fprintf(fp,"%-20s%-12s%3s\n",p->name,p->number,p->age);
p=p->next;
}
printf("保存成功\n");
}
void remove(struct student *head){
if(head==NULL)
return;
remove(head->next);
free(head);
}
int control(struct student *head){
int i=0;
char ch;
head_print();
if((fp=fopen("stu.out","r"))==NULL){
printf("无文件,请先建立:\n");
head=creat();
}
else if((ch=getc(fp))==EOF){
printf("无信息,请先建立:\n");
head=creat();
}
else
head=build_list();
while((ch=getchar())!='q'){
switch(ch){
case'a':search(head);break;
case'b':delete_(head);break;
case'c':ad_list(head);break;
case'd':change_list(head);break;
case'e':read_list(head);break;
case'f':save(head);break;
case'h':head_print();
}
}
if(ch=='q')
remove(head);
return 0;
}
void head_print(){
printf("%13.0s********************************************%13.0s\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0sa.查找%9.0sb.删除%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0sc.增加%9.0sd.更改%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0se.查看%9.0sf.存档%10.0s**\n");
printf("%13.0s**%40.0s**\n");
printf("%13.0s**%9.0s按h显示提示信息,q退出%10.0s**\n");
printf("%13.0s********************************************%13.0s\n");
}