//4_7_22: Experssion Evaluator ++和--字符串的求值 POJ3337
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int i,j,T,value,length,flag[30],alpha[30];
char sign,enter,str[100],ans[100],org[100];
scanf("%d%c",&T,&enter);
while(T --)
{
cin.getline(str,1000);
for(i = 0;i < 26;i ++)
alpha[i] = i + 1;
memset(flag,0,sizeof(flag));
strcpy(ans,str);
for(i = 0;i < strlen(str);i ++)
while(str[i] == ' ')
for(j = i;j < strlen(str);j ++)
str[j] = str[j + 1];
strcpy(org,str);
length = strlen(str);
i = value = 0;
while(org[i])
{
if(org[i] >= 'a' && org[i] <= 'z')
{
if(i > 1 && org[i - 1] == '+' && org[i - 2] == '+')
{
str[i - 1] = ' ';
str[i - 2] = ' ';
flag[(int)(org[i] - 'a')] = 1;
alpha[(int)(org[i] - 'a')] += 1;
for(j = 0;j < length;j ++)
if(org[j] == org[i])
str[j] = str[j] + 1;
}
else if(i > 1 && org[i - 1] == '-' && org[i - 2] == '-')
{
str[i - 1] = ' ';
str[i - 2] = ' ';
flag[(int)(org[i] - 'a')] = 1;
alpha[(int)(org[i] - 'a')] -= 1;
for(j = 0;j < length;j ++)
if(org[j] == org[i])
str[j] = str[j] - 1;
}
else if(i < length - 2 && org[i + 1] == '+' && org[i + 2] == '+')
{
str[i + 1] = ' ';
str[i + 2] = ' ';
flag[(int)(org[i] - 'a')] = 1;
alpha[(int)(org[i] - 'a')] += 1;
}
else if(i < length - 2 && org[i + 1] == '-' && org[i + 2] == '-')
{
str[i + 1] = ' ';
str[i + 2] = ' ';
flag[(int)(org[i] - 'a')] = 1;
alpha[(int)(org[i] - 'a')] -= 1;
}
else flag[(int)(org[i] - 'a')] = 1;
i++;
}
else
i++;
}
i = 0;
sign = '+';
//printf("%s\n",str);
while(str[i])
{
if(str[i] == ' ') i++;
else if(str[i] == '+')
{
sign = '+';
i ++;
}
else if(str[i] == '-')
{
sign = '-';
i ++;
}
else
{
if(sign == '+') value += (int)(str[i] - '`');
else value -= (int)(str[i] - '`');
i++;
}
}
printf("Expression: %s\n",ans);
printf("value = %d\n",value);
for(i = 0;i < 26;i ++)
if(flag[i] == 1)
printf("%c = %d\n",(char)(i + 'a'),alpha[i]);
}
return 0;
}
/*测试结果:通过POJ3337检测
1
--b+c-++b+b--
Expression: --b+c-++b+b--
value = 5
b = 1
c = 3
请按任意键继续. . .
*/
POJ3337 Experssion Evaluator
最新推荐文章于 2019-10-23 23:37:45 发布