#include #include #include #include FILE *in; typedef struct { int n[11]; }POL; POL p; POL processa_LOOP() { char pal[20]; char pal2[20]; int i; int num; POL temp; POL stemp; for (i=0;i<=10;i++) { temp.n[i]=0; stemp.n[i]=0; } fscanf(in,"%s",pal); if (strcmp(pal,"n")==0) { fscanf(in,"%s",pal2); while (strcmp(pal2,"END")!=0) { if (strcmp(pal2,"OP")==0) { fscanf(in,"%d",&num); stemp.n[0]+=num; } if (strcmp(pal2,"LOOP")==0) { temp=processa_LOOP(); for(i=0;i<=10;i++) { stemp.n[i]+=temp.n[i]; } } fscanf(in,"%s",pal2); } for(i=9;i>=0;i--) { stemp.n[i+1]=stemp.n[i]; } stemp.n[0]=0; return(stemp); } else { fscanf(in,"%s",pal2); while (strcmp(pal2,"END")!=0) { if (strcmp(pal2,"OP")==0) { fscanf(in,"%d",&num); stemp.n[0]+=num; } if (strcmp(pal2,"LOOP")==0) { temp=processa_LOOP(); for(i=0;i<=10;i++) { stemp.n[i]+=temp.n[i]; } } fscanf(in,"%s",pal2); } num=atoi(pal); for(i=0;i<=10;i++) { stemp.n[i]*=num; } return(stemp); } } int main() { int i,j; char pal[20]; int n; POL temp,stemp; int num; char pal2[20]; int aux; in=fopen("complex.in","r"); fscanf(in,"%d",&n); for(i=1;i<=n;i++) { for (j=0;j<=10;j++) { temp.n[j]=0; stemp.n[j]=0; } fscanf(in,"%s",pal); fscanf(in,"%s",pal2); while (strcmp(pal2,"END")!=0) { if (strcmp(pal2,"OP")==0) { fscanf(in,"%d",&num); stemp.n[0]+=num; } if (strcmp(pal2,"LOOP")==0) { temp=processa_LOOP(); for(j=0;j<=10;j++) { stemp.n[j]+=temp.n[j]; } } fscanf(in,"%s",pal2); } aux=0; printf("Program #%d\n",i); printf("Runtime = "); for (j=10;j>=2;j--) { if (aux==0) { if (stemp.n[j]!=0) { if (stemp.n[j]==1) { printf("n^%d",j); } else { printf("%d*n^%d",stemp.n[j],j); } aux=1; } } else { if (stemp.n[j]!=0) { if (stemp.n[j]==1) { printf("+n^%d",j); } else { printf("+%d*n^%d",stemp.n[j],j); } } }} if (aux==0) { if (stemp.n[1]!=0) { if (stemp.n[1]==1) { printf("n"); } else { printf("%d*n",stemp.n[1]); } aux=1; } } else { if (stemp.n[1]!=0) { if (stemp.n[1]==1) { printf("+n"); } else { printf("+%d*n",stemp.n[1]); } } } if (aux==0) { if (stemp.n[0]!=0) { printf("%d",stemp.n[0]); aux=1; } } else { if (stemp.n[0]!=0) printf("+%d",stemp.n[0]); } printf("\n\n"); } fclose(in); return 0; }