CSDN博客

img Coolingverse

[C] ACCP一期大作业

发表于2004/6/24 21:37:00  858人阅读

#include
#include
#include
typedef struct
{
 char name[31];
 char add[51];
 long tel;
}consumer_det;
void sort(consumer_det *scr,int y)
{
 FILE *fp;
 int i,j,k,x;
 consumer_det temp[30];
 if((fp=fopen("telefon.dat","rb"))==NULL)
 {
  printf("Cannot open this file!");
  exit(0);
 }
 for(i=0;!feof(fp);i++)
 {
  fread(&temp[i],sizeof(consumer_det),1,fp);
  if(feof(fp))
  {
   if(y==0) x=i+1;
   if(y==1) x=i-1;
   break;
  }
  if(y==0)
   if(!strcmp(temp[i].name,(*scr).name) && !strcmp(temp[i].add,(*scr).add) && temp[i].tel==(*scr).tel)
   {
    printf("/n/tThis consumer already existed!/n");    
    return;
   }
 }
 fclose(fp);
 if((fp=fopen("telefon.dat","wb"))==NULL)
 {
  printf("Cannot open this file!");
  exit(0);
 }
 if(y==0) //插入记录
 {
  for(k=i;(strcmp(temp[k].name,(*scr).name))>=0 && k>=0 ;k--)
  {
   temp[k+1]=temp[k];
  }
  temp[k+1]=(*scr);
 }
 if(y==1) //删除记录
 {
  for(k=0; k<=i ;k++)
  {
   if((strcmp(temp[k].name,(*scr).name))>=0)
    temp[k]=temp[k+1];
  }
 } 
 for(j=0;j  fwrite(&temp[j],sizeof(consumer_det),1,fp);
 fclose(fp); 
}
void input()
{
 int flag='Y';
 consumer_det det;
 while(flag=='Y'||flag=='y') 
 {
  system("cls"); 
  printf("/t/tADD NEW SUBSCRIBER DETAILS");
  do
  {   
   printf("/n/nEnter Subscriber name ( upte 30 chars ): ");
   fflush(stdin);
   scanf("%s",det.name);
   if(strlen(det.name)>30)
    printf("/n/tSubscriber name must upte 30 chars");
  }while(strlen(det.name)>30);
  do
  {
   printf("/n/nEnter Subscriber address ( upte 50 chars ): ");
   fflush(stdin);
   scanf("%s",det.add );
   if(strlen(det.add)>50)
    printf("/n/tSubscriber address must upte 50 chars");
  }while(strlen(det.add)>50);
  do
  {   
   printf("/n/nEnter Subscriber number: ");
   fflush(stdin);
   scanf("%ld",&det.tel);
   if(det.tel<4000000 || det.tel>4999999)
    printf("/n/tSubscriber telephone number must be between 4000000 and 4999999");
  }while(det.tel<4000000 || det.tel>4999999);
  sort(&det,0);
  printf("/n/nContinue ? (Y/N)");
  fflush(stdin);
  scanf("%c",&flag);
 }
}
void displayall(char scr,int x)
{
 FILE *fp;
 int i,flag=1,pointadd1,pointadd2;
 consumer_det temp;
 long temp_tel;
 char temp_name[31];
 if((fp=fopen("telefon.dat","rb"))==NULL)
 {
  printf("Cannot open this file!");
  exit(0);
 }
 system("cls");
 if(scr=='t')
 {
  do
  {   
   printf("/n/nEnter Subscriber number: ");
   fflush(stdin);
   scanf("%ld",&temp_tel);
   if(temp_tel<4000000 || temp_tel>4999999)
    printf("/n/tSubscriber telephone number must be between 4000000 and 4999999");
  }while(temp_tel<4000000 || temp_tel>4999999);
 }
 if(scr=='n')
 {  
  do
  {
   printf("/n/nEnter Subscriber name ( upte 30 chars ): ");
   fflush(stdin);
   scanf("%s",temp_name);
   if(strlen(temp_name)>30)
    printf("/n/tSubscriber name must upte 30 chars");
  }while(strlen(temp_name)>30);
 }
 system("cls");
 if(scr=='a')
 {
  printf("/n/tCONSUMER TOTOAL:");
  printf("/n/nNumber/t/tName/t/tAddress/t/t/tTelephone/tFileAdd/n");
 }
 for(i=0;!feof(fp);i++)
 {    
  fseek(fp,i*sizeof(consumer_det),0);
  fread(&temp,sizeof(consumer_det),1,fp);
  pointadd1=ftell(fp);
  if(pointadd1==pointadd2)
   break;
  if(temp.tel >4999999||temp.tel<4000000)
   break;  
  if(scr=='t')
  {
   if(temp_tel==temp.tel)
   {
    printf("/n/tREQUESTED SUBSCRIBER DETAILS/n/nName: %s/nAddress: %s/nTelephone number: %d/n",temp.name ,temp.add ,temp.tel );
    flag=0;
   }
  }
  if(scr=='n')
  {
   if(!strcmp(temp_name,temp.name))
   {
    printf("/n/tREQUESTED SUBSCRIBER DETAILS/n/nName: %s/nAddress: %s/nTelephone number: %d/n",temp.name ,temp.add ,temp.tel );
    flag=0;
    if(x==1)
     sort(&temp,1);
    if(x==2)
    {
     fflush(stdin);
     getchar();
     sort(&temp,1);
     input();
    }
   }
  }
  if(scr=='a')
  {
   printf("/nRecord%-10d%-16s%-24s %-16ld%-d",i+1,temp.name ,temp.add ,temp.tel,ftell(fp));
   flag=0;
  }
  pointadd2=ftell(fp);
 } 
 if(flag)
 {
  printf("/n/t/tThe record is not exist!");
 }
 fflush(stdin);
 getchar();
 fclose(fp);
}
void main()
{
 int choice,flag=1;
 while(flag)
 {
  system("cls");
  printf("S19305/nTELEPHONE DIRECTORY SYSTEM/n/n/t1. Add new subscriber details/n/t2. Modify existing subscriber details/n/t3. Delete existing subscriber details/n/t4. Display subscriber details based on telephone number/n/t5. Display subscriber details based on subscriber name/n/t6. View Directory/n/t7. Quit/n/nEnter choice: ");
  scanf("%d",&choice);
  if(choice==1)
   input();
  if(choice==2)
   displayall('n',2);
  if(choice==3)
   displayall('n',1);
  if(choice==4)
   displayall('t',0);
  if(choice==5)
   displayall('n',0);
  if(choice==6)
   displayall('a',0);
  if(choice==7)
   exit(0);
  if(choice>7||choice<1)
  {
   printf("Error: Please typing integer( 1 - 7 ).");
   fflush(stdin);
   getchar();     
  }
 }
}
0 0

相关博文

我的热门文章

img
取 消
img