#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EmFile "C:\\zm.txt" //密码帐号文件名
#define N 3
#define TEMP 2
#define TRUE 1
#define FALSE 0
int shoudsave=0 ;
int count1=0,count2=0,mark=0,mark1=0 ;
struct train
{ char num[10];//列车号
char city[10];//目的城市
char takeoffTime[10];//发车时间
char receiveTime[10];//到达时间
int price;//票价
int bookNum ;//票数
int leftbookNum;
};
//订票人的信息
struct man
{
char num[10];//ID
char name[10];//姓名
int bookNum ;//需求的票数
char password[7];//密码
int tag;
};
//定义火车信息链表的结点结构
typedef struct node
{ struct train data ;
struct node * next ;
}Node,*Link ;
struct item_node{
struct train wanted;
int amount;
struct item_node *next;
};
struct train trains[N];
struct item_node *cart;
//初始界面
void menu()
{ puts("********************************************************");
puts("* 欢迎使用订票系统 *");
puts("********************************************************");
puts("* 1:增加一个火车信息 *");
puts("* 2:查询一个火车信息 *");
puts("* 3:订票 *");
puts("* 4:付款 *");
puts("* 5:更新火车信息 *");
puts("* 6:系统给用户的提示信息 *");
puts("* 7:保存信息到文件 *");
puts("* 8:退出 *");
puts("********************************************************");
}
void PW( char *password, int PWlength)//密码输入
{
char temp;
int i = 0;
loop:
printf ( "请输入你的密码(不超过%d位):", PWlength-1 );
while( 1 )
{
temp = getch();
if ( temp != '\b' && temp != '\r' && i < PWlength )
{
printf ( "*" );
password[i] = temp;
i++;
}
else if ( temp == '\b' )
{
printf ( "\b" );
printf( " " );
printf( "\b" );
i--;
}
else if( temp == '\r' )
{
password[i] = '\0';
printf( "\n" );
break;
}
else
{
printf ( "\nwarming!!!!\n" );
printf ( "注意!!!\n" );
printf( "密码长度不能超过%d位\n" ,PWlength-1);
printf( "请重新输入!!!\n" );
i = 0;
goto loop;
}
}
return;
}
void ID(char *name, int IDlength )//账号输入
{
char temp;
int i = 0;
loop:
printf ( "请输入你的帐号(不超过%d个字符):", IDlength-1 );
while( 1 )
{
temp = getche();
if ( temp != '\b' && temp != '\r' && i < IDlength )
{
name[i] = temp;
i++;
}
else if ( temp == '\b' )
{
printf( " " );
printf( "\b" );
i--;
}
else if (temp == '\r')
{
name[i] = '\0';
printf( "\n" );
break;
}
else
{
printf ( "\nwarming!!!!\n" );
printf ( "注意!!!\n" );
printf( "帐号长度不能超过%d位(中文字符不能超过%d位)\n" ,IDlength-1, ( IDlength-1)/2 );
printf( "请重新输入!!!\n" );
i = 0;
goto loop;
}
}
}
void Reg()//注册
{
int tempflag = FALSE;
FILE *fp;
man ma;
int i;
man tempma;
int flag;
ID(ma.name, 19);
PW(ma.password, 7);
ma.tag = 1;
fp = fopen(EmFile, "a+" );
if ( NULL != fp )
{
rewind(fp);
do
{
flag = fread(&tempma, sizeof(man), 1, fp);
if( 1 == flag )
{
tempflag = TRUE;
if ( 0 == strcmp(ma.name, tempma.name) )
{
if( 1 == tempma.tag )
{
fclose(fp);
printf( "该帐号已注册,不能再次注册!!!\n" );
system("pause");
i = TEMP;
}
else
{
fseek (fp, sizeof(man), 1);
i = FALSE;
}
}
else
{
i= TRUE;
}
}
else
{
if ( tempflag )
{
fseek(fp,0,2);
flag = fwrite(&ma,sizeof(man),1,fp);
if ( 1== flag )
{
printf( "注册成功!!!\n" );
system("pause");
fclose(fp);
i = TEMP;
}
else
{
printf( "error!!!\n" );
system("pause");
exit(1);
}
}
else
{
rewind(fp);
flag = fwrite(&ma,sizeof(man),1,fp);
if ( 1== flag )
{
printf( "注册成功!!!\n" );
system("pause");
fclose(fp);
i = TEMP;
}
else
{
printf( "error!!!\n" );
system("pause");
exit(1);
}
}
}
}while( TRUE == i );
if( TEMP != i )
{
fwrite ( &ma, sizeof(man), 1, fp );
flag = ferror(fp);
if ( 0 == flag )
{
clearerr(fp);
fclose(fp);
printf ( "注册成功!!!\n" );
}
else
{
printf ( "error:00024\n" ); //EmFile读取失败
system( "pause" );
exit(1);
}
}
}
else
{
printf ( "error:00002\n错误!!!n" ); //EmFile 创建失败
printf ( "系统退出!!" );
exit(1);
}
}
bool Login()//登录
{
FILE *fp;
int flag;
int count = 0;
int tempflag = 0;
man ma;
char na[10],mi[10];
fp = fopen(EmFile, "r");
if ( NULL != fp )
{
//ID(ma.name, 19);
//PW(ma.password, 7);
printf("请输入帐户名:");
scanf("%s",na);
printf("请输入密码:");
scanf("%s",mi);
do
{
flag = fread(&ma,sizeof(ma),1, fp);
if ( 1 == flag )
{
if ( 0 == strcmp(ma.name,na) && ma.tag == 1)
{
tempflag = 0;
while(count<3)
{
if ( 0 != count )
{
PW(mi,7);
}
if( 0 == strcmp(ma.password,mi) )
{
fclose(fp);
Sleep(1000);
system("cls");
return TRUE;
}
else
{
printf( "密码错误!!!\n" );
count++;
if( 3 == count )
{
printf ( "密码错误三次!\n" );
printf( "系统退出\n" );
fclose(fp);
Sleep(1000);
system("cls");
return FALSE;
}
}
}
}
else
{
tempflag = 1;
}
}
else
{
printf( "无此帐号,请核对帐号重新登录系统\n" );
tempflag = 0;
fclose(fp);
Sleep(1000);
system("cls");
return FALSE;
}
}while( tempflag );
}
else
{
printf( "error: 000010\n 错误!!!\n" ); //Emfile打开失败
exit(0);
}
}
//添加一个火车信息
void InsertTraininfo(Link linkhead)
{ struct node *p,*r,*s ;
char num[10];
r = linkhead ;
s = linkhead->next ;
while(r->next!=NULL)
r=r->next ;
while(1)
{ printf("请输入火车车次(0-返回)");
scanf("%s",num);
if(strcmp(num,"0")==0)
break ;
while(s)
{ if(strcmp(s->data.num,num)==0)
{
printf("the train '%s'has been born!\n",num);
return ;
}
s = s->next ;
}
p = (struct node*)malloc(sizeof(struct node));
strcpy(p->data.num,num);
printf("输入火车所到达的城市:");
scanf("%s",p->data.city);
printf("输入火车开车时间:");
scanf("%s",p->data.takeoffTime);
printf("输入火车到达时间:");
scanf("%s",&p->data.receiveTime);
printf("输入票价:");
scanf("%d",&p->data.price);
printf("输入票数:");
scanf("%d",&p->data.bookNum);
p->next=NULL ;
r->next=p ;
r=p ;
shoudsave = 1 ;
}
}
//打印火车票信息
void printTrainInfo(struct node*p)
{ puts("\nThe following is the record you want:");
printf(">>车次: %s\n",p->data.num);
printf(">>所到达城市: %s\n",p->data.city);
printf(">>开车时间: %s\n",p->data.takeoffTime);
printf(">>到达时间:%s\n",p->data.receiveTime);
printf(">>票价: %d\n",p->data.price);
printf(">>票数: %d\n",p->data.bookNum);
}
struct node * Locate1(Link l,char findmess[],char numorcity[])
{ Node*r ;
if(strcmp(numorcity,"num")==0)
{
r=l->next ;
while(r)
{
if(strcmp(r->data.num,findmess)==0)
return r ;
r=r->next ;
}
}
else if(strcmp(numorcity,"city")==0)
{
r=l->next ;
while(r)
{
if(strcmp(r->data.city,findmess)==0)
return r ;
r=r->next ;
}
}
return 0 ;
}
//查询火车信息
void QueryTrain(Link l)
{
Node *p ;
int sel ;
char str1[5],str2[10];
if(!l->next)
{
printf("There is not any record !");
return ;
}
printf("选择:\n>>1:按照火车车次;\n>>2:按照所到城市:\n");
scanf("%d",&sel);
if(sel==1)
{
printf("输入火车车次:");
scanf("%s",str1);
p=Locate1(l,str1,"num");
if(p)
{
printTrainInfo(p);
}
else
{
mark1=1 ;
printf("\nthe file can't be found!");
}
}
else if(sel==2)
{
printf("输入所到城市:");
scanf("%s",str2);
p=Locate1(l,str2,"city");
if(p)
{
printTrainInfo(p);
}
else
{
mark1=1 ;
printf("\nthe file can't be found!");
}
}
}
void calculate()
{
float total=0,pay;
struct item_node *p;
int i;
FILE *fp;
if((fp=fopen("c:\\train.txt","wb"))==NULL){
printf("打开文件失败: \n");
exit(0);
}
for(i=0;(fread(trains+i,sizeof(struct train),1,fp))!=0;i++);
fclose(fp);
p=cart;
while(p!=NULL){
total+=p->wanted.price*p->amount;
for(i=0;strcmp(trains[i].num,p->wanted.num)!=0;i++);
trains[i].leftbookNum-=p->amount;
p=p->next;
}
printf("总计 %7.2f",total);
printf("\n输入实付金额: ");
scanf("%f",&pay);
printf("实付: %7.2f 找零: %7.2f",pay,pay-total);
if((fp=fopen("goods","w"))==NULL){
printf("打开文件失败.\n");
return;
}
fwrite(trains,sizeof(struct train),N,fp);
fclose(fp);
}
void add()
{
FILE *fp;
int i,n;
char str[20];
char choice1,choice2;
struct item_node *p,*p1;
do
{
printf("输入车次: ");
fflush(stdin);
gets(str);
if((fp=fopen("c:\\train.txt","a+"))==NULL){
printf("打开文件失败\n");
continue;
}
for(i=0;fread(trains+i,sizeof(struct train),1,fp)!=0;i++){
if((strcmp(trains[i].num,str)==0 && trains[i].leftbookNum!=0)){
printf("已经找到所乘车次: \n");
printf("---------------------\n");
printf("车次 目的地 开车时间 到达时间 票价 余票\n");
printf("%s%6s%10s%10s%4d %4d\n",trains[i].num,trains[i].city,trains[i].takeoffTime,trains[i].receiveTime,trains[i].price,trains[i].leftbookNum);
printf("请输入所需数量: ");
scanf("%d",&n);
if(n>trains[i].leftbookNum){
printf("余票不足\n");
break;
}
printf("\n是否购买?(Y/N)");
fflush(stdin);
choice1=getchar();
if(choice1=='Y'||choice1=='y')
{
p1=(struct item_node*)malloc(sizeof(struct item_node));
if(p1==NULL){
printf("内存申请失败!\n");
exit(1);
}
p1->amount=n;
p1->wanted=trains[i];
p1->next=NULL;
p=cart;
if(cart==NULL)
cart=p1;
else{
while(p->next!=NULL)
p=p->next;
p1->next=p->next;
p->next=p1;
}
}
break;
}
}
if(i==N)
printf("未找到所需车次\n");
fclose(fp);
printf("是否继续购买?(Y/N)");
choice2=getchar();
}while(choice2=='Y'||choice2=='y');
}
//修改火车信息
void UpdateInfo(Link l)
{
Node*p ;
char findmess[20],ch ;
if(!l->next)
{
printf("\nthere isn't record for you to modify!\n");
return ;
}
else
{
QueryTrain(l);
if(mark1==0)
{
printf("\nDo you want to modify it?\n");
getchar();
scanf("%c",&ch);
if(ch=='y')
{
printf("\nInput the number of the train:");
scanf("%s",findmess);
p=Locate1(l,findmess,"num");
if(p)
{
printf("输入新的车次:");
scanf("%s",&p->data.num);
printf("输入新的到达城市:");
scanf("%s",&p->data.city);
printf("输入新的开车时间");
scanf("%s",&p->data.takeoffTime);
printf("输入新的到达时间:");
scanf("%s",&p->data.receiveTime);
printf("输入新的票价:");
scanf("%d",&p->data.price);
printf("输入新的票数:");
scanf("%d",&p->data.bookNum);
printf("\nmodifying record is sucessful!\n");
shoudsave=1 ;
}
else
printf("\t\t\tcan't find the record!");
}
}
else
mark1=0 ;
}
}
//系统给用户的提示信息
void AdvicedTrains(Link l)
{
Node*r ;
char str[10];
int mar=0 ;
r=l->next ;
printf("输入要到达的城市: ");
scanf("%s",str);
while(r)
{
if(strcmp(r->data.city,str)==0&&r->data.bookNum<200)
{
mar=1 ;
printf("\nyou can select the following train!\n");
printf("\n\nplease select the fourth operation to book the ticket!\n");
printTrainInfo(r);
}
r=r->next ;
}
if(mar==0)
printf("\n\t\t\tyou can't book any ticket now!\n");
}
//保存火车信息
void SaveTrainInfo(Link l)
{
FILE*fp ;
Node*p ;
int count=0,flag=1 ;
fp=fopen("c:\\train.txt","ab");
if(fp==NULL)
{
printf("the file can't be opened!");
return ;
}
p=l->next ;
while(p)
{
if(fwrite(p,sizeof(Node),1,fp)==1)
{
p=p->next ;
count++;
}
else
{
flag=0 ;
break ;
}
}
if(flag)
{
printf("the number of the record which have been saved is %d\n",count);
shoudsave=0 ;
}
fclose(fp);
}
int main()
{
system("COLOR 8a");
FILE*fp1,*fp2 ;
Node*p,*r ;
char ch1,ch2 ;
Link l ;
int sel ;
printf("请先登录!!!!!\n\n");
while(1){
if (Login())
{
l=(Node*)malloc(sizeof(Node));
l->next=NULL ;
r=l ;
fp1=fopen("c:\\train.txt","ab+");
if((fp1==NULL))
{
printf("can't open the file!");
return 0 ;
}
while(!feof(fp1))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp1)==1)
{
p->next=NULL ;
r->next=p ;
r=p ;
count1++;
}
}
fclose(fp1);
while(1)
{
system("cls");
menu();
printf("请选择: ");
scanf("%d",&sel);
system("cls");
switch(sel)
{
case 1 :
InsertTraininfo(l);break ;
case 2 :
QueryTrain(l);break ;
case 3 :
add();break ;
case 4:
calculate();break;
case 5 :
UpdateInfo(l);break ;
case 6 :
AdvicedTrains(l);break ;
case 7 :
SaveTrainInfo(l);break ;
case 8 :
return 0;
}
printf("\nplease press any key to continue.......");
getch();
}
}
else{
printf("请先注册!!!\n");
printf("按任意键继续\n");
getch();
Reg();
system("cls");
}
}
system("cls");
return 0;
}
来源:https://www.cnblogs.com/hqcao/archive/2012/11/27/2791457.html