每个结点包含学号、姓名、3门考试成绩(英语、数学、计算机)。
要求利用菜单的形式进行管理
菜单为:
1.排序(按学号);
2.插入(输入一个学生的信息将它插入链表中,假定链表按学号有序);
3.查找(输入一个学生学号,输出其名科成绩);
4.删除(从链表中按输入的学号删除该学生);
5.统计
若按1,则输入学生的学号统计该生的总分及平均分
若按2,则输入课程求该门课程的总平均分。
6.存盘
将建立起来的链表以文件的形式存盘
7.读入
将原来已经存盘的文件读入内存,进行管理。
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void input();
void output();
void insertone();
void dele();
void count();
void failure();
void search();
void insert();
struct student *head='\0';
long in_num;int w;
char in_name[20];
int in_score[3];
struct student
{
long num;
char name[20];
int score[3];
int sum;
int average;
struct student* link;
};
void main()
{
int ch;
clrscr();
do
{
printf("*********** Student score the management system *****************\n");
printf("*** Welcome you use the system regulate student' score ***\n");
printf(" \n");
printf("* * ------------ * ------------ * * \n");
printf(" \n");
printf(" \n");
printf("\n---Operation(0 to 7)---\n (1)Input\n (2)Output\n (3)Insert\n");
printf(" (4)Delete\n (5)Sum and Average\n (6)Failure\n (7)Search\n (0)Exit\n");
printf(" \n");
printf(" \n");
printf("* * ------------ * ------------ * * \n");
printf(" department :xiepan\n");
printf(" ----builded 2004.6.15\n");
printf("Enter operation(0--7):");
scanf("%d",&ch);
switch(ch)
{
case 0:
break;
case 1:
input();
break;
case 2:
output();
break;
case 3:
insertone();
break;
case 4:
dele();
break;
case 5:
count();
break;
case 6:
failure();
break;
case 7:
search();
break;
default:
break;
}
}while(ch!=0);
}
void input()
{
int n,i;
printf("How many students do you want to input?:");
scanf ("%d",&n);
for(i=1;i<=n;i++)
{
printf("ID :");
scanf("%ld",&in_num);
printf("Name:");
scanf("%s",in_name);
for(w=0;w<=2;w++)
{
printf("%s %d %s","Score",w+1,":");
scanf("%d",&in_score[w]);
if(in_score[w]<0||in_score[w]>100)
{
printf("Wrong Score(0--100)!Again\n");
w--;
}
}
insert();
}
}
void output()
{
struct student* disp=head;
if(disp=='\0')
{
printf("\nNo data!\n");
}
printf("\n--ID-------Name-------Score\n");
while(disp!='\0')
{
printf("%-10ld%-15s%-4d%-4d%-4d\n",disp->num,disp->name,disp->score[0],disp->score[1],disp->score[2]);
disp=disp->link;
}
}
void insertone()
{
printf("\nID insert:");
scanf("%ld",&in_num);
printf("Name insert:");
scanf("%s",in_name);
for(w=0;w<=2;w++)
{
printf("%s %d %s","Score insert",w+1,":");
scanf("%d",&in_score[w]);
}
insert();
}
void dele()
{
struct student *previous;
struct student *current;
printf("\nID your want to delete:");
scanf("%ld",&in_num);
previous ='\0';
current=head;
while(current!='\0')
{
if(current->num==in_num)
{
break;
}
previous=current; current=current->link;
}
if(current=='\0')
{
printf("\nNo found!\n");
return;
}
if(previous=='\0')
{
head=current->link;
}
else
{
previous->link=current->link;
}
free(current);
}
void count()
{
struct student *head2='\0';
struct student *previous2;
struct student *current2;
struct student *newnode2;
struct student *temp;
int t=1;
temp=head;
if(temp=='\0')
{
printf("\nNo data!\n");
return;
}
while(temp!='\0')
{
previous2='\0'; current2=head2;
while(current2!='\0')
{
if(current2->sum>temp->sum)
{
break;
}
previous2=current2;
current2=current2->link;
}
newnode2=(struct student*)malloc(sizeof(struct student));
newnode2->num=temp->num;
strcpy(newnode2->name,temp->name);
newnode2->sum=temp->sum;
newnode2->average=temp->average;
newnode2->link=current2;
if(previous2=='\0')
{
head2=newnode2;
}
else
{
previous2->link=newnode2;
}
temp=temp->link;
}
printf("----------Score Board----------\n");
while(head2!='\0')
{
printf("%-3d%-10ld%-15s%-5d%-5d\n",t,head2->num,head2->name,head2->sum,head2
->average);
head2=head2->link;
t++;
}
}
void failure()
{
struct student* fail;
fail=head;
if(fail=='\0')
{
printf("\nNo data!\n");
}
while(fail!='\0')
{
for(w=0;w<=2;w++)
{
if(fail->score[w]<60)
{
printf("\n-------Fail to pass-------\n");
printf("%-10ld%-15s%-4d%-4d%-4d\n",fail->num,fail->name,fail->score[0],fail->score[1],fail->score[2]);
break;
}
}
fail=fail->link;
}
}
void search()
{
struct student* current;
int cho;
current=head;
printf("\nSearch by: (1)Name (2)ID :");
scanf("%d",&cho);
switch(cho)
{
case 1:
printf("Name:");
scanf("%s",in_name);
while(current!='\0')
{
if(strcmp(current->name,in_name)==0)
{
break;
}
current=current->link;
}
break;
case 2:
printf("ID:");
scanf("%ld",&in_num);
while(current!='\0')
{
if(current->num==in_num)
{
break;
}
current=current->link;
}
break;
default:
printf("\nChoose (1) or (2)!\n");
return;
}
if(current=='\0')
{
printf("\nNo data!\n");
}
else
{
printf("\n--ID-------Name-------Score");
printf("\n%-10ld%-15s%-4d%-4d%-4d\n",current->num,current->name,current->score[0],current->score[1],current->score[2]);
}
}
void insert()
{
struct student* previous;
struct student* current;
struct student* newnode;
previous='\0'; current=head;
while(current!='\0')
{
if(current->num>in_num)
{
break;
}
previous=current;
current=current->link;
}
newnode=(struct student*)malloc(sizeof(struct student));
newnode->num=in_num;
strcpy(newnode->name,in_name);
for(w=0,newnode->sum=0;w<=2;w++)
{
newnode->score[w]=in_score[w];
newnode->sum+=in_score[w];
}
newnode->average=newnode->sum/3;
newnode->link=current;
if(previous=='\0')
{
head=newnode;
}
else
{
previous->link=newnode;
}
}
//学生系统(引用&附加头结点) //预定义 #include<iostream.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<malloc.h> #include<string.h> typedef struct student{ long num;//学号 char name[20];//姓名 float English,Math,Computer;//各科成绩 struct student *next;//指针域 }Node; int n; //基本操作 Node *creat(void){ //创建学生信息链表 Node *head; Node *p1,*p2; n=0; p1=p2 =(Node *)malloc(sizeof(Node)); scanf("%ld%s%f%f%f",&p1->num,p1->name,&p1->English,&p1->Math,&p1->Computer); head=NULL; while(p1->num!=0){ n=n+1; if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(Node*)malloc(sizeof(Node)); scanf("%ld%10s%f%f%f",&p1->num,p1->name,&p1->English,&p1->Math,&p1->Computer); } p2->next=NULL; return(head); } void print(Node *head){ //输出学生信息 Node *p; printf("\nNOW,These %d recordes are:\n",n); printf("num\t\tname\tEnglish\tMath\tComputer"); p=head; if(head!=NULL) while(p!=NULL){ printf("\n%ld\t%10s\t%f\t%f\t%f\n",p->num,p->name,p->English,p->Math,p->Computer); p=p->next; } } /* Bubble(Node *head){ Node *p1,*p2,*p0; p1=head; for(int i=1;i<n;i++) for(int j=n-1;j>=i;j--) if(p1->num>p1->next->num) p2=p1->next,p1->next=p1->next->next,p2->next=p1,head=p2; }*/ void Get(Node *p,long num){ //查找并输出学生信息 while(p&&p->num!=num)p=p->next; if(n<1||!p){printf("Do not find %ld",num);return ;} else printf("\n%ld\t%10s\t%f\t%f\t%f\n",p->num,p->name,p->English,p->Math,p->Computer); } Node *insert(Node *head,Node *stud){ //插入学生信息 Node *p0,*p1,*p2; p1=head; p0=stud; if(head==NULL) {head=p0;p0->next=NULL;} else {while((p0->num>p1->num)&&(p1->next!=NULL)) {p2=p1; p1=p1->next;} if(p0->num<=p1->num) {if(head==p1)head=p0; else p2->next=p0; p0->next=p1;} else{p1->next=p0;p0->next=NULL;} } n=n+1; return(head); } Node *del(Node *head,long num){ //删除学生信息 Node *p1,*p2; p1=head; while(num!=p1->num&&p1->next!=NULL){p2=p1;p1=p1->next;} if(num==p1->num) {if(p1==head)head=p1->next; else p2->next=p1->next; printf("delete:%ld\n",num); n=n-1; } else printf("%ld not been found!\n",num); return(head); } void sum(Node *p,long num){ //求学生总分和平均分 while(p&&p->num!=num)p=p->next; if(!p){printf("Have no this student:");return ;} printf("\n%ld\t%10s\t%f\t%f\t%f\n",p->num,p->name,p->English,p->Math,p->Computer); printf("\nThis student total cent is:%f\n",(*p).English+(*p).Math+(*p).Computer); printf("\nThis student average cent is:%f\n",(p->English+p->Math+p->Computer)/3);
} void Save(Node *head){ //存盘 FILE *fp; Node *p; if((fp=fopen("stu_list","wb"))==NULL){ printf("Cannot open file\n"); return; } for(p=head;p;p=p->next) if(fwrite(p,sizeof(Node),1,fp)!=1) printf("file write error\n"); fclose(fp); } //主函数 void main(){ clrscr();//清屏 FILE *fp; Node *head,stu,*p; long del_num,Get_num,stu_num; printf("Pleade input records(With 5 zero end):\n"); head=creat(); print(head); int choose=0; while(1){ cout<<"\n\n1.Taxis:\n"; cout<<"2.Insert:\n"; cout<<"3.Sort:\n"; cout<<"4.Del:\n"; cout<<"5.Statistics:\n"; cout<<"6.Save:\n"; cout<<"7.Read into the memory:\n"; //…… cout<<"9.End\n\n"; cout<<"Please choose:"; cin>>choose; switch(choose){ case 1: /*printf("\n排序前为:\n"); print(head); printf("\n排序后为:"); Bubble(head); print(head); break;*/ case 2: printf("\ninput the inserted record:\n"); scanf("%ld%s%f%f%f",&stu.num,&stu.name,&stu.English,&stu.Math,&stu.Computer); head=insert(head,&stu); print(head); break; case 3: printf("input the sort number:\n"); scanf("%ld",&Get_num); Get(head,Get_num); break; case 4: printf("\ninput the deleted number:"); scanf("%ld",&del_num); head=del(head,del_num); print(head); break; case 5: int choose1=0; while(1){ cout<<"\n\n(1).Input the student's registration number covariance shouldliving total cent and even divide equally:\n"; //输入学生的学号统计该生的总分及平均分 cout<<"(2).Input the course Beg that course total and evento divide equally:\n"; //输入课程求该门课程的总平均分 cout<<"(3).End\n\n"; cout<<"please choose:"; cin>>choose1; switch(choose1){ case 1: printf("\ninput the student number:"); scanf("%ld",&stu_num); sum(head,stu_num); break; case 2: char str[20]; Node *p; p=head; float s=0; printf("\ninput the course:"); scanf("%s",str); if(strcmp(str,"English")==0) for(;p;p=p->next) s+=p->English; else if(strcmp(str,"Math")==0) for(;p;p=p->next) s+=p->Math; else if(strcmp(str,"Computer")==0) for(;p;p=p->next) s+=p->Computer; else/*(strcmp(str,"English")!=0||strcmp(str,"Math")!=0||strcmp(str,"Computer")!=0)*/{printf("There is not this course");} printf("Averages is :%f",s/n); break; case 3:
clrscr(); cout<<"\n\n\n\n\n\n\n\n\t\t\t\t\tPlease continue!";getch(); //请继续 break; } break; } break; case 6: Save(head); fp=fopen("stu_list","rb"); printf("\nYou save the dish student's information is:\n"); for(p=head;p;p=p->next){ fread(p,sizeof(Node),1,fp); printf("\n%ld\t%10s\t%f\t%f\t%f\n",p->num,p->name,p->English,p->Math,p->Computer); } fclose(fp); break; //…… case 9: clrscr(); cout<<"\n\n\n\n\n\n\n\n\t\t\t\t\tBye-Bye!"; getch(); return; } cout<<"\n\nArbitrarily the key continue……"; getch(); } }