直接展示源码,采用或者用于参考的,麻烦给个赞呗,谢谢!
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string s;
while (cin >> s) {
string a, b;
char ch = ' ';
int A = 0, B = 0;
int flag = 1, cnt = 0, unt = 0;
for (int i = 0; i < s.length(); i++)
{
//如果是&,不用第一个if语句的话,ch==' '(吾不知为什么)
if (s[i] == '&')
{
ch = '&';
flag = 0;
}
else if (s[i] > '9') {
ch = s[i];
flag = 0;
}
else if (flag&& s[i] >= '0' && s[i] <= '9'){
a += s[i];
cnt++;
}
else if (!flag && s[i] >= '0' && s[i] <= '9') {
b += s[i];
unt++;
}
}
for (int i = cnt - 1; i >= 0; i--)
{
int x = pow(10, i);
A += (a[cnt - 1 - i] - '0') * x;
}
for (int i = unt - 1; i >= 0; i--)
{
int x = pow(10, i);
B += (b[unt - 1 - i] - '0') * x;
}
//cout << ch << endl;
int tmp;
switch (ch)
{
case '^':
tmp = A ^ B;
cout << tmp << endl;
break;
case '~':
tmp = ~B;
cout << tmp << endl;
break;
case '&':
tmp = A & B;
cout << tmp << endl;
break;
case '|':
tmp = A | B;
cout << tmp << endl;
break;
default:
break;
}
}
}
|