View Code of Problem 11

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define N 1000
int main()
{
    int t,length;
    int k=1;
    while(scanf("%d",&t)!=EOF&&t!=0){
        getchar();
        printf("Case #%d:\n", k++);
        while(t--){
            char a[1000];
            gets(a);
            length=strlen(a);
            int x[2]={0},y[2]={0},z[2]={0};
            
            for(int i=0;i<length;i++){
                if(a[i]=='X'){
                    x[0]+=(a[i-2]-'0');
                    x[1]=a[i+2]-'0';
                }
                if(a[i]=='Y'){
                    y[0]+=(a[i-2]-'0');
                    y[1]=a[i+2]-'0';
                }
                if(a[i]=='Z'){
                    z[0]+=(a[i-2]-'0');
                    z[1]=a[i+2]-'0';
                }
            }
            if(x[0]!=0&&x[1]!=0)
                printf("%d * X ^ %d", x[0]*x[1],x[1]-1);
            if(x[0]!=0&&x[1]==0)
                printf("%d", x[0]);
            if(x[0]!=0&&y[0]!=0)
                printf(" + ");
            if(y[0]!=0&&y[1]!=0)
                printf("%d * Y ^ %d", y[0]*y[1],y[1]-1);
            if(y[0]!=0&&y[1]==0)
                printf("%d", y[0]);
            if((z[0]!=0&&y[0]!=0)||(x[0]!=0&&z[0]!=0))
                printf(" + ");
            if(z[0]!=0&&z[1]!=0)
                printf("%d * Z ^ %d", z[0]*z[1],z[1]-1);
            if(z[0]!=0&&z[1]==0)
                printf("%d", z[0]);
            printf("\n");


        }
        

    }
    return 0;
}















Double click to view unformatted code.


Back to problem 11