读取号码到数组只能读出一组,比较后怎么返回重新比较?代码如下:#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>
#define SHMKEY 75
#define CLIENTONE 100
#define CLIENTTWO 150
#define CLIENTTHREE 200
int main()
{
void server();
void client();
void display();
FILE *fp;
if((fp=fopen("Info.txt","rw+"))==NULL)
{
printf("The film can not open!\n");
}
int choice,i;
char ch[100],c;
fgets(ch,12,fp);
display();
do
{
printf("Please choice a feature :\n");
scanf("%d",&choice);
fflush(stdin);
while(choice!=1&&choice!=2&&choice!=3&&choice!=4)
{
printf("Error input,please input again:\n");
scanf("%d",&choice);
fflush(stdin);
}
if(choice==4)
exit();
else
{
while((i=fork())==-1);
if(!i) server(ch);
else
{
while ((i=fork())==-1);
if(!i)
client(choice);
}
wait(0);
wait(0);
}
}
while(choice!=4);
return 0;
}
typedef struct addre
{
int makr;
char ch[50];
}addre;
addre *addr1,*addr2,*addr3;
int shmid,*addr;
void server(char phonenumber[5][11])
{
char ch1[20];
int shmid1,shmid2,shmid3,shmid4;
int i,m;
shmid=shmget(SHMKEY,1024,0777|IPC_CREAT);
addr=shmat(shmid,0,0);
*addr=-1;
while(*addr==-1);
if(*addr==1)
{
shmid1=shmget(CLIENTONE,1024,0777|IPC_CREAT);
addr1=shmat(shmid1,0,0);
printf("Please input your favorite cellphone number:\n");
addr1->makr=-1;
char n;
FILE *fp;
fp=fopen("Info.txt","rw+");
int choice,i;
char ch[100],c;
while(addr1->makr==-1);
if(addr1->makr==1)
{
for(i=0;i<5;++i)
{
if(strcmp(phonenumber[i],addr1->ch)==0)
{
fgets(ch,12,fp);
printf("The number has existed ,please input again!\n");
scanf("%s",&addr1->ch);
}
else if(i=4)
printf("The number is OK! Please pay cost 100 yuan!:\n");
}
}
addr1->makr=-1;
while(addr1->makr==-1);
if(addr1->makr==2)
printf("The deal,wish you happy!\n");
shmctl(shmid1,IPC_RMID,0);
}
if(*addr==2)
{
shmid2=shmget(CLIENTTWO,1024,0777|IPC_CREAT);
addr2=shmat(shmid2,0,0);
printf("Please input your cellphone :\n");
addr2->makr=-1;
while(addr2->makr==-1);
if(addr2->makr==1)
printf("Input Your Payroll:\n");
addr2->makr=-1;
while(addr2->makr==-1);
if(addr2->makr==2)
printf("Top-up complete!\n");
shmctl(shmid2,IPC_RMID,0);
}
if(*addr==3)
{
shmid3=shmget(CLIENTTHREE,1024,0777|IPC_CREAT);
addr3=shmat(shmid3,0,0);
printf("Please input your ID:\n");
addr3->makr=-1;
while(addr3->makr==-1);
if(addr3->makr==1)
printf("Please input your phone number:\n");
addr3->makr=-1;
while(addr3->makr==-1);
if(addr3->makr==2)
printf("To deal with complete, wish you happy!\n");
shmctl(shmid3,IPC_RMID,0);
}
shmctl(shmid,IPC_RMID,0);
}
void client(int choice)
{
int shmid1,shmid2,shmid3;
int payroll;
char ch1[20],ch2[50];
shmid=shmget(SHMKEY,1024,0777|IPC_CREAT);
addr=shmat(shmid,0,0);
while(*addr!=-1);
if(choice==1)
{
*addr=1;
shmid1=shmget(CLIENTONE,1024,0777|IPC_CREAT);
addr1=shmat(shmid1,0,0);
while(addr1->makr!=-1);
scanf("%s",addr1->ch);
addr1->makr=1;
while(addr1->makr!=-1);
printf("Already pay cos 100 yuan!\n");
addr1->makr=2;
}
if(choice==2)
{
*addr=2;
shmid2=shmget(CLIENTTWO,1024,0777|IPC_CREAT);
addr2=shmat(shmid2,0,0);
while(addr2->makr!=-1);
scanf("%s",ch1);
addr2->makr=1;
while(addr2->makr!=-1);
scanf("%d",&payroll);
addr2->makr=2;
}
if(choice==3)
{
*addr=3;
shmid3=shmget(CLIENTTHREE,1024,0777|IPC_CREAT);
addr3=shmat(shmid3,0,0);
while(addr3->makr!=-1);
scanf("%s",ch1);
addr3->makr=1;
while(addr3->makr!=-1);
scanf("%s",ch2);
addr3->makr=2;
}
exit(0);
}
void display()
{
printf("--------------------\n");
int i;
for(i=1;i<=15;++i)
{
if(i==3||i==6||i==9||i==12)
{
if(i==3)
printf("| 1.purchase Mobile phone card |\n");
if(i==6)
printf("| 2.Pre-paid phone: |\n");
if(i==9)
printf("| 3.To deal with net silver: |\n");
if(i==12)
printf("| 4.back: |\n");
}
else
printf("| |\n");
}
printf("-------------------\n");
}问题出在server()函数。
- 1 回答
- 1 关注
- 1788 浏览
添加回答
举报
0/150
提交
取消