UVa 327 - Evaluating Simple C Expressions

按理说应该是一个二叉树的题,许久以前用数组做的 ~

代码如下:

#include<stdio.h>
#include<string.h>
char m[33][33];
int main()
{
#ifdef wct
    freopen("sample.txt","r",stdin);
#endif
    char m[150],a[150],b[30],flag[100];
    int value,i,j,len,count,flagnum,num[30],numval[30];
    while(gets(m)!=NULL)
    {
        value=flagnum=count=0;
        memset(b,0,sizeof(b));
        memset(a,0,sizeof(a));
        memset(flag,0,sizeof(flag));
        len=strlen(m);
        for(i=0; i<len; i++)
            if(m[i]!=' ')
                a[count++]=m[i];
        count=0;
        len=strlen(a);
        for(i=0; i<len; i++)
        {
            if(a[i]>='a'&&a[i]<='z')
            {
                b[count]=a[i];
                num[count]=a[i]-'a'+1;
                numval[count++]=a[i]-'a'+1;
                if(a[i+1]=='+'&&a[i+2]=='+')
                {
                    num[count-1]++;
                    i+=2;
                }
                else if(a[i-1]=='+'&&a[i-2]=='+')
                {
                    num[count-1]++;
                    numval[count-1]++;
                }
                if(a[i+1]=='-'&&a[i+2]=='-')
                {
                    num[count-1]--;
                    i+=2;
                }
                else if(a[i-1]=='-'&&a[i-2]=='-')
                {
                    num[count-1]--;
                    numval[count-1]--;
                }
            }
            if(a[i]=='+')
            {
                if((a[i+2]=='-'&&a[i+1]=='-'&&(a[i-1]>='a'&&a[i-1]<='z'))||(a[i-2]=='-'&&a[i-1]=='-'&&(a[i+1]>='a'&&a[i+1]<='z')))
                    flag[flagnum++]='+';
                else if((a[i+1]=='-'&&a[i-1]=='-')||(a[i+1]=='+'&&a[i-1]=='+'))
                    flag[flagnum++]='+';
                else if((a[i+2]=='-'&&a[i+1]=='-'&&a[i-1]=='+')||(a[i+2]=='+'&&a[i+1]=='+'&&a[i-1]=='-'))
                    flag[flagnum++]='+';
                else if((a[i-1]>='a'&&a[i-1]<='z')&&(a[i+1]>='a'&&a[i+1]<='z'))
                    flag[flagnum++]='+';
            }
            if(a[i]=='-')
            {
                if((a[i+2]=='+'&&a[i+1]=='+'&&(a[i-1]>='a'&&a[i-1]<='z'))||(a[i-2]=='+'&&a[i-1]=='+'&&(a[i+1]>='a'&&a[i+1]<='z')))
                    flag[flagnum++]='-';
                else if((a[i+1]=='-'&&a[i-1]=='-')||(a[i+1]=='+'&&a[i-1]=='+'))
                    flag[flagnum++]='-';
                else if((a[i+2]=='-'&&a[i+1]=='-'&&a[i-1]=='+')||(a[i+2]=='+'&&a[i+1]=='+'&&a[i-1]=='-'))
                    flag[flagnum++]='-';
                else if((a[i-1]>='a'&&a[i-1]<='z')&&(a[i+1]>='a'&&a[i+1]<='z'))
                    flag[flagnum++]='-';
            }
        }
        value+=numval[0];
        j=1;
        for(i=0; i<flagnum; i++)
            if(flag[i]=='+')
                value+=numval[j++];
            else if(flag[i]=='-')
                value-=numval[j++];
        printf("Expression: %s\n",m);
        printf("    value = %d\n",value);
        for(j=0; j<=25; j++)
            for(i=0; i<count; i++)
                if(b[i]=='a'+j)
                    printf("    %c = %d\n",b[i],num[i]);
    }
    return 0;
}


你可能感兴趣的:(c,null)