C语言实现学生个人消费管理系统
本文实例为大家分享了C语言实现学生个人消费管理系统的具体代码,供大家参考,具体内容如下
程序介绍
运行程序时,首先进入到菜单部分,菜单部分提供了菜单显示和输入功能部分。其运行效果如图所示。在主界面上输入数字0——7,实现相应的功能。
此系统有两种录入方式,一种是输入1时,直接从终端键盘输入信息,使用方法如下:
主界面输入“1”,进入创建界面,开始创建学生个人消费信息。学生个人消费信息创建界面运行效果如图所示。
另一种是输入2时,从磁盘文件录入学生信息。使用方法如下:
首先在自己创建的文件f:\cff.txt记事本中输入学生个人消费信息,如图所示。
然后,在主界面中输入“2”,开始加载学生消费信息文件,根据提示输入存有学生信息的文件路径和名称,即可弹出文件中的信息。运行效果如图所示。
通过以上两种录入方式,分别对学生信息进行3-7的操作,当输入1,创建完信息后,可以直接操作3-7的功能,同样在输入2后,也可以直接操作3-7的功能。但是需要注意,每次3-7的功能操作都是针对前一步的录入方式。与之前操作过的录入方式无关。
输入“3”,可以查询学生消费信息,根据提示输入查询的学生学号,即可调出该学生的信息,运行效果如图所示。
输入“5”,可以添加学生的消费信息,并显示添加后学生的人数。运行效果如图所示。
输入“6”,可以显示录入的学生信息,运行效果如图所示。
输入“7”,可以把录入的学生信息保存到指定的文件中,运行效果如图所示。
本程序指定的保存路径为F:\CONSUME,保存成功后在F盘中会自动生成CONSUME文件,效果如图所示。
由于未指定文件的打开方式,故打开文件会弹出打开方式对话框,如图所示。
以记事本的方式打开文件,效果如图所示。
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <coNIO.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);
int 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("please input student's infORMation,input 0 exit!\n");
repeat1: printf("please input student's number(number>0):");
scanf(" %d",&p1->number);
while(p1->number<0)
{
getch();
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 repeate,please input again!\n");
goto repeat1;
}
}
}
}
printf("please input student's name:");
scanf("%s",&p1->name);
printf("please input student's consume money:");
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 student's information,input 0 exit!\n");
repeat2:printf("please input student's number(number>0):");
scanf("%d",&p1->number);
while(p1->number<0)
{
getch();
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 repeate,please input again!\n");
goto repeat2;
}
}
}
}
}
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("please input file's postion and its name:");
scanf("%s",filepn);
if((fp=fopen(filepn,"r+"))==NULL)
{
printf("can'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);
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);
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);
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("\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);
}
#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)
{
getch();
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();
}
}
int save(score *p1)
{
FILE *fp;
if((fp=fopen("f:\\consume","wb"))==NULL)
{
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 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):");
}
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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章