题意
模拟二进制数字的位运算
思路
手写 位运算函数
要注意几个坑点
一元运算符的优先级 大于 二元
一元运算符 运算的时候 要取消前导0
二元运算符 运算的时候 要将两个数字 数位补齐
输出的时候 也要 注意 要取消前导0
特别要注意
如果输入的算式 只有一个数字
那么要将 这个数字的前导0 取消 再输出
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 4e5 + 5;
const int MOD = 1e9 + 7;
string Not(string s)
{
while (s.size() > 1 && s[0] == '0')
s.erase(0, 1);
int len = s.size();
map <char, char> m;
m['0'] = '1';
m['1'] = '0';
string ans = "";
for (int i = 0; i < len; i++)
ans += m[s[i]];
while (ans.size() > 1 && ans[0] == '0')
ans.erase(0, 1);
return ans;
}
string Shr (string s)
{
while (s.size() > 1 && s[0] == '0')
s.erase(0, 1);
string ans = s;
if (ans.size() == 1)
return "0";
if (ans.size() > 1)
ans.erase(ans.size() - 1, 1);
while (ans.size() > 1 && ans[0] == '0')
ans.erase(0, 1);
return ans;
}
string Shl (string s)
{
while (s.size() > 1 && s[0] == '0')
s.erase(0, 1);
string ans = s;
ans.insert(ans.size(), "0");
while (ans.size() > 1 && ans[0] == '0')
ans.erase(0, 1);
return ans;
}
string Xor (string x, string y)
{
string ans = "";
while (x.size() < y.size())
x.insert(0, "0");
while (x.size() > y.size())
y.insert(0, "0");
int len = x.size();
for (int i = 0; i < len; i++)
{
if (x[i] != y[i])
ans += "1";
else
ans += "0";
}
while (ans.size() > 1 && ans[0] == '0')
ans.erase(0, 1);
return ans;
}
string And (string x, string y)
{
string ans = "";
while (x.size() < y.size())
x.insert(0, "0");
while (x.size() > y.size())
y.insert(0, "0");
int len = x.size();
for (int i = 0; i < len; i++)
{
if (x[i] == '0' || y[i] == '0')
ans += '0';
else
ans += '1';
}
while (ans.size() > 1 && ans[0] == '0')
ans.erase(0, 1);
return ans;
}
string Or(string x, string y)
{
string ans = "";
while (x.size() < y.size())
x.insert(0, "0");
while (x.size() > y.size())
y.insert(0, "0");
int len = x.size();
for (int i = 0; i < len; i++)
{
if (x[i] == '1' || y[i] == '1')
ans += '1';
else
ans += '0';
}
while (ans.size() > 1 && ans[0] == '0')
ans.erase(0, 1);
return ans;
}
int main()
{
int t;
cin >> t;
getchar();
int count = 1;
while (t--)
{
string s;
getline(cin, s);
stack <string> code;
stack <string> num;
string temp = "";
int len = s.size();
for (int i = 0; i <= len; i++)
{
if (i < len && s[i] != ' ')
temp += s[i];
else
{
if (isdigit(temp[0]))
{
string n = temp;
num.push(n);
if (num.size() >= 1)
{
n = num.top();
num.pop();
string s = " ";
if (!code.empty())
s = code.top();
while (s == "not" || s == "shr" || s == "shl")
{
code.pop();
if (s == "not")
n = Not(n);
else if (s == "shr")
n = Shr(n);
else if (s == "shl")
n = Shl(n);
if (!code.empty())
s = code.top();
else
s = " ";
}
num.push(n);
}
if (num.size() >= 2)
{
string s = " ";
if (!code.empty())
s = code.top();
while ((s == "xor" || s == "and" || s == "or") && num.size() >= 2)
{
code.pop();
string x = num.top();
num.pop();
string y = num.top();
num.pop();
string ans;
if (s == "xor")
ans = Xor(x, y);
else if (s == "and")
ans = And(x, y);
else if (s == "or")
ans = Or(x, y);
if (!code.empty())
s = code.top();
else
s = " ";
num.push(ans);
}
}
}
else
{
code.push(temp);
}
temp.clear();
}
}
string ans = num.top();
while (ans.size() > 1 && ans[0] == '0')
ans.erase(0, 1);
printf("Case %d: ", count++);
cout << ans << endl;
}
}