一个在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')