题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=263
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstddef>
using namespace std;
bool mark[27];
int alp[27];
void add(const string &s, int &ans, size_t &i)
{
++i;
if(isalpha(s[i]))
{
ans += alp[s[i] - 'a'];
mark[s[i] - 'a'] = 1;
if(i + 2 < s.size() && s[i + 1] == '+' && s[i + 2] == '+')
{
++alp[s[i] - 'a'];
i += 2;
}
else if(i + 2 < s.size() && s[i + 1] == '-' && s[i + 2] == '-')
{
--alp[s[i] - 'a'];
i += 2;
}
}
else
{
if(i + 2 < s.size() && s[i] == '+' && s[i + 1] == '+')
{
mark[s[i + 2] - 'a'] = 1;
++alp[s[i + 2] - 'a'];
ans += alp[s[i + 2] - 'a'];
i += 2;
}
else if(i + 2 < s.size() && s[i] == '-' && s[i + 1] == '-')
{
mark[s[i + 2] - 'a'] = 1;
--alp[s[i + 2] - 'a'];
ans += alp[s[i + 2] - 'a'];
i += 2;
}
}
}
void sub(const string &s, int &ans, size_t &i)
{
++i;
if(isalpha(s[i]))
{
ans -= alp[s[i] - 'a'];
mark[s[i] - 'a'] = 1;
if(i + 2 < s.size() && s[i + 1] == '+' && s[i + 2] == '+')
{
++alp[s[i] - 'a'];
i += 2;
}
else if(i + 2 < s.size() && s[i + 1] == '-' && s[i + 2] == '-')
{
--alp[s[i] - 'a'];
i += 2;
}
}
else
{
if(i + 2 < s.size() && s[i] == '+' && s[i + 1] == '+')
{
mark[s[i + 2] - 'a'] = 1;
++alp[s[i + 2] - 'a'];
ans -= alp[s[i + 2] - 'a'];
i += 2;
}
else if(i + 2 < s.size() && s[i] == '-' && s[i + 1] == '-')
{
mark[s[i + 2] - 'a'] = 1;
--alp[s[i + 2] - 'a'];
ans -= alp[s[i + 2] - 'a'];
i += 2;
}
}
}
int main()
{
string s;
while(getline(cin, s))
{
memset(mark, 0, sizeof(mark));
for(size_t i = 0; i < 27; alp[i] = i + 1, ++i);
string ss = "+";
for(size_t i = 0; i < s.size(); ++i)
if(s[i] != ' ')
ss += s[i];
int ans = 0;
for(size_t i = 0; i < ss.size(); ++i)
{
if(ss[i] == '+') add(ss, ans, i);
else sub(ss, ans, i);
}
cout << "Expression: " << s << endl;
cout << " value = " << ans << endl;
for(size_t i = 0; i < 27; ++i)
{
if(mark[i])
{
cout << " " << char('a' + i) << " = " << alp[i] << endl;
}
}
}
return 0;
}
posted on 2011-11-29 21:19
wuxu 阅读(362)
评论(0) 编辑 收藏 引用 所属分类:
模拟