标签:fine nod money return system n+1 typedef default osi
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define LEN sizeof(struct scorenode)
#define DEBUG
struct scorenode{
int number;
char name[10];
int xiaofei;
struct scorenode *next;
};
typedef struct scorenode score;
int i,n,k,j;
char t;
score *become(void);
score *load(score *head);
score *search(score *head);
score *del(score *head);
score *add(score *head,score *stu);
void print(score *head);
int save(score *p1);//************************
void menu(){
system("cls"); /*清屏*/
printf("\n\n\n");
printf("\t\t-------------STUDENT CONSUME-------------\n");
printf("\t\t\t0 exit \n");
printf("\t\t\t1 creat \n");
printf("\t\t\t2 load \n");
printf("\t\t\t3 search \n");
printf("\t\t\t4 delete \n");
printf("\t\t\t5 add \n");
printf("\t\t\t6 show \n");
printf("\t\t\t7 save \n");
printf("\t\t-----------------------------------------\n\n");
printf("\t\tchoose(0-7):");
}
int main()
{
int num;
score *head=0,*stu=0;
menu();
scanf("%d",&num);
while(1)
{
switch(num)
{
case 1:head=become();break;
case 2:head=load(head);break;
case 3:head=search(head);break;
case 4:head=del(head);break;
case 5:head=add(head,stu);break;
case 6:print(head);break;
case 7:save(head);break;
case 0:exit(0);
default:printf("input error,please again");
}
menu();
scanf("%d",&num);
}
}
score *become(void){
score *head;
score *p1,*p2,*p3,*max;
p1=(score *)malloc(LEN);
p2=(score *)malloc(LEN);
p3=(score *)malloc(LEN);/*开辟一个新单元*/
printf("please input students‘s information ,input 0 exit!\n");
repeat1:printf("please input students‘s number(number>0):");
scanf(" %d",&p1->number);
while(p1->number<0){
getchar();
printf("error,please input number again:");
scanf("%d",&p1->number);
}
if(p1->number==0)
goto end;
else
{
head=p3;
if(n>0)
{
for(i=0;i<n;i++)
{
if(p1->number!=p3->number)
p3=p3->next;
else
{
printf("number repeat,please input again!\n");
goto repeat1;
}
}
}
}
printf("please input students‘s name:");
scanf("%s",p1->name);
printf("please input students‘s consume:");
scanf("%d",&p1->xiaofei);
while(p1->number!=0)
{
n=n+1;
if(n==1)
{
head=p1;
}
else
p2->next=p1;
p2=p1;
p1=(score *)malloc(LEN);
printf("please input students‘s information,input 0 exit\n");
repeat2:printf("please input students‘s number (number>0).");
scanf("%d",&p1->number);
while(p1->number<0)
{getchar();
printf("error,please input number again:");
scanf("%d",&p1->number);
}
if(p1->number==0)
goto end;
else
{
p3=head;
if(n>0)
{
for(i=0;i<n;i++)
{
if(p1->number!=p3->number)
p3=p3->next;
else
{
printf("number repeat,please input again!\n");
goto repeat2;
}
}
}
}
printf("please input students‘s name:");
scanf("%s",p1->name);
printf("please input students‘s consume money:");
scanf("%d",&p1->xiaofei);
}
end:p1=head;
p3=p1;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
max=p1;
p1=p1->next;
if(max->number>p1->number)
{
int k=max->number;
max->number=p1->number;
p1->number=k;
strcpy(&t,max->name);
strcpy(max->name,p1->name);
strcpy(p1->name,&t);
k=max->xiaofei;
max->xiaofei=p1->xiaofei;
p1->xiaofei=k;
}
}
max=head;
p1=head;
}
p2->next=NULL;
printf("input students num:%d ge",n);
getch();
return (head);
}
score *load(score *head)
{
score *p1,*p2;
int m=0;
char filepn[10];
FILE *fp;
printf("please input file‘s position and its name:");
scanf("%s",filepn);
if((fp=fopen(filepn,"r+"))==NULL)
{
printf("cann‘t open this file!\n");
getch();
return 0;
}
else
{
p1=(score *)malloc(LEN);
fscanf(fp,"%d%s%d\n",&p1->number,p1->name,&p1->xiaofei);
printf("|%d\t|%s\t|%d\t|\n",p1->number,p1->name,p1->xiaofei);/*读入与显示*/
printf("------------------------\n");
head=NULL;
while (!feof(fp))
{
n=n+1;
if(n==1)
{
head=p1;
}else
p2->next=p1;
p2=p1;
p1=(score*)malloc(LEN);
fscanf(fp,"%d%s%d\n",&p1->number,p1->name,&p1->xiaofei);
printf("|%d\t|%s\t|%d\t|\n",p1->number,p1->name,p1->xiaofei);
printf("------------------------\n");
}
p2->next=p1;
p1->next=NULL;
n=n+1;
}
getch();
fclose(fp);
return (head);
}
score*search(score *head)
{
int number;
score *p1,*p2;
printf("input the students number of searching:");
scanf("%d",&number);
while(number!=0)
{
if(head==NULL)
{
printf("\n nobody information!\n");
return(head);
}
printf("---------------------------\n");
printf("|number\t|name\t|consume\t\n");
printf("----------------------------\n");//打印表格域
p1=head;
while(number!=p1->number&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(number==p1->number)
{
printf("|%d\t|%s\t|%d\t|\n",p1->number,p1->name,p1->xiaofei);
printf("-------------------------\n");
}//打印表格域
else
printf("%dthis studet not exit!\n",number);
printf("input the student‘s number of searching:");
scanf("%d",&number);
getch();
}
printf("already exit|\n");
getch();
return (head);
}
score *del(score *head)
{
score *p1,*p2;
int number;
printf("input the student‘s number of deleting(input 0 exit):");
scanf("%d",&number);
while(number!=0)
{
if(head==NULL)
{
printf("\nobody information !\n");
return (head);
}
p1=head;
while(number!=p1->number&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(number==p1->number)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete number:%d\n",number);
n=n-1;
}
else
printf("%dstudent not exist!\n",number);
printf("input the student‘s number of deleting:‘");
scanf("%d",&number);
}
#ifdef DEBUG
printf("already exit\n");
#endif
printf("now how many students:%d ge!\n",n);
getch();
return (head);
}
void print(score *head)
{
score *p;
if(head==NULL)
printf("\nnobody information!\n");
else
{
printf("%d\n",n);
printf("--------------------------------\n");
printf("|number\t|name\t|consumme\t|\n");
printf("--------------------------------\n");
p=head;
do
{
printf("|%d\t|%s\t|%d\t\n",p->number,p->name,p->xiaofei);
printf("--------------------------------\n");
p=p->next;
}while(p!=NULL);
getch();
}
}
int save(score *p1)
{
FILE *fp;
if((fp=fopen("C:\\consume.txt","wb"))==NULL)
{
printf("can‘t open this file!");
return 0;
}
else
{
while(p1!=NULL)
{
fprintf(fp,"%d\t%s\t%d\t\n",p1->number,p1->name,p1->xiaofei);
p1=p1->next;
}
printf("file save complete please return:");
getch();
}
fclose(fp);
}
score *add(score *head,score *stu)
{
score *p0,*p1,*p2,*p3,*max;
int i,j;
char t[10];
p3=stu=(score *)malloc(LEN);
printf("\ninput the students‘s information of adding:\n");
repeat4:printf("please input the students‘s number (number>0):");
scanf("%d",&stu->number);
while(stu->number<0)
{
getch();
printf("error;please input number again!\n");
scanf("%d",&stu->number);
}
/**********************************************************/
if(stu->number==0)
{
goto end2;
}
else
{
p3=head;
if(n>0)
{
for(i=0;i<n;i++)
{
if(stu->number!=p3->number)
p3=p3->next;
else
{
printf("number repeat,please input again!\n");
goto repeat4;
}
}
}
}
/********************************************************************************/
printf("input the students‘s name:");
scanf("%s",stu->name);
printf("please input the consuming:");
scanf("%d",&stu->xiaofei);
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
if(p1->next==NULL)
{
p1->next=p0;
p0->next=NULL;
}
else
{
while(p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
p1=head;
p0=stu;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
max=p1;
p1=p1->next;
if(max->number>p1->number)
{
k=max->number;
max->number=p1->number;
p1->number=k;
strcpy(t,max->name);
strcpy(max->name,p1->name);
strcpy(p1->name,t);
k=max->xiaofei;
max->xiaofei=p1->xiaofei;
p1->xiaofei=k;
}
}
max=head;
p1=head;
}
end2:
printf("now how many students are they:%d ge !\n",n);
getch();
return (head);
}
标签:fine nod money return system n+1 typedef default osi
原文地址:https://www.cnblogs.com/my-boke/p/14209408.html