📄 ke.c
字号:
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{char num[15];
char name[10];
char tel[30];
char birth[30];
char sushe[30];
char address[30];
int m;
int math;
int English;
int physics;
struct student *next;
};
int n; /*定义全局变量n*/
struct student *creat(void) /*建立学生数据的单向动态链表*/
{
int i;
int length,flag=1;
struct student *head;
struct student *p,*pt;
n=0;
while(flag==1)
{
printf("BEGIN!--How many students do you want to input?(Input the amount):_"); /*输入你想输入学生数据的学生数量*/
scanf("%d",&length);
if(length!=0) flag=0;
}
for(i=0;i<length;i++)
{p=(struct student * )malloc(LEN);/*开辟一个新单元*/
if(i==0) head=pt=p;
else pt->next=p;
pt=p;
printf("Plese input the basic info and his(her) grade of the studen_%d\n",i+1);
p->m=i+1;
printf("This is a serial number for using!__( %d )\n",p->m); /*为各学生定一编号*/
printf(" No.");
scanf("%s",p->num);
printf(" NAME:");
scanf("%s",p->name);
printf(" tel:");
scanf("%s",p->tel);
printf(" birth:");
scanf("%s",p->birth);
printf("sushe:");
scanf("%s",p->sushe);
printf("address:");
scanf("%s",p->address);
printf("His(Her) grade:\n");
printf(" MATH:");
scanf("%d",&p->math); /*要求输入的为学生的数学,英语,物理三科的分数*/
printf(" English:");
scanf("%d",&p->English);
printf(" PHYSICS:");
scanf("%d",&p->physics);
}
n=n+i;
p->next=NULL;
return(head);
}
void print(struct student *head) /*输出链表函数,将各数据元素值输出*/
{struct student *p1;
printf("\nNOW!These %d studends'infor and his(her) grade you have inputed are:\n",n);
p1=head;
printf("\n(SER NUM) NO. NAME tel birth sushe address score:MATH ENGLISH PHYSICS\n");
if(head!=NULL)
do
{printf("(%d)%12s%8s%8s%8s%8s%9s%13d%8d%10d\n",p1->m,p1->num,p1->name,p1->tel,p1->birth,p1->sushe,p1->address,p1->math,p1->English,p1->physics);
p1=p1->next;
}while(p1!=NULL);
}
struct student * del(struct student *head,int num) /*删除结点函数,即将你想要删除的一组数据删除*/
{struct student *p1,*p2;
if(head==NULL)
{printf("\nlist null!\n");
goto end;
}
p1=head;
while(num!=p1->m&&p1->next!=NULL) /*p1指向的不是所要找的结点,并且后面还有结点*/
{p2=p1;p1=p1->next;} /*p1后移一个结点*/
if(num==p1->m)
{if(p1==head) head=p1->next; /*找到结点*/
else p2->next=p1->next; /*否则将下一结点地址赋给前一结点地址*/
printf("dele:%d\n",num);
n=n-1;
}
else printf("%d not been found! \n",num);
end:;
return(head);
}
struct student * insert(struct student *head,struct student *stud)/*插入结点函数,插入一组新数据*/
{struct student *p0,*p1,*p2;
p1=head; /*使p1指向第一个结点*/
p0=stud; /*p0指向要插入的结点*/
if(head==NULL) /*原来的链表为空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->m>p1->m)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才指向的结点*/
p1=p1->next;} /*p1后移一个结点*/
if(p0->m<=p1->m)
{if(head==p1) head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;}/*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
return(head);
}
struct student *change(struct student *head,long num) /*修改各数据元素的值的函数*/
{struct student *p1,*pk;
char b[10],c[5],d[30],e[30],f[30];
int M,E,P;
p1=head;
printf("\ninput the new infor\n");
printf(" NAME:");
scanf("%s",b);
printf(" tel:");
scanf("%s",c);
printf(" birth:");
scanf("%s",d);
printf(" sushe:");
scanf("%s",e);
printf(" address:");
scanf("%s",f);
printf("His(Her) grade:\n");
printf(" MATH:");
scanf("%d",&M);
printf(" English:");
scanf("%d",&E);
printf(" PHYSICS:");
scanf("%d",&P);
while(num!=p1->m&&p1->next!=NULL)
{p1=p1->next;}
if(num==p1->m) /*找到要修改的结点*/
{strcpy(p1->name,b); /*将新值复制到要修改的结点的对应的元素上*/
strcpy(p1->tel,c);
p1->math=M;
p1->English=E;
p1->physics=P;
}
return(head);
}
void save(struct student *head) /*存盘函数,将数据存入磁盘中*/
{FILE *fp;
struct student *p1;
p1=head;
if((fp=fopen("stu.rec","wb"))==NULL)
{printf("cannot open file\n");
return;
}
if(head!=NULL)
do
{if(fwrite(p1,sizeof(struct student),1,fp)!=1)
printf("file write error!\n");
p1=p1->next;}while(p1!=NULL);
fclose(fp);
}
void pr(struct student *head) /*读盘数据并输出函数*/
{FILE *fp;
struct student *p1;
p1=head;
printf("The nether datas are that you have inputed:\n");
printf("\n(SER NUM) NO. NAME tel birth sushe address score:MATH ENGLISH PHYSICS\n");
printf("-------------------------------------------------------------------------------\n");
printf("-------------------------------------------------------------------------------\n");
fp=fopen("stu.rec","rb");
if(head!=NULL)
do
{fread(p1,sizeof(struct student),1,fp);
printf(" (%d)%12s%8s%6s%8s%8s%9s%13d%8d%10d\n",p1->m,p1->num,p1->name,p1->tel,p1->birth,p1->sushe,p1->address,p1->math,p1->English,p1->physics);
p1=p1->next;}while(p1!=NULL);
}
void main() /*主函数*/
{struct student *head,stu;
int c,j,k,t;
int del_num;
int change_num;
printf(" ******************************\n"); /*显示制作者自定义该程序的名字*/
printf(" * *\n");
printf(" * welcome to keweibin's play *\n");
printf(" * *\n");
printf(" ******************************\n");
printf(" PROGRAM BEGIN!!\n");
RE:printf("FIRST:(1).INPUT THE NEWS DATAS (2).CHOOSE THE FUNCTION\n"); /*程序开始,1选择为输入新的数据,2选择为选择该程序的功能*/
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("<<CHOOSE(1)or(2)>>");
scanf("%d",&t);
if(t==1)
{ head=creat();
printf("Whether save these datas?(1.yes 2.no) ");
scanf("%d",&k);if(k==1) save(head);printf("\n *SAVE SUCCEED!*\n");}
else if(t==2)goto PT;
else {printf(" Error!!\n");
goto RE;} /*若输入的非1或2,则跳回重新选择*/
PT: printf("* chose the function *\n");
printf("======================================================================\n");/*制作者自设计的功能排列表*/
printf(" (1).show the students'infor (2).delete one student's infor\n");
printf(" (3).insert another students'infor (4).change one students'infor \n");
printf(" (5).lookup and output the datas you have inputed \n");
printf(" (6).Input the new infor again! (7).END \n");
printf("======================================================================\n");
printf(" {PROMPT!!:If there is not some datas appearing on the screen,not to choose(1)(2)(3)(4)(5).}\n");/*该句为向用户提示:如果屏幕没有显示数据,则不要选择1至5项*/
loop:printf("Plese choose(1--7): ");
scanf("%d",&c);/*输入用户选择项的编号*/
switch(c)
{
case 1: print(head); /*执行显示函数*/
printf("Whether save these datas?(1.yes 2.no)_"); /*以下三句为向用户询问是否保存该数据*/
scanf("%d",&k);if(k==1) {save(head); printf("\n *SAVE SUCCEED!*\n"); }
printf("Whether continue choose the function?(1.yes 2.no)_");/*向用户询问是否继续使用功能选择*/
scanf("%d",&j);if(j==1) goto loop;else exit(0);break;
case 2: printf("\ndelete(choose the serial number)_:" ); /*执行删除函数*/
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
printf("Whether save these datas?(1.yes 2.no)_");
scanf("%d",&k);if(k==1) {save(head); printf("\n *SAVE SUCCEED!*\n"); }
printf("Whether continue choose the function(1.yes 2.no)_");
scanf("%d",&j);if(j==1)goto loop;else exit(0);
break;
case 3: printf("\ninput the inserted record:\n"); /*执行插入函数*/
printf("you must give him(her) a serial number:__");/*用户必须向新输入的学生定一个编号,方便记忆使用*/
scanf("%d",&stu.m);
printf("No.:");
scanf("%s",stu.num);
printf("name:");
scanf("%s",stu.name);
printf("tel:");
scanf("%s",stu.tel);
printf("birth:");
scanf("%s",stu.birth);
printf("His(Her) grade:\n");
printf("MATH:");
scanf("%d",&stu.math);
printf("ENGLISH:");
scanf("%d",&stu.English);
printf("PHYSICS:");
scanf("%d",&stu.physics);
head=insert(head,&stu);
print(head);
printf("Whether save these datas?(1.yes 2.no)_");
scanf("%d",&k);if(k==1) {save(head); printf("\n *SAVE SUCCEED!*\n"); }
printf("Whether continue choose the function(1.yes 2.no)_");
scanf("%d",&j);if(j==1)goto loop;else exit(0);
break;
case 4: printf("\ninput the serial number you want to change the infor of its:"); /*执行修改函数*/
scanf("%d",&change_num);
head=change(head,change_num);
print(head);
printf("Whether save these datas?(1.yes 2.no)_");
scanf("%d",&k);if(k==1) {save(head); printf("\n *SAVE SUCCEED!*]\n"); }
printf("Whether continue choose the function(1.yes 2.no)_");
scanf("%d",&j);if(j==1)goto loop;else exit(0);
break;
case 5: pr(head);/*执行从盘读出数据并向屏幕输出函数*/
printf("Whether save these datas?(1.yes 2.no)_");
scanf("%d",&k);if(k==1) {save(head); printf("\n *SAVE SUCCEED!*\n"); }
printf("Whether continue choose the function(1.yes 2.no)_");
scanf("%d",&j);if(j==1)goto loop;else exit(0);
break;
case 6: head=creat(); /*执行输入新数据函数*/
printf("Whether save these datas?(1.yes 2.no)_");
scanf("%d",&k);if(k==1) {save(head); printf("\n *SAVE SUCCEED!*\n"); }
printf("Whether continue choose the function?(1.yes 2.no)_");
scanf("%d",&j);if(j==1) goto loop;else exit(0);break;
case 7: exit(0); /*终止程序*/
}
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -