2010-04-27 69 views
-1
#include<stdio.h> 

#include<ctype.h> 

#include<string.h> 


int main() 

{ 

    int i=0,j,k,lines_count[2]={1,1},operand_count[2]={0},operator_count[2]={0},uoperator_count[2]={0},control_count[2]={0,0},cl[13]={0},variable_dec[2]={0,0},l,p[2]={0},ct,variable_used[2]={0,0},constant_count[2],s[2]={0},t[2]={0}; 

    char a,b[100],c[100]; 

    char d[100]={0}; 

    j=30; 



    FILE *fp1[2],*fp2; 


    fp1[0]=fopen("program1.txt","r"); 

    fp1[1]=fopen("program2.txt","r"); //the source file is opened in read only mode which will passed through the lexer 

    fp2=fopen("ccv1ouput.txt","wb"); 

    //now lets remove all the white spaces and store the rest of the words in a file 


    if(fp1[0]==NULL) 

    { 

     perror("failed to open program1.txt"); 

     //return EXIT_FAILURE; 

    } 

     if(fp1[1]==NULL) 

    { 

     perror("failed to open program2.txt"); 

     //return EXIT_FAILURE; 

    } 

    i=0; 

    k=0; 

    ct=0; 

while(ct!=2) 

{ 

    while(!feof(fp1[ct])) 

    { 


     a=fgetc(fp1[ct]); 


     if(a!=' '&&a!='\n') 

     { 

         if (!isalpha(a) && !isdigit(a)) 

           { 

           switch(a) 

            { 

            case '+':{ 

               i=0; 

               cl[0]=1; 

               operator_count[ct]=operator_count[ct]+1;break;} 

            case '-':{ cl[1]=1; 

               operator_count[ct]=operator_count[ct]+1;i=0;break;} 

            case '*':{ cl[2]=1; 

               operator_count[ct]=operator_count[ct]+1;i=0;break;} 

            case '/':{ cl[3]=1; 

               operator_count[ct]=operator_count[ct]+1;i=0;break;} 

            case '=':{a=fgetc(fp1[ct]); 

               if (a=='='){cl[4]=1; 

                 operator_count[ct]=operator_count[ct]+1; 

                 operand_count[ct]=operand_count[ct]+1;} 

               else 

                 { cl[5]=1; 

                  operator_count[ct]=operator_count[ct]+1; 

                  operand_count[ct]=operand_count[ct]+1; 

                  ungetc(1,fp1[ct]); 

                 } 

               break;} 

            case '%':{ cl[6]=1; 

               operator_count[ct]=operator_count[ct]+1;i=0;break;} 

            case '<':{ 

               a=fgetc(fp1[ct]); 

               if (a=='=') 

                 {cl[7]=1; 

                 operator_count[ct]=operator_count[ct]+1;} 

               else 

                 { cl[8]=1; 

                  operator_count[ct]=operator_count[ct]+1; 

                  ungetc(1,fp1[ct]); 

                 } 

               break; 

              } 


            case '>':{ ; 

               a=fgetc(fp1[ct]); 

               if (a=='='){cl[9]=1; 

                 operator_count[ct]=operator_count[ct]+1;} 

               else 

                 { cl[10]=1; 

                  operator_count[ct]=operator_count[ct]+1; 

                  ungetc(1,fp1[ct]); 

                 } 

               break;} 

            case '&':{ cl[11]=1; 

               a=fgetc(fp1[ct]); 

               operator_count[ct]=operator_count[ct]+1; 

               operand_count[ct]=operand_count[ct]+1; 

               variable_used[ct]=variable_used[ct]-1; 

               break; 

              } 

            case '|':{ cl[12]=1; 

               a=fgetc(fp1[ct]); 

               operator_count[ct]=operator_count[ct]+1; 

               operand_count[ct]=operand_count[ct]+1; 

               variable_used[ct]=variable_used[ct]-1; 

               break; 

              } 

            case '#':{ while(a!='\n') 

               { 

                 a=fgetc(fp1[ct]); 

               } 

              } 




            } 

         } 

        else 

         { 

           d[i]=a; 

           i=i+1; 

           k=k+1; 

         } 







     } 

     else 

     { 

        //printf("%s \n",d); 

        if((strcmp(d,"if")==0)){ 

            memset (d, 0, 100); 

            i=0; 

            control_count[ct]=control_count[ct]+1; 

            } 

     else if(strcmp(d,"then")==0){ 

            i=0;memset (d, 0, 100);control_count[ct]=control_count[ct]+1;} 

     else if(strcmp(d,"else")==0){ 

              i=0;memset (d, 0, 100);control_count[ct]=control_count[ct]+1;} 



     else if(strcmp(d,"while")==0){ 

              i=0;memset (d, 0, 100);control_count[ct]=control_count[ct]+1;} 


     else if(strcmp(d,"int")==0){ 

              while(a != '\n') 

               { 

                 a=fgetc(fp1[ct]); 

                 if (isalpha(a)) 

                  variable_dec[ct]=variable_dec[ct]+1; 

               } 

               memset (d, 0, 100); 

               lines_count[ct]=lines_count[ct]+1; 

              } 




     else if(strcmp(d,"char")==0){while(a != '\n') 

               { a=fgetc(fp1[ct]); 

                 if (isalpha(a)) 

                  variable_dec[ct]=variable_dec[ct]+1; 

               } 

               memset (d, 0, 100); 

               lines_count[ct]=lines_count[ct]+1; 

               } 

     else if(strcmp(d,"float")==0){while(a != '\n') 

               { a=fgetc(fp1[ct]); 

                 if (isalpha(a)) 

                  variable_dec[ct]=variable_dec[ct]+1; 

               } 

               memset (d, 0, 100); 

               lines_count[ct]=lines_count[ct]+1; 

               } 

     else if(strcmp(d,"printf")==0){while(a!='\n') 

               a=fgetc(fp1[ct]); 

              memset(d,0,100); 

              } 

     else if(strcmp(d,"scanf")==0){while(a!='\n') 

              a=fgetc(fp1[ct]); 

              memset(d,0,100);} 



      else if (isdigit(d[i-1])) 

        { 

         memset (d, 0, 100); 

         i=0; 

         constant_count[ct]=constant_count[ct]+1; 

         operand_count[ct]=operand_count[ct]+1; 

        } 

      else if (isalpha(d[i-1]) && strcmp(d,"int")!=0 && strcmp(d,"char")!=0 && strcmp(d,"float")!=0 && (strcmp(d,"if")!=0) && strcmp(d,"then")!=0 && strcmp(d,"else")!=0 && strcmp(d,"while")!=0 && strcmp(d,"printf")!=0 && strcmp(d,"scanf")!=0) 

        { 

         memset (d, 0, 100); 

         i=0; 

         operand_count[ct]=operand_count[ct]+1; 



        } 

      else if(a=='\n') 

      { 

      lines_count[ct]=lines_count[ct]+1; 

      memset (d, 0, 100); 

      } 



     } 




    } 

fclose(fp1[ct]); 

operand_count[ct]=operand_count[ct]-5; 

variable_used[0]=operand_count[0]-constant_count[0]; 

variable_used[1]=operand_count[1]-constant_count[1]; 

for(j=0;j<12;j++) 

uoperator_count[ct]=uoperator_count[ct]+cl[j]; 

fprintf(fp2,"\n statistics of program %d",ct+1); 

fprintf(fp2,"\n the no of lines ---> %d",lines_count[ct]); 

fprintf(fp2,"\n the no of operands --->%d",operand_count[ct]); 

fprintf(fp2,"\n the no of operator --->%d",operator_count[ct]); 

fprintf(fp2,"\n the no of control statments --->%d",control_count[ct]); 

fprintf(fp2,"\n the no of unique operators --->%d",uoperator_count[ct]); 

fprintf(fp2,"\n the no of variables declared--->%d",variable_dec[ct]); 

fprintf(fp2,"\n the no of variables used--->%d",variable_used[ct]); 

fprintf(fp2,"\n ---------------------------------"); 

fprintf(fp2,"\n \t \t \t"); 

ct=ct+1; 

} 


t[0]=lines_count[0]+control_count[0]+uoperator_count[0]; 

t[1]=lines_count[1]+control_count[1]+uoperator_count[1]; 



s[0]=operator_count[0]+operand_count[0]+variable_dec[0]+variable_used[0]; 

s[1]=operator_count[1]+operand_count[1]+variable_dec[1]+variable_used[1]; 

fprintf(fp2,"\n the time complexity of program 1 is %d",t[0]); 

fprintf(fp2,"\n the time complexity of program 2 is %d",t[1]); 

fprintf(fp2,"\n the space complexity of program 1 is %d",s[0]); 

fprintf(fp2,"\n the space complexity of program 2 is %d",s[1]); 

if((t[0]>t[1]) && (s[0] >s[1])) 

fprintf(fp2,"\n the efficiency of program 2 is greater than program 1"); 

else if(t[0]<t[1] && s[0] < s[1]) 

fprintf(fp2,"\n the efficiency of program 1 is greater than program 2 "); 

else if (t[0]+s[0] > t[1]+s[1]) 

fprintf(fp2,"\n the efficiency of program 1 is greater than program 2"); 

else if (t[0]+s[0] < t[1]+s[1]) 

fprintf(fp2,"\n the efficiency of program 2 is greater than program 1"); 

else if (t[0]+s[0] == t[1]+s[1]) 

fprintf(fp2,"\n the efficiency of program 1 is equal to that of program 2"); 



fclose(fp2); 


return 0; 

} 

