futurezone 2011-01-05
一个在Linux下用C语言编写的购票系统,经测试是可以编译通过并执行的
服务器代码:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
struct my_msg
{
long int my_msg_type;
char text[256];
char text2[256];
char text3[256];
float time1;
float time2;
float price;
char addr1[30];
char addr2[30];
int n;
float mon;
int sum;
int f;
}msgbuf;
int main()
{//11111111111111
msgbuf.my_msg_type=1;
int i;
int len=10;
char add1[10][30];
char add2[10][30];
float tim1[10];
float tim2[10];
float pri[10];
int num[10];
int f;
strcpy(add1[0],"beijing");strcpy(add2[0],"shanghai");
strcpy(add1[1],"shanghai");strcpy(add2[1],"beijing");
strcpy(add1[2],"fuzhou");strcpy(add2[2],"guangzhou");
strcpy(add1[3],"tianjin");strcpy(add2[3],"nanjing");
strcpy(add1[4],"nanjing");strcpy(add2[4],"tianjin");
strcpy(add1[5],"chong qing");strcpy(add2[5],"fuzhou");
strcpy(add1[6],"fuzhou");strcpy(add2[6],"chongqing");
strcpy(add1[7],"guangzhou");strcpy(add2[7],"kunming");
strcpy(add1[8],"kunming");strcpy(add2[8],"guangzhou");
strcpy(add1[9],"nanchang");strcpy(add2[9],"hefei");
tim1[0]=9.00;tim2[0]=12.00;pri[0]=100.00;
tim1[1]=10.00;tim2[1]=12.00;pri[1]=100.00;
tim1[2]=7.00;tim2[2]=12.00;pri[2]=300.00;
tim1[3]=12.00;tim2[3]=15.00;pri[3]=500.00;
tim1[4]=22.00;tim2[4]=06.00;pri[4]=200.00;
tim1[5]=14.00;tim2[5]=20.00;pri[5]=500.00;
tim1[6]=16.00;tim2[6]=20.00;pri[6]=400.00;
tim1[7]=18.00;tim2[7]=22.00;pri[7]=300.00;
tim1[8]=14.00;tim2[8]=19.00;pri[8]=200.00;
tim1[9]=12.00;tim2[9]=18.00;pri[9]=400.00;
num[0]=10;
num[1]=10;num[2]=10;num[3]=10;num[4]=10;num[5]=10;num[6]=10;num[7]=10;num[8]=10;num[9]=10;
int running=1;
int msgid;
long int msg_to_receive=0;
msgid=msgget((key_t)1234,0666|IPC_CREAT);
if(msgid==-1)
{
printf("msgget failed!\n");
exit(1);
}
char co[30];
strcpy(co,"******");
char code[30];
int rr=1;
char ch;
char cho;
char or[20];
while(rr)
{
printf("please enter your code:\n");
scanf("%s",code);
printf("\n");
if(strncmp(code,co,6)==0)
{
printf("welcome!\n");
rr=0;
}
else
printf("worng code,please enter again.\n");
}
strcpy(msgbuf.text3,"start");
if(msgsnd(msgid,(void*)&msgbuf,sizeof(msgbuf),0)==-1)
printf("msgsnd failed.\n");
strcpy(msgbuf.text2,"mm");
while(running)
{///////////222222222222222
//if(msgsnd(msgid,(void*)&msgbuf,sizeof(msgbuf),0)==-1)
//printf("msgsnd failed.\n");
printf("Do you want to management the system? y/n.\n");
scanf("%c",&ch);
printf("\n");
printf("%c\n",ch);
if(ch=='y')
{/////////////////3333333333333333
printf("please choose your order:\n");
printf("checkall add delete \n");
getchar();
scanf("%s",or);
printf("\n");
if(strncmp(or,"checkall",8)==0)//checkall
{
printf("all the message is:\n");
for(i=0;i<len;i++)
{
printf("the %d one, from %s to %s, start time is %f ,arrive time is %f ,the price is %f ,the num is %d\n",i,add1[i],add2[i],
tim1[i],tim2[i],pri[i],num[i]);
printf("\n");
printf("\n");
strcpy(msgbuf.text2,"no");
if(msgsnd(msgid,(void*)&msgbuf,sizeof(msgbuf),0)==-1)
printf("msgsnd failed.\n");
}
}////////////////////////checkall
if(strncmp(or,"add",3)==0)///////////////////////////////add
{///////////////666666666
char cho;
int ss=1;
//while(ss)
//{///////////777777777777
printf("add the message. \n");
printf("please enter the following messages:from place arrive place from time arrive time price number.\n");
scanf("%s%s%f%f%f%d",add1[len],add2[len],&tim1[len],&tim2[len],&pri[len],&num[len]);
printf("\n");
len++;
strcpy(msgbuf.text2,"new");
strcpy(msgbuf.addr1,add1[len]);
strcpy(msgbuf.addr2,add2[len]);
msgbuf.time1=tim1[len];msgbuf.time2=tim2[len];msgbuf.price=pri[len];msgbuf.n=num[len];
if(msgsnd(msgid,(void*)&msgbuf,sizeof(msgbuf),0)==-1)
printf("msgsnd failed.\n");
printf("add successed.\n");
//scanf("%c",&cho);Do you want to add again? y/n
//getchar();
//scanf("%c",&cho);
//printf("\n");
//printf("ch=%c\n",cho);
//if(cho=='y')
//{///////////////////888888
//ss=1;
//}/////////////888
//else
//{//////////////9999999999
//ss=0;
//}///////////9999999999999999
///}//////////////777777
}/////////////66666666666/////////////////////////add
if(strncmp(or,"delete",6)==0)//////////////delete
{/////////////////10
char addre1[30];
char addre2[30];
int tt;
int qq=0;
int sss=1;
char choo;
char choos;
printf("delete the message.\n");
//while(sss)
//{/////////////////11
printf("please enter the from place and the arrive place\n");
scanf("%s%s",addre1,addre2);
getchar();
printf("Do you really want to delete the message? y/n\n");
scanf("%c",&choo);
printf("choo=%c\n",choo);
printf("\n");
if(choo=='y')
{///////////////////12
strcpy(msgbuf.text2,"delete");
for(i=0;i<len;i++)
{//////////////////13
if(strncmp(add1[i],addre1,strlen(addre1))==0&&strncmp(add2[i],addre2,strlen(addre2))==0)
{////////////////////////14
tt=i;
qq++;
}////////////////////////14
}/////////////////////////////13
if(qq==0)
printf("there is no the ticket.\n");
else if(qq>0)
{/////////////////////15
if(tt=len-1)
{///////////////////16
len--;
}/////////////16
else
{/////////////17
for(i=tt;i<len-1;i++)
{////////////18
strcpy(add1[i],add1[i+1]);
strcpy(add2[i],add2[i+1]);
tim1[i]=tim1[i+1];
tim2[i]=tim2[i+1];
pri[i]=pri[i+1];
num[i]=num[i+1];
}////////////////////////18
len--;
}///////////////17
printf("delete successed.\n");
}//////////////////////15
}////////////////////////12
/*printf("Do you want to delete again?.y/n\n");
getchar();
scanf("%c",&choos);
printf("\n");
if(choos=='n')
sss=0;*/
if(msgsnd(msgid,(void*)&msgbuf,sizeof(msgbuf),0)==-1)
printf("msgsnd failed.\n");
}/////////////////11//////////delete
//}/////////////10
}/////////////////////333333333
//}
if(ch=='n')
{
printf("msgbuf.text2=%s",msgbuf.text2);
if(strncmp(msgbuf.text2,"mm",2)==0)
{
strcpy(msgbuf.text2,"no");
if(msgsnd(msgid,(void*)&msgbuf,sizeof(msgbuf),0)==-1)
printf("msgsnd failed.\n");
if(msgrcv(msgid,(void*)&msgbuf,sizeof(msgbuf),msg_to_receive,0)==-1)
{
printf("msgrcv failed!\n");
exit(1);
}
printf("msgbuf.text2=%s\n",msgbuf.text2);
printf("msgbuf.text=%s\n",msgbuf.text);
if(strncmp(msgbuf.text,"buy",3)==0)
{
printf("the client buy the ticket.\n");
printf("the %d one,the from place is %s, the arrive place is %s,the number is %d,the total money is %f\n",msgbuf.f,msgbuf.addr1,msgbuf.addr2,msgbuf.n,msgbuf.mon);
}
if(strncmp(msgbuf.text,"return",6)==0)
{
printf("the client return the ticket.\n");
printf("the from place is %s, the arrive place is %s\n",msgbuf.addr1,msgbuf.addr2);
}
if(strncmp(msgbuf.text,"end",3)==0)
{
running=0;
}
}
//}
}
}//////////////////222
if(msgctl(msgid,IPC_RMID,0)==-1)
{
printf("msgct(IPC_RMID)failed!\n");
exit(1);
}
return 0;
}////////////////1111111