#include
#include
#include
struct Link/*定义结构体链表*/
{
int number;
char name[20];
char sex[4];
int chinese;
int math;
int lizong;
int english;
int sum;
float average;
struct Link*next;
};
void Picture(void);/*进行函数调用*/
char Menu1(void);
char Menu2(void);
struct Link *Append(struct Link *head);
void Print(struct Link*head);
struct Link *Delete(struct Link *head);
struct Link *Change(struct Link *head);
void Find(struct Link*head);
void Sort1(struct Link*head);
void Sort2(struct Link*head);
void Sort3(struct Link*head);
void Sort4(struct Link*head);
void Deletememory(struct Link*head);
void Beifen(struct Link*head);
struct Link*Huanyuan(struct Link*head);
int Total(struct Link*head);
const char *file="wenjian";
char *Mima(void);
int main()
{
char choice,ch,*shuru,mima[7]="123456";
struct Link *head=NULL;
system("color 4f");
Picture();
printf("登陆身份认证:\n你是一位老师还是一名学生?(Teacher/Student)\n");
scanf("%c",&ch);
if(ch=='T'||ch=='t')
{
do
{ printf("请输入6位密码:\n");
scanf("%s",shuru);
if(strcmp(shuru,mima)==0)
break;
else {printf("输入密码不正确\n");}
}while(strcmp(shuru,mima)!=0);
while(1)
{
system("cls");
choice=Menu1();
switch(choice)
{
case '1':
head=Append(head);
system("cls");
break;
case '2':
Print(head);
break;
case '3':
head=Delete(head);
break;
case '4':system("cls");
head=Change(head);
printf("想要查看吗?(Y/N)");
scanf(" %c",&ch);
if(ch=='Y'||ch=='y')
Print(head);
break;
case '5':Find(head);
break;
case '6':Sort1(head);
printf("已经排好顺序了\n");
break;
case '7':Sort2(head);
printf("已经排好顺序了\n");
break;
case '8':Sort3(head);
printf("已经排好顺序了\n");
break;
case '9':Sort4(head);
printf("已经排好顺序了\n");
break;
case 'A':Beifen(head);
exit(0);
case 'B':head=Huanyuan(head);
char i=getchar();
printf("您已经成功还原,按ENTER键返回主菜单\n");
i=getchar();
break;
case 'C':Deletememory(head);
exit(0);
default:printf("input error!");
break;
}
}
}
else
{
while(1)
{
system("cls");
choice=Menu2();
switch(choice)
{
case '1':
Print(head);
break;
case '2':Find(head);
break;
case '3':Sort1(head);
char i=getchar();
printf("已经排好顺序了,按ENTER键返回主菜单\n");
i=getchar();
break;
case '4':Sort2(head);
i=getchar();
printf("已经排好顺序了,按ENTER键返回主菜单\n");
i=getchar();
break;
case '5':Sort3(head);
i=getchar();
printf("已经排好顺序了,按ENTER键返回主菜单\n");
i=getchar();
break;
case '6':Sort4(head);
i=getchar();
printf("已经排好顺序了,按ENTER键返回主菜单\n");
i=getchar();
Print(head);
break;
case '7':head=Huanyuan(head);
i=getchar();
printf("您已经成功还原,按ENTER键返回主菜单\n");
i=getchar();
break;
case '8':Deletememory(head);
exit(0);
default:printf("input error!");
break;
}
}
}
}
char Menu1(void)/*教师菜单*/
{
char ch;
printf("%5\t教师菜单\n");
printf("创名校 做名师 育名人\n");
printf("%5\t1、增添\n");
printf("%5\t2、列表显示\n");
printf("%5\t3、删除\n");
printf("%5\t4、修改\n");
printf("%5\t5、查询\n");
printf("%5\t6、以总分降序排列\n");
printf("%5\t7、以总分升序排列\n");
printf("%5\t8、以学号降序排列\n");
printf("%5\t9、以学号升序排列\n");
printf("%5\tA、备份文件\n");
printf("%5\tB、还原文件\n");
printf("%5\tC、退出\n");
printf("please enter your choice:\n");
scanf(" %c",&ch);
return ch;
}
char Menu2(void)/*学生菜单*/
{
char ch;
printf("%5\t学生菜单\n");
printf("规格严格 功夫到家\n");
printf("%5\t1、列表显示\n");
printf("%5\t2、查询\n");
printf("%5\t3、以总分降序排列\n");
printf("%5\t4、以总分升序排列\n");
printf("%5\t5、以学号降序排列\n");
printf("%5\t6、以学号升序排列\n");
printf("%5\t7、还原文件\n");
printf("%5\t8、退出\n");
printf("please enter your choice:\n");
scanf(" %c",&ch);
return ch;
}
struct Link *Append(struct Link *head)/*增添的函数*/
{
char c;
do{
system("cls");
system("color 1f");
struct Link *p=NULL;
struct Link *pr=head;
p=(struct Link*)malloc(sizeof(struct Link));
if(p==NULL)
{
printf("没有足够内存,返回主菜单\n");
break;
}
if(head==NULL)
{
head=p;
}
else
{
while(pr->next!=NULL)
{
pr=pr->next;
}
pr->next=p;
}
pr=p;
printf("请输入学号\n");
scanf("%d",&p->number);
printf("请输入姓名\n");
scanf("%s",p->name);
printf("请输入性别\n");
scanf("%s",p->sex);
printf("请输入语文成绩(0——150)\n");
do{
scanf("%d",&p->chinese);
if((p->chinese)<0||(p->chinese)>150)
printf("输入分数有误,请重新输入\n");
}while((p->chinese)<0||(p->chinese)>150);
printf("请输入数学(0——150)\n");
scanf("%d",&p->math);
printf("请输入理科综合成绩(0——300)\n");
scanf("%d",&p->lizong);
printf("请输入英语成绩(0——150)\n");
scanf("%d",&p->english);
p->sum=(p->chinese)+(p->math)+(p->lizong)+(p->english);
p->average=(float)(p->sum)/4;
pr->next=NULL;
printf("\nnew nodes have been append!\n");
printf("\n是否想新增加一个新成员?(Y/N)\n");
scanf(" %c",&c);
}
while(c=='Y'||c=='y');
return head;
}
void Print(struct Link*head)/*列表显示的函数*/
{
if(head==NULL)
{
char i=getchar();
printf("无内容,无法显示,按ENTER键返回菜单\n");
i=getchar();
system("cls");
}
else{
printf("学号 姓名 性别 语文 数学 理科综合 英语 总分 平均分\n");
struct Link*p=head;
while(p!=NULL)
{
printf("\n%d %s %s %d",p->number,p->name,p->sex,p->chinese);
printf(" %d %d %d %d %.2f",p->math,p->lizong,p->english,p->sum,p->average);
p=p->next;
}
char i=getchar();
printf("\n按ENTER键返回主菜单\n");
i=getchar();
system("cls");
}
}
struct Link *Delete(struct Link *head)/*删除的函数*/
{
if(head==NULL)
{
char i=getchar();
printf("链表为空,没有要删除对象,按ENTER键返回主菜单\n");
i=getchar();
system("cls");
}
else{
int c;
char ch;
do{
if(head==NULL)
{
char i=getchar();
printf("链表为空,没有要删除对象,按ENTER键返回主菜单\n");
i=getchar();
system("cls");
break;
}
printf("请输入要删除的学号\n");
scanf("%d",&c);
struct Link *p=head,*pr=head;
while(c!=p->number&&p->next!=NULL)
{
pr=p;
p=p->next;
}
if(c==p->number)
{
if(p==head)
{
head=p->next;
}
else
{
pr->next=p->next;
}
free(p);
printf("您已经成功删除");
}
else printf("没有找到!");
printf("\n想继续吗?(Y/N)");
scanf(" %c",&ch);
}while(ch=='Y'||ch=='y');
system("cls");
}
return head;
}
struct Link *Change(struct Link *head)/*修改的函数*/
{
int c;
char ch;
do{
if(head==NULL)
{
printf("链表为空,没有要删除对象\n");
break;
}
printf("请输入要修改的学号\n");
scanf("%d",&c);
struct Link *p=head;
while(c!=p->number&&p!=NULL)
{
p=p->next;
}
if(c==p->number)
{
printf("请输入改后学号\n");
scanf("%d",&p->number);
printf("请输入改后姓名\n");
scanf("%s",p->name);
printf("请输入改后性别\n");
scanf("%s",p->sex);
printf("请输入改后语文成绩\n");
scanf("%d",&p->chinese);
printf("请输入改后数学\n");
scanf("%d",&p->math);
printf("请输入改后理科综合成绩\n");
scanf("%d",&p->lizong);
printf("请输入改后英语成绩\n");
scanf("%d",&p->english);
p->sum=(p->chinese)+(p->math)+(p->lizong)+(p->english);
p->average=(float)(p->sum)/4;
printf("您已经成功修改\n");
}
else printf("没找到!\n");
printf("想继续修改吗?(Y/N)");
scanf(" %c",&ch);
}while(ch=='Y'||ch=='y');
return head;
}
void Find(struct Link*head)/*查找的函数*/
{
int c;
char ch;
struct Link*p=head;
do{
printf("请输入想要查询的学号:\n");
scanf("%d",&c);
if(head==NULL)
{
printf("链表为空,没有查询对象\n");
break;
}
while(c!=p->number&&p!=NULL)
{
p=p->next;
}
if(c==p->number)
{
printf("学号 姓名 性别 语文 数学 理科综合 英语 总分 平均分\n");
printf("\n%d %s %s %d",p->number,p->name,p->sex,p->chinese);
printf(" %d %d %d %d %.2f",p->math,p->lizong,p->english,p->sum,p->average);
}
else printf("您的输入有误,暂时没有此学号的学生,请查证后再查询\n");
printf("想继续查询吗?(Y/N)\n");
scanf(" %c",&ch);
}while(ch=='Y'||ch=='y');
}
void Picture(void)/*超豪华界面,后来发现可以比这更简单,但已经做完了,就没有修改*/
{
printf ("hello world");
}
void Sort1(struct Link*head)/*排序函数1*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("链表为空,按ENTER键返回菜单\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->sum)<(p->next->sum))/*只交换节点内容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Sort2(struct Link*head)/*排序函数2*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("链表为空,按ENTER键返回菜单\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->sum)>(p->next->sum))/*只交换节点内容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Sort3(struct Link*head)/*排序函数3*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("链表为空,按ENTER键返回菜单\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->number)<(p->next->number))/*只交换节点内容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Sort4(struct Link*head)/*排序函数4*/
{
struct Link*p;
int flag=0;
char temp1[20];
int temp2;
do
{
if(head==NULL)
{
char i=getchar();
printf("链表为空,按ENTER键返回菜单\n");
i=getchar();
break;
}
flag=0;
p=head;
while(p->next!=NULL)
{
if((p->number)>(p->next->number))/*只交换节点内容*/
{
temp2=(p->next->sum);//sum
(p->next->sum)=(p->sum);
(p->sum)=temp2;
strcpy(temp1,(p->next->name));//name
strcpy((p->next->name),(p->name));
strcpy((p->name),temp1);
strcpy(temp1,(p->next->sex));//sex
strcpy((p->next->sex),(p->sex));
strcpy((p->sex),temp1);
temp2=(p->next->chinese);
(p->next->chinese)=(p->chinese);
(p->chinese)=temp2;
temp2=(p->next->math);
(p->next->math)=(p->math);
(p->math)=temp2;
temp2=(p->next->lizong);
(p->next->lizong)=(p->lizong);
(p->lizong)=temp2;
temp2=(p->next->english);
(p->next->english)=(p->english);
(p->english)=temp2;
temp2=(p->next->average);
(p->next->average)=(p->average);
(p->average)=temp2;
flag=1;
}
p=p->next;
}
}while(flag);
}
void Deletememory(struct Link *head)/*清楚内存*/
{
struct Link*p=head,*pr=NULL;
while(p!=NULL)
{
pr=p;
p=p->next;
free(pr);
}
}
void Beifen(struct Link*head)/*仿照数据库的思路,用备份和还原的方法解决文件问题*/
{
FILE*fp;
struct Link*p=head;
int x=0;
fp=fopen(file,"w+");
if(fp==NULL)
{
printf("Can't open %s file\n",file);
exit(1);
}
x=Total(head);
fputc(x,fp);
while(p!=NULL)
{
fwrite(p,sizeof(struct Link),1,fp);
p=p->next;
}
printf("您已经成功备份,按ENTER键退出系统\n");
fclose(fp);
}
struct Link*Huanyuan(struct Link*head)/*还原文件*/
{
int m=1,n;
FILE*fp;
fp=fopen(file,"r+");
if(fp==NULL)
{
printf("Can't open %s file\n",file);
//exit(1);
}
struct Link*p=NULL;
struct Link*pr=NULL;
n=fgetc(fp);
pr=(struct Link*)malloc(sizeof(struct Link));
fread(pr,sizeof(struct Link),1,fp);
head=pr;
while(m<=n)
{
p=(struct Link*)malloc(sizeof(struct Link));
fread(p,sizeof(struct Link),1,fp);
pr->next=p;
pr=p;
pr->next=NULL;
m=m+1;
}
return head;
}
int Total(struct Link*head)/*计算输入的学生总数,在还原函数中要用到*/
{
struct Link*p=head;
int n=-1;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
同学你好:
你这个问题其实完成起来不是很困难,关键是自己想不想去做。同学你应该学完了C了吧?用你所学的知识肯定可以解决。如果你是上网来COPY完成作业的话,还不如抄你同学的或者是和你同学一起做。
给你一个思路。这个系统的代码要大量用到文件,如果你对文件还不是很熟悉的话,很困难。数据的读取和写入都要用到文件。
结构体可以这样定义
struct student
{
int id;
char name[10];
char sex;
int score_1;
int score_2;
int score_3;
};
或:
struct student
{
int id;
char name[10];
char sex;
int score[3];
};
界面的话比较容易,要有详细的提示。
*******************************
*** 学生成绩管理系统 ***
*******************************
*** 查询请输入:L ***
*** 排序请输入:S ***
*** 删除请输入:D ***
*** 退出请输入:E ***
*******************************
这些功能模块用函数实现,例如:
void sort( ......) // 排序函数
{
................
}
流程图可以等程序写完后再写出来,当然也可以先写。
思路要明确,考虑要周全。注意函数的传参,这很容易出错。
同学你还是自己去做比较好,世上无难事只怕有心人,我相信你肯定能做出来。加油了。
我用的是链表
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"process.h"
/*单链表结构*/
typedef struct LNote {
char num[15];
char name[10];
int score;
struct LNote *next;
}LNote,*List;
/*初始化*/
List Start()
{
List L;
L=(List)malloc(sizeof(LNote));
L->next=NULL;
return L;
}
/*插入并排序*/
void Insert(List L,char num[],char name[],int score)
{
List pre,p,s;
p=L;
s=(List)malloc(sizeof(LNote));
strcpy(s->num,num);
strcpy(s->name,name);
s->score=score;
do {
pre=p;
p=p->next;
} while(p!=NULL&&s->score
s->next=pre->next;
pre->next=s;
printf("\n\t\t\t\t插入成功");
}
/*删除*/
void Delete(List L,char num[])
{
List pre,p;
p=L;
if(p->next==NULL) {
printf("\n\t\t\t\t记录为空");
goto end;
}
while(p!=NULL&&strcmp(p->num,num)!=0) {
pre=p;
p=p->next;
}
if(p==NULL) {
printf("\n\t\t\t\t没有该学生的数据");
goto end;
}
if(strcmp(p->num,num)==0) {
pre->next=p->next;
free(p);
printf("\n\t\t\t\t删除成功");
}
end:
printf("\n");
}
/*遍历*/
void Traverse(List L)
{
List p;
p=L->next;
if(p==NULL)
printf("\n\t\t\t\t没有记录");
else {
printf("\n\t\t学号\t\t姓名\t成绩\n");
while(p!=NULL) {
printf("\t\t%-15s%-10s%-5d\n",p->num,p->name,p->score);
p=p->next;
}
}
}
/*查找*/
void Find(List L,char num[])
{
List p;
p=L;
while(p!=NULL&&strcmp(p->num,num)!=0)
p=p->next;
if(p==NULL)
printf("\t\t\t\t没有找到");
else {
printf("\n\t\t学号\t\t姓名\t成绩\n");
printf("\t\t%-15s%-10s%-5d",p->num,p->name,p->score);
}
}
/*保存*/
void Save(List L)
{
FILE *fp;
List p;
p=L->next;
if((fp=fopen("学生管理.txt","w"))==NULL) {
printf("文件打开失败\n");
exit(1);
}
while(p!=NULL) {
fprintf(fp,"%-15s%-10s%-4d\n",p->num,p->name,p->score);
p=p->next;
}
printf("\n\t\t\t\t保存成功");
}
/*统计*/
void Tegether(List L)
{
List p,q;
float a=0,b=0,c=0,length=0;
q=L->next;
while(q!=NULL) {
length++;
q=q->next;
}
if(length==0)
printf("\n\t\t\t\t记录为空\n");
else {
p=L->next;
while(p!=NULL) {
if(p->score>=80&&p->score<=100)
a++;
if(p->score>=60&&p->score<80)
b++;
if(p->score>=0&&p->score<60)
c++;
p=p->next;
}
printf("\n\t优秀%.0f人数\t及格%.0f人\t\t不及格%.0f人\n",a,b,c);
printf("\t优秀率%-3.2f\t及格率%-3.2f\t不及格率%-3.2f",a/length,b/length,c/length);
}
}
/*读取文件*/
void Read(List L)
{
FILE *fp;
char num[15];
char name[10];
int score;
List s,pre;
if((fp=fopen("学生管理.txt","r+"))==NULL)
printf("文件打开失败");
printf("%s\t\t%s\t%s\n","学号","姓名","成绩");
pre=L;
while(!feof(fp)) {
fscanf(fp,"%s%s%d\n",num,name,&score);
printf("%-15s%-10s%-4d\n",num,name,score);
s=(List)malloc(sizeof(LNote));
strcpy(s->num,num);
strcpy(s->name,name);
s->score=score;
pre->next=s;
pre=s;
}
s->next=NULL;
if(fclose(fp))
printf("文件关闭失败");
}
int scan()
{
int d,c=2;
printf("\t\t\t\t%c 1.初始化 %c\n\n",c,c);
printf("\t\t\t\t%c 2.输入数据 %c\n\n",c,c);
printf("\t\t\t\t%c 3.删除数据 %c\n\n",c,c);
printf("\t\t\t\t%c 4.遍历 %c\n\n",c,c);
printf("\t\t\t\t%c 5.保存为数据文件 %c\n\n",c,c);
printf("\t\t\t\t%c 6.查找 %c\n\n",c,c);
printf("\t\t\t\t%c 7.统计 %c\n\n",c,c);
printf("\t\t\t\t%c 8.读取数据文件 %c\n\n",c,c);
printf("\t\t\t\t%c 9.退出 %c\n\n",c,c);
do {
printf("\t\t\t\t 输入操作:");
scanf("%d",&d);
if(d<=0||d>9)
printf("\n\t\t\t\t无此操作\n\n");
} while(d<0||d>9);
return d;
}
main()
{
int quit=0,score;
char name[10],num[15];
List L;
L=Start();
printf("\t\t\t\t***** 欢迎使用 *****\n\n");
while(!quit) {
switch(scan()) {
case 1:
Start();
printf("\n\n");
break;
case 2:
printf("\n\t\t\t\t学号:");
scanf("%s",num);
printf("\n\t\t\t\t姓名:");
scanf("%s",name);
printf("\n\t\t\t\t分数:");
scanf("%d",&score);
Insert(L,num,name,score);
printf("\n\n");
break;
case 3:
printf("\n\t\t\t\t输入学号:");
scanf("%s",num);
Delete(L,num);
printf("\n");
break;
case 4:
Traverse(L);
printf("\n\n");
break;
case 5:
Save(L);
printf("\n\n");
break;
case 6:
printf("\n\t\t\t\t输入学号:");
scanf("%s",num);
Find(L,num);
printf("\n\n");
break;
case 7:
Tegether(L);
printf("\n\n");
break;
case 8:
Read(L);
printf("\n\n");
break;
case 9:
quit=1;
break;
}
}
}