找出lexems和没有一个文本文件的行的代码进行比较基本上两个C代码和找出没有。变量声明,使用,不。的控制陈述,没有。线和没有。独特的运算符和操作数,以便找出文本文件program1.txt和program2.txt中给出的两个程序的时间复杂度和空间复杂度...误差在用C

可以说program1.txt是这样的

#include<stdio.h> 
#include<math.h> 
int main() 
{ 
FILE *fp; 
fp=fopen("output.txt","w"); 
long double t,y=0,x=0,e=5,f=1,w=1; 
for (t=0;t<10;t=t+0.01) 
{ 
//if (isnan(y) || isinf(y)) 
//break; 
fprintf(fp,"%ld\t%ld\n",y,x); 

y = y + ((e*(1 - (x*x))*y) - x + f*cos(w*0.1))*0.1; 
x = x + y*0.1; 
} 
fclose(fp); 
return (0); 
} 

我还没有缩进它只是一个文本文件。但是我的输出是完全错误的。它无法找到我需要的任何输出。这个错误在哪里?算法看起来不错,我无法弄清楚。

+0

固定的源代码(缩进和行间距)的格式可能会有所帮助。 – 2010-04-27 10:23:51

+0

没有功课。只是一个学习曲线.. – Hick 2010-04-27 10:25:59

+0

好吧,至少告诉我为什么我的线路不会来rt? – Hick 2010-04-27 10:29:01

回答

0

你需要编写一个被赋予了FILE *(也许文件名太)的功能,并读取和分析该文件。您可以创建一个结构来存储统计信息;你可能会把它传递给函数,或者你可能会让函数返回它。您可能会创建一些额外的函数来读取文件的各个部分 - 读取带引号的字符串,注释和单引号字符都是可能的候选项。然后

你的主要程序将调用分析功能两次,一次为每个文件,收集统计数据的结构,然后你就可以在比较格式(可能是另一种功能)打印。

在代码中,你必须:

int i=0,j,k,lines_count[2]={1,1},operand_count[2]={0},operator_count[2]={0},uoperator_count[2]={0},control_count[2]={0,0},cl[13]={0},variable_dec[2]={0,0},l,p[2]={0},ct,variable_used[2]={0,0},constant_count[2],s[2]={0},t[2]={0}; 

这应该被分隔成了多行以提高可读性。事实上,我会重复关键字int,并让每个变量独立:

int i=0; 
int j; 
int k; 
int lines_count[2]={1,1}; 
int operand_count[2]={0}; 
int operator_count[2]={0}; 
int uoperator_count[2]={0}; 
int control_count[2]={0,0}; 
int cl[13]={0}; 
int variable_dec[2]={0,0}; 
int l; 
int p[2]={0}; 
int ct; 
int variable_used[2]={0,0}; 
int constant_count[2]; 
int s[2]={0}; 
int t[2]={0}; 

我们现在可以看到,你有你的数组初始化不一致。结果是一样的(无论如何,缺失的值都被视为零)。在读取任何内容之前,文件中只有一行是很奇怪的。在每一个陈述之间也没有空白。这使得阅读代码非常困难。大多数双元素数组将成为我设计中统计结构的一部分。