代码修改如下:
main.cpp
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"func.h"
void main ()
{
struct Node* head,*p;
int i,j,k;
char name[20];
char pwd[10];
head=(struct Node*)malloc(sizeof(struct Node));
head->next=NULL;
printf("\n*******************欢迎进入火车票管理系统*******************\n");
printf("\t\t 1.用户登录\n");
printf("\t\t 2.用户注册\n");
printf("\t\t 0.退出系统\n");
printf("请输入所选序号:");
scanf("%d",&k);
switch(k)
{
case 1:printf("请输入用户名:");
scanf("%s",name);
printf("请输入密码:");
scanf("%s",pwd);
if(check(name,pwd))
{
printf("密码正确.\n");
do
{
printf("\n\n\t\t**************欢迎进入火车票管理系统**************\n");
printf("\t\t 1.录入火车票信息\t\t 2.添加火车票信息\n");
printf("\t\t 3.修改火车票信息\t\t 4.删除火车票信息\n");
printf("\t\t 5.打印火车票信息\t\t 6.查询火车票信息\n");
printf("\t\t 7.统计火车票信息\t\t 8.火车票销售排行\n");
printf("\t\t 0.退出系统\n");
printf("请输入您要进入菜单的序号(0-8):");
scanf("%d",&i);
switch(i)
{
case 1:
printf("请录入火车票信息\n\n");
head=create();
save(head);
head=read();
break;
case 2:
append ();
break ;
case 3:
printf("请输入您要修改的火车票的编号:\n");
scanf("%d",&j);
change(head,j);
save(head);
break ;
case 4:
head=read();
if(dele(head))
{
printf("已经正确删除!\n");
save(head);
}
else
printf("要删除的结点不存在!\n");
break;
case 5:
head=read();
print(head);
break ;
case 6:
printf("请输入您要查询火车票的编号(以0结束):\n");
scanf("%d",&j);
{
p=find(head);
printf("编号 起点和终点 出发时间 车票价格 剩余票数:\n");
printf("%d %10s %5s %10d %6d\n",p->num,p->name,p->time,p->price,p->amount);
printf("请继续输入序号(以0结束):");
scanf("%d",&j);
}
break ;
case 7:
head=read();
count(head);
break;
case 8:
sort(head);
break ;
case 0:
printf("*******************谢谢使用!*******************");
break;
}
}
while(i!=0);
}
else
printf("密码错误或用户名不存在.\n");
break;
case 2:save_user();
break;
case 0:
break;
}
}
date.h:
//date.h//
struct Node
{
int num;
char name[20];
char time[20];
int price;
int amount;
struct Node* next;
};
func.h
//func.h//
#include "date.h"
struct Node* create();
void save(struct Node*h);
struct Node*read ();
void print(struct Node*h);
struct Node*find(struct Node*h);
void change(struct Node* h,int k);
int dele(struct Node* h);
void append();
void count(struct Node*h);
void save_user();
int check(char *name,char *pwd);
void sort (struct Node*h);
func.c
//func.c//
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "func.h"
struct Node* create()
{
struct Node*head,*r,*s;
int i=0;
char choice;
head=(struct Node*)malloc(sizeof(struct Node));
head->next=NULL;
r=head;
do
{
s=(struct Node*)malloc(sizeof(struct Node));
s->next=NULL;
printf("请输入第%d种火车票的信息:n",++i);
printf("请输入火车的编号:");
scanf("%d",&s->num );
printf("起点和终点:");
scanf("%s",s->name);
printf("出发时间:");
scanf("%s",s->time);
printf("车票价格:");
scanf("%d",&s->price);
printf("剩余数量:");
scanf("%d",&s->amount);
r->next=s;
r=s;
printf ("Continue?( Y / N )");
scanf ("%s",&choice );
}
while (choice=='Y'||choice=='y');
r->next=NULL;
return(head);
}
//保存到1//
void save(struct Node* h)
{
struct Node*s;
FILE * fp ;
char filename [10]="1.txt";
fp=fopen("1.txt","wt");
if(fp==NULL)
{
printf ("\n 写文件出错,按任意键退出!");
getchar ();
exit (1);
}
for (s=h->next;s!=NULL;s=s->next);
fprintf(fp,"%d %s %s %d %d \n ",s->num, s->name,s->time,s->price,s->amount);
getchar();
fclose (fp);
}
//读取1//
struct Node*read ()
{
struct Node*head ,*r,*s;
FILE*fp ;
char filename[10]="zl.txt ";
fp=fopen ("1.txt","rt");
if(fp==NULL)
{
printf ("读文件错误,按任意键退出!");
getchar ();
exit (1);
}
head=(struct Node*) malloc(sizeof(struct Node));
head->next=NULL ;
r=head ;
while(!feof(fp))
{
s=(struct Node*)malloc(sizeof(struct Node));
fscanf (fp,"%d %s %s %d %d",&s->num,s->name,s->time,&s->price,&s->amount);
r->next=s;
}
r->next=NULL;
fclose(fp);
return head;
}
//输出//
void print(struct Node*h)
{
struct Node * s ;
printf ("\n火车票信息如下\n ");
printf ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf ("编号 起点和终点 出发时间 车票价格 剩余票数:\n");
for(s=h->next;s->next!=NULL;s=s->next)
{
printf ("%d %10s %5s %10d %6d\n",s->num,s->name,s->time,s->price,s->amount);
}
}
//查询//
struct Node*find(struct Node*h)
{
int i,j;
char s[20];
printf ("\t\t 查询方法有以下几种: \n");
printf ("\t\t 1.火车票编号\n");
printf ("\t\t 2.起点和终点\n");
printf ("\t\t 3.出发时间\n");
printf ("\t\t 4.车票价格\n");
printf ("\t\t 5.剩余票数\n");
printf ("请输入您要查询的方法的序号:");
scanf("%d",&i);
switch (i)
{
case 1:printf("请输入你要查询火车票的编号:");
scanf ("%d",&j);
while(h->next!=NULL)
{
h=h->next;
if(h->num==j)
return h;
}
return NULL ;
break ;
case 2:printf("请输入您要查询火车票的起点和终点:");
scanf("%s",s);
while (h->next!=NULL)
{
h=h->next;
if(strcmp(h->name,s)==0)
return h ;
}
return NULL;
break;
case 3:printf("请输入您要查询火车票的时间:");
scanf("%s",s);
while(h->next!=NULL)
{
h=h->next;
if(strcmp(h->time,s)==0)
return h;
}
return NULL;
break;
case 4:printf("请输入你要查询火车票的价格:");
scanf("%d",&j);
while(h->next!=NULL)
{
h=h->next ;
if(h->price==j)
return h ;
}
return NULL;
break;
case 5:printf("请输入你要查询火车票的剩余票数:");scanf("%d",&j);
while(h->next!=NULL)
{
h=h->next;
if(h->amount==j)
return h;
}
return NULL ;
break;
}
}
//修改//
void change(struct Node*h,int k)
{
int j;
struct Node*p;
p=find(h);
printf ("----------------------------");
printf ("\t 您要修改哪一项?\n");
printf ("\t 1.火车编号\n");
printf ("\t 2.起点和终点\n");
printf ("\t 3.出发时间\n");
printf ("\t 4.车票价格\n");
printf ("\t 5.剰余票数\n");
printf ("\t 0.退出系统\n");
printf ("-------------------\n");
printf ("请输入您要修改项的编号:");
scanf ("%d",&j);
switch (j)
{
case 1:
printf ("修改后的火车编号:");
scanf ("%d",&p->num);
break;
case 2:
printf("修改后的起点和终点:");
scanf ("%s",p->name);
break;
case 3:
printf("修改后的出发时间:");
scanf ("%s",p->time);
break;
case 4:
printf("修改后的车票价格:");
scanf ("%d",&p->price);
break;
case 5:
printf("修改后的剩余票数:");
scanf ("%d",&p->amount);
break ;
case 0:break;
}
}
//删除//
int dele(struct Node *h)
{
struct Node*p ;
int j;
printf("请输入您要删除的火车票的编号:");
scanf ("%d",&j);
p=h->next;
if(p==NULL)
return 0;
while(p!=NULL)
{
if(p->num==j)
{
h->next=p->next;
free ( p );
return 1;
}
h=p;
p=p->next;
}
return 0;
}
//添加//
void append ()
{
struct Node *p;
FILE*fp;
fp=fopen("1.txt","at+");
if(fp==NULL)
{
printf("写文件出错,按任意键返回.\n");
getchar ();
exit (1);
}
printf("请输入要添加的火车票的信息:火车编号,起点和终点,出发时间,车票价格,剩余票数:\n");
scanf("%d%s%s%d%d",&p->num,p->name,p->time,&p->price,&p->amount);
fprintf(fp,"%d %s %s %d %d\n",p->num,p->name,p->time,p->price,p->amount);
getchar();
fclose(fp);
}
//统计//
void count(struct Node *h)
{
int i,j,k,n=0;
struct Node *s;
s = h;
printf ("******************************************************************************************************\n");
printf ("\t\t 请选择您要统计项目的序号:\n");
printf ("\t\t 1.车票价格\n");
printf ("\t\t 2.剰余票数\n");
printf ("\t\t 0.退出界面\n");
scanf("%d",&i);
switch(i)
{
case 1:
printf ("请输入您要统计车票的价格的标准:");
scanf ("%d",&j);
printf ("\t\t请选择低于或高于标准:\n");
printf ("\t\t1.价格低于%d的个数\n",j);
printf ("\t\t2.价格高于%d的个数\n",j);
scanf ("%d",&k);
if(k==1)
{
for(s=h->next;s->next!=NULL;s=s->next)
if(s->price<j)
n++;
printf("车票价格低于%d的个数有%d个.\n",j,n);
}
else
{
for(s=h->next;s->next!=NULL;s=s->next)
if(s->price>j)
n++;
printf("车票价格高于%d的个数有%d个.\n",j,n);
}
break;
case 2:
printf("请输入您要统计剩余票数的数量:");
scanf("%d",&j);
printf("\t\t请选择低于或高于所输票数:\n");
printf("\t\t1.票数低于%d的个数\n",j);
printf("\t\t2.票数高于%d的个数\n",j);
scanf("%d",&k);
if(k==1)
{
for(s=h->next;s->next!=NULL;s=s->next)
if(s->amount<j)
n++;
printf("剰余票数低于%d的个数有%d个.\n",j,n);
}
else
{
for(s=h->next;s->next!=NULL;s=s->next)
if(s->amount>j)
n++;
printf("剩余票数高于%d的个数有%d个.\n",j,n);
}
break;
case 0:break;
}
}
//用户注册//
void save_user()
{
char file[10]="2.txt";
FILE*fp ;
char name[20];
char pwd[10];
fp=fopen("2.txt","at+");
if(fp==NULL)
{
printf("\n写文件出错,按任意键退出.\n");
getchar();
exit(1);
}
printf("请输入用户名:");
scanf("%s",name);
printf("请输入密码:");
scanf("%s",pwd);
fprintf(fp,"%s%s\n",name,pwd);
getchar();
fclose(fp);
printf("用户注册成功!\n");
}
//登陆//
int check(char* name,char* pwd)
{
char name1[20];
char pwd1[10];
FILE*fp;
char file[10]="2.txt";
if((fp=fopen("2.txt","rt"))==NULL)
{
printf("读文件出错,按任意键退出!\n");
getchar();
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%s%s",name1,pwd1);
if(strcmp(name1,name)==0&&strcmp(pwd1,pwd)==0)
return 1;
}
return 0;
}
//排序//
void sort(struct Node*h)
{
struct Node*s,*p,*m,*n;
int t,t1,t2,t3;
char s1[20];
char s2[10];
printf("车票价格由低到高排序如下:\n");
for(s=h->next;s->next!=NULL;s=s->next)
for(p=s->next;p->next!=NULL;p=p->next)
if(s->price>p->price)
{
t1=s->num;s->num=p->num;p->num=t1;
t2=s->price;s->price=p->price;p->price=t2;
t3=s->amount;s->amount=p->amount;p->amount=t3;
strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1);
strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2);
}
print(h);
printf("\n\n剩余车票数量由多到少排序如下:n");
for(s=h->next;s->next!=NULL;s=s->next)
for(p=s->next;p->next!=NULL;p=p->next)
if(s->amount<p->amount)
{
t1=s->num;s->num=p->num;p->num=t1;
t2=s->price;s->price=p->price;p->price=t2;
t3=s->amount;s->amount=p->amount;p->amount=t3;
strcpy(s1,s->name);strcpy(s->name,p->name);strcpy(p->name,s1);
strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2);
}
print(h);
}