Advertisement
ivan4ov4

Valkata

Jan 30th, 2018
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define SIZE 10000
  4.  
  5. int dalipasva (char *t, const char *duma, int n)
  6. {
  7.     for(int i=0; i<n; i++)
  8.     {
  9.         if(t[i]!=duma[i])return 0;
  10.     }
  11.     return 1;
  12. }
  13.  
  14. char* Chetene (FILE *f,char *rez)
  15. {
  16.  
  17.     char u;
  18.     int i;
  19.     for(i=0; (u=fgetc(f))!=EOF; i++)
  20.     {
  21.         rez[i]=u;
  22.     }
  23.     rez[i+1]='\0';
  24.     return rez;
  25. }
  26.  
  27. int dalineeotime(char x)
  28. {
  29.     if(x=='_')return 0;
  30.     if(x>='a'&&x<='z')return 0;
  31.     if(x>='A'&&x<='Z')return 0;
  32.     if(x>='0'&&x<='9')return 0;
  33.     return 1;
  34. }
  35.  
  36. void ifelse(char *text,int dg,int gg,int &brif, int &brel)
  37. {
  38.     for(int i=dg; i<=gg; i++)
  39.     {
  40.         if(dalipasva(&text[i],"if",2)&&dalineeotime(text[i-1])&&dalineeotime(text[i+2]))brif++;
  41.         else if(dalipasva(&text[i],"else",4)&&dalineeotime(text[i-1])&&dalineeotime(text[i+4]))brel++;
  42.     }
  43. }
  44.  
  45. void razpredelenie(char* text,int &brkom, int &brif, int &brel)
  46. {
  47.     int dg=0,i;
  48.     for(i=0; text[i]!='\0'; i++)
  49.     {
  50.             if(dalipasva(&text[i],"//",2))
  51.             {
  52.                
  53.                 ifelse(text,dg,i-1,brif,brel);
  54.                 dg=i+2;
  55.             }
  56.             else
  57.             {
  58.                 if(dalipasva(&text[i],"/*",2))
  59.                 {
  60.                     ifelse(text,dg,i-1,brif,brel);
  61.                     dg=i+2;
  62.                    
  63.                 }
  64.             }
  65.  
  66.     }
  67.     ifelse(text,dg,i-1,brif,brel); //obira posledniq nezavurshen interval
  68. }
  69.  
  70. int main () {
  71.     int mode;
  72.     do {
  73.         printf("1 - Reading program from file and Write final result on the input file name\n");
  74.         printf("2 - Reading program from file and Printing result from Console\n");
  75.         printf("3 - Reading program from Console and Writing final result from input file name \n");
  76.         printf("4 - Readind program from Console and Printig result from Console\n");
  77.         printf("5 - Exit\n");
  78.         scanf("%d",&mode);
  79.         FILE *f1,*f2;
  80.         char masiv[SIZE],rez[SIZE];
  81.         int brif=0,brel=0,brk=0;
  82.         switch(mode){
  83.             case 1:
  84.                 printf("Enter file name for Read: ");
  85.                 scanf("%s",masiv);
  86.                 f1=fopen(masiv,"r");
  87.                
  88.                 printf("Enter file name for Write: ");
  89.                 scanf("%s",masiv);
  90.                 f2=fopen(masiv,"w");
  91.                
  92.                 razpredelenie(Chetene(f1,rez),brk,brif,brel);
  93.                 fprintf(f2,"The if/else operators are ");
  94.                 sprintf(masiv,"%d",brel);
  95.                 fprintf(f2,masiv);
  96.                 fputc('\n',f2);
  97.                 fprintf(f2,"The solo if operators are ");
  98.                 sprintf(masiv,"%d",brif-brel);
  99.                 fprintf(f2,masiv);
  100.                 fputc('\n',f2);
  101.                 fclose(f1);
  102.                 fclose(f2);
  103.                 return 0;
  104.             case 2:
  105.                 printf("Enter file name for Read: ");
  106.                 scanf("%s",masiv);
  107.                 f1=fopen(masiv,"r");
  108.                
  109.                 f2=stdout;
  110.                
  111.                 razpredelenie(Chetene(f1,rez),brk,brif,brel);
  112.                 fprintf(f2,"The if/else operators are ");
  113.                 sprintf(masiv,"%d",brel);
  114.                 fprintf(f2,masiv);
  115.                 fputc('\n',f2);
  116.                 fprintf(f2,"The solo if operators are ");
  117.                 sprintf(masiv,"%d",brif-brel);
  118.                 fprintf(f2,masiv);
  119.                 fputc('\n',f2);
  120.                 fclose(f1);
  121.                 fclose(f2);
  122.                 return 0;
  123.             case 3:
  124.                
  125.                 f1=stdin;
  126.                
  127.                 printf("Enter file name for Write: ");
  128.                 scanf("%s",masiv);
  129.                 f2=fopen(masiv,"w");
  130.                 printf("You now is Read Mod forom Console \n Insert Contr + Z to Stop Reading \n");
  131.                
  132.                 razpredelenie(Chetene(f1,rez),brk,brif,brel);
  133.                 fprintf(f2,"The if/else operators are ");
  134.                 sprintf(masiv,"%d",brel);
  135.                 fprintf(f2,masiv);
  136.                 fputc('\n',f2);
  137.                 fprintf(f2,"The solo if operators are ");
  138.                 sprintf(masiv,"%d",brif-brel);
  139.                 fprintf(f2,masiv);
  140.                 fputc('\n',f2);
  141.                 fclose(f1);
  142.                 fclose(f2);
  143.                 return 0;
  144.             case 4:
  145.                
  146.                 f1=stdin;
  147.                
  148.                 f2=stdout;
  149.                
  150.                 printf("You now is Read Mod forom Console \n Insert Contr + Z to Stop Reading \n");
  151.                
  152.                 razpredelenie(Chetene(f1,rez),brk,brif,brel);
  153.                 fprintf(f2,"The if/else operators are ");
  154.                 sprintf(masiv,"%d",brel);
  155.                 fprintf(f2,masiv);
  156.                 fputc('\n',f2);
  157.                 fprintf(f2,"The solo if operators are ");
  158.                 sprintf(masiv,"%d",brif-brel);
  159.                 fprintf(f2,masiv);
  160.                 fputc('\n',f2);
  161.                 fclose(f1);
  162.                 fclose(f2);
  163.                 return 0;
  164.                 break;
  165.             case 5:
  166.                 break;
  167.             default:
  168.                 printf("Wrong choise\n");
  169.                 break;
  170.         }
  171.        
  172.     } while (mode != 5);
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement