sabertooth09

project x%10==0

Nov 21st, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. struct cnct
  4. {
  5.     char name[1009];
  6.     char mail[1009];
  7.     char number_1[1009];
  8.     char number_2[1009];
  9. };
  10.  
  11. struct cnct a[1000];
  12. int size;
  13.  
  14. void sort(int sz)
  15. {
  16.     int ipos=0,jpos=0;
  17.     for(ipos=0; ipos<sz; ipos++)
  18.     {
  19.         for(jpos=ipos; jpos<sz; jpos++)
  20.         {
  21.             if(strcmp(a[ipos].name,a[jpos].name)>0)
  22.             {
  23.                 struct cnct t=a[ipos];
  24.                 a[ipos]=a[jpos];
  25.                 a[jpos]=t;
  26.             }
  27.         }
  28.     }
  29. }
  30.  
  31. int main()
  32. {
  33.     FILE *inp;
  34.     FILE *out;
  35.     FILE *usr;
  36.     inp=fopen("input.txt","r");
  37.     usr=fopen("usr.txt","r");
  38.     int n,i=0,j,command,sub,size=0;
  39.     struct cnct usr_cnct;
  40.     char x[1009];
  41.  
  42.     while(fscanf(inp,"%s",&a[i].name)!=EOF)
  43.     {
  44.         fscanf(inp,"%s",&a[i].mail);
  45.         fscanf(inp,"%s",&a[i].number_1);
  46.         fscanf(inp,"%s",&a[i].number_2);
  47.         i++;
  48.         size++;
  49.     }
  50.     sort(size);
  51.     fclose(inp);
  52.     while(1)
  53.     {
  54.         printf("Input Command\n1.Append\n2.Delete\n3.Display\n");
  55.         //fgetchar();
  56.         scanf("%d",&command);
  57.         if(command==1 || command==2)
  58.         {
  59.             printf("Input\n1.From file\n2.From console\n");
  60.             scanf("%d",&sub);
  61.         }
  62.         if(command==1)
  63.         {
  64.             if(sub==1)
  65.             {
  66.                 fscanf(usr,"%s",&usr_cnct.name);
  67.                 fscanf(usr,"%s",&usr_cnct.mail);
  68.                 fscanf(usr,"%s",&usr_cnct.number_1);
  69.                 fscanf(usr,"%s",&usr_cnct.number_2);
  70.             }
  71.             else
  72.             {
  73.                 scanf("%s",&usr_cnct.name);
  74.                 scanf("%s",&usr_cnct.mail);
  75.                 scanf("%s",&usr_cnct.number_1);
  76.                 scanf("%s",&usr_cnct.number_2);
  77.             }
  78.             int is=0,f_pos=size,c,qw;
  79.             for(is=0; is<size; is++)
  80.             {
  81.                 c=strcmp(a[is].name,usr_cnct.name);
  82.                 if(c>=0)
  83.                 {
  84.                     f_pos=is;
  85.                     break;
  86.                 }
  87.             }
  88.             if(strcmp(a[f_pos].name,usr_cnct.name)==0)
  89.                 a[f_pos]=usr_cnct;
  90.             else
  91.             {
  92.                 for(is=size-1; is>=f_pos; is--)
  93.                     a[is+1]=a[is];
  94.                 a[f_pos]=usr_cnct;
  95.                 size++;
  96.             }
  97.  
  98.             inp=fopen("input.txt","w");
  99.             for(i=0; i<size; i++)
  100.             {
  101.                 fprintf(inp,"%s\n",a[i].name);
  102.                 fprintf(inp,"%s\n",a[i].mail);
  103.                 fprintf(inp,"%s\n",a[i].number_1);
  104.                 fprintf(inp,"%s\n",a[i].number_2);
  105.             }
  106.             fclose(inp);
  107.         }
  108.         else if(command==2)
  109.         {
  110.             if(sub==1)
  111.                 fscanf(usr,"%s",&x);
  112.             else
  113.                 scanf("%s",&x);
  114.             int del_pos=-1,is,c;
  115.             for(is=0; is<size; is++)
  116.             {
  117.                 c=strcmp(a[is].name,x);
  118.                 if(c==0)
  119.                 {
  120.                     del_pos=is;
  121.                     break;
  122.                 }
  123.             }
  124.             if(del_pos!=-1)
  125.             {
  126.                 for(is=del_pos; is<size; is++)
  127.                     a[is]=a[is+1];
  128.                 size--;
  129.             }
  130.             inp=fopen("input.txt","w");
  131.             for(i=0; i<size; i++)
  132.             {
  133.                 fprintf(inp,"%s\n",a[i].name);
  134.                 fprintf(inp,"%s\n",a[i].mail);
  135.                 fprintf(inp,"%s\n",a[i].number_1);
  136.                 fprintf(inp,"%s\n",a[i].number_2);
  137.             }
  138.             fclose(inp);
  139.         }
  140.         else if(command==3)
  141.         {
  142.             for(i=0; i<size; i++)
  143.             {
  144.                 printf("%s\n",a[i].name);
  145.                 printf("%s\n",a[i].mail);
  146.                 printf("%s\n",a[i].number_1);
  147.                 printf("%s\n\n",a[i].number_2);
  148.             }
  149.             printf("\n");
  150.         }
  151.  
  152.         else
  153.         {
  154.             printf("Thanks\n");
  155.             break;
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment