327 - Evaluating Simple C Expressions

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>

using namespace std;

int alpha[2];
int ope[2];
int Ca;
int Co;
int Fpre;
char c;
int A[27];
int Used[27];


void Initial()
{
    for(int i = 1;i <= 26;i++){
        A[i] = i;
        Used[i] = 0;
    }

    Ca = Co = Fpre = 0;

    for(int i = 0;i < 2;i++)
     ope[i] = alpha[i] = 0;
}


void Calculate()
{
    if(Ca > 1){
        Ca = 1;
        if(ope[Co-1] == '+')
         alpha[0] += alpha[1];
        else
         alpha[0] -= alpha[1];

        alpha[1] = 0;
    }


}


int main()
{
   // freopen("in.in","r",stdin);

    while((c = getchar()) != EOF)
    {
        ungetc(c,stdin);

        Initial();
        cout<<"Expression: ";
        while(1)
        {
            if((c = getchar()) == EOF){cout<<endl;break;}
            cout<<c;
            if(c == '\n') break;
            if(c == ' ') continue;

            if(isalpha(c))
            {
                int n = c-'a'+1;
                Used[n] = 1;
                alpha[Ca++] = A[n];

                if(Fpre){
                    A[n] += Fpre;
                    alpha[Ca-1] += Fpre;
                    Fpre = 0;
                }
            }
            else if(c == '+' || c == '-')
            {
                char temp1;
                loop2:
                temp1 = getchar();
                  if(temp1 == ' ')
                  {
                       cout<<' ';
                       goto loop2;
                  }

                if(temp1 != c){
                    ungetc(temp1,stdin);
                    if(Co)
                    {
                        Calculate();
                        Co = 0;
                    }
                    ope[Co++] = c;
                }
                else{
                    cout<<c;
                    char temp2;

                    loop1:
                    temp2 = getchar();
                    if(temp2 == ' ')
                    {
                       cout<<' ';
                       goto loop1;
                    }


                    if(isalpha(temp2)){
                        if( c == '+')
                         Fpre = 1;
                        else
                         Fpre = -1;
                    }
                    else{
                        if(c == '-')
                          A[alpha[Ca-1]]--;
                        else
                          A[alpha[Ca-1]]++;
                    }
                    ungetc(temp2,stdin);
                }
            }

        }

        Calculate();

        cout<<"    value = "<<alpha[0]<<endl;

        for(int i = 1;i < 27;i++)
         if(Used[i])
         {
             char temp = 'a' + i - 1;
             cout<<"    ";
             cout<<temp<<" = "<<A[i]<<endl;
         }

    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值