C语言实现学生消费管理系统
本文实例为大家分享了C语言实现学生消费管理系统的具体代码,供大家参考,具体内容如下
代码可以实现的功能:
(1)直接从终端键盘输入信息
(2)从磁盘文件录入学生信息
(3)可以查询学生消费信息
(4)可以删除学生信息
(5)可以添加学生的消费信息,并显示添加后学生的人数
(6)显示录入的学生信息
(7) 把录入的学生信息保存到指定的文件中
代码:
#include <stdio.h>
#include <stdlib.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 n,k;
void menu();
score *creat(void);
score *load(score *head);
score *search(score *head);
score *del(score *head);
score *add(score *head,score *stu);
void print(score *head);
save(score *p1);
score *creat(void)
{
score *head;
score *p1,*p2,*p3,*max;
int i,j;
char t[10];
n=0;
p1=p2=p3=(score *)malloc(LEN);
printf("请输入学生信息!(以0结束)\n");
repeat1: printf("请输入学生编号(编号>0):");
scanf("%d",&p1->number);
while(p1->number<0)
{
getchar();
printf("错误,请重新输入号码:");
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 repeate,please input again!\n");
goto repeat1;
}
}
}
}
printf("请输入学生姓名");
scanf("%s",&p1->name);
printf("请输入消费情况");
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("请输入学生信息(以0结束)\n");
repeat2:printf("请输入学号(学号应大于0):");
scanf("%d",&p1->number);
while(p1->number<0)
{getchar();
printf("请重新输入学号:");
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 repeate,please input again!\n");
goto repeat2;
}
}
}
}
printf("请输入学生姓名:");
scanf("%s",&p1->name);
printf("请输入学生消费形况:");
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)
{
k=max->number;
max->number=p1->number;
p1->number=k;
strcpy(t,max->name);
strcpy(max->name,p1->name);
strcpy(p1->name,t);
}
}
max=head;p1=head;
}
p2->next=NULL;
printf("input student's num:%d ge!\n",n);
getch();
return(head);
}
score *load(score *head)
{
score *p1,*p2;
int m=0;
char filepn[10];
FILE *fp;
printf("请输入路径及文件名:");
scanf("%s",filepn);
if((fp=fopen(filepn,"r+"))==NULL)
{
printf("不能打开此文件\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);
head=NULL;
do
{
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);
}while(!feof(fp));
p2->next=p1;
p1->next=NULL;
n=n+1;
}
printf("-----------------------------------------\n");
getch();
fclose(fp);
return (head);
}
score *search(score *head)
{
int number;
score *p1,*p2;
printf("input the student's number of searching:");
scanf("%d",&number);
getchar();
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 student not exist!\n",number);
printf("input the student's number of searching:");
scanf("%d",&number);
getchar();
}
printf("already exit!\n");
getchar();
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);
getchar();
while(number!=0)
{
if(head==NULL)
{
printf("\nnobody 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("%d student not exist!\n",number);
printf("input the student's number of deleting:");
scanf("%d",&number);
getchar();
}
#ifdef DEBUG
printf("already exit\n");
#endif
printf("now how many students:%d ge!\n",n);
getch();
return(head);
}
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 student's information of adding!");
repeat4: printf("please input the student's number(number>0):");
scanf("%d",&stu->number);
while(stu->number<0)
{
getchar();
printf("error,please input number again:");
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 student'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);
}
max=head;
p1=head;
}
}
end2:
printf("now how many students are they:%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|consume\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();
}
}
save(score *p1) //保存
{
FILE *fp;
if((fp=fopen("d:\\text","wb+"))==NULL) //文件保存在d盘下,文件名为text
{
printf("can't open this file!\n"); //不能打开这个文件
return 0;
}
else
{
while(p1!=NULL)
{
fprintf(fp,"%d %s %d\t\t\t",p1->number,p1->name,p1->xiaofei);//将学生信息写入到文件中
p1=p1->next;
}
printf("file save complete!please enter return!\n"); //文件保存完成!请输入空车键返回
getch();
}
fclose(fp); //关闭文件
}
void menu()
{
system("cls");
printf("\n\n\n");
printf("\t\t-------------STUDENT CONSUME-------------\n");
printf("\t\t\t0 退出 \n"); //错误
printf("\t\t\t1 直接从终端键盘输入信息 \n"); //直接从终端键盘输入信息
printf("\t\t\t2 从磁盘文件录入学生信息 \n"); //(加载)从磁盘文件录入学生信息
printf("\t\t\t3 可以查询学生消费信息 \n"); //(搜索)可以查询学生消费信息
printf("\t\t\t4 可以删除学生信息 \n"); //(删除)可以删除学生信息
printf("\t\t\t5 可以添加学生的消费信息 \n"); //(添加)可以添加学生的消费信息,并显示添加后学生的人数
printf("\t\t\t6 显示录入的学生信息 \n"); //(显示)显示录入的学生信息
printf("\t\t\t7 把录入的学生信息保存到指定的文件中 \n"); //(保存)把录入的学生信息保存到指定的文件中
printf("\t\t-----------------------------------------\n\n"); //
printf("\t\tchoose(0-7):");
}
main()
{
int num;
score *head=0,*stu=0;
menu();
scanf("%d",&num);
while(1)
{
switch(num)
{
case 1: head=creat();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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章