Very Simple Calculator

This problem doesn’t have a story. You just have to write a calculator which could perform simple binary operations. You will be given an expression and your program should calculate it’s result. Expression is formed according to this rules:
::= 0|1
::= |
:== not | shr | shl
:== xor | and | or
::= | |
::= |
Operation definitions:
not – binary negation operation (example: not 101 = 010)
shr – binary right shift operation (example: shr 101 = 10)
shl – binary left shift operation (example: shl 101 = 1010)
xor – binary exclusive or operation (example: 0111 xor 1011 = 1100) and – binary and operation (example: 0111 and 1011 = 0011)
or – binary or operation (example: 0111 or 1011 = 1111)
All unary operators have higher priority than binary ones and binary operators must be calculated from left to right (their priority is considered equal). Before any unary operation all additional leading zeros should be removed (for example 0011 becomes 11 and 000 becomes 0), and before a binary one you should align binary numbers by adding leading zeros if necessary (for example 11 xor 101 becomes 011 xor 101).

Input

The number of tests T (T ≤ 100) is given on the first line. Each of next T lines contains an expression itself. Length of the expression will be always less than 1000 characters.

Output

For each test case output a single line ‘Case T: N’. Where T is the test case number (starting from 1) and N is the value of evaluated expression in the same binary form. Answer should not contain any leading zeroes.

Sample Input

4
shl not 101
not 11 and 111
111 xor not 0
shl 0
Sample Output
Case 1: 100
Case 2: 0
Case 3: 110
Case 4: 0

题意:

(大)模拟

思路:

只有一个坑点,每次进行一级运算符之前要去掉前缀0,并且不能为空。

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <queue>
#include <cstdlib>
using namespace std;
void NOT(string &str) {
    while (str[0] == '0') str.erase(str.begin());
    if (str.empty()) str += '0';
    for (int i = 0; i < str.size(); i++)
        if (str[i] == '1') str[i] = '0';
        else str[i] = '1';
}
void SHR(string &str) {
    while (str[0] == '0') str.erase(str.begin());
    if (str.empty()) str += '0';
    str.erase(str.end() - 1);
}
void SHL(string &str) {
    while (str[0] == '0') str.erase(str.begin());
    if (str.empty()) str += '0';
    str += '0';
}
string XOR(string str1, string str2) {
    while (str1.size() < str2.size()) str1.insert(0, 1, '0');
    while (str2.size() < str1.size()) str2.insert(0, 1, '0');
    string ans;
    for (int i = 0; i < str1.size() || i < str2.size(); i++) {
        if (str1[i] == str2[i]) ans += '0';
        else ans += '1';
    }
    while (ans[0] == '0') ans.erase(ans.begin());
    if (ans.empty()) ans += '0';
    return ans;
}
string AND(string str1, string str2) {
    while (str1.size() < str2.size()) str1.insert(0, 1, '0');
    while (str2.size() < str1.size()) str2.insert(0, 1, '0');
    string ans;
    for (int i = 0; i < str1.size() || i < str2.size(); i++) {
        if (str1[i] == '1' && str2[i] == '1') ans += '1';
        else ans += '0';
    }
    while (ans[0] == '0') ans.erase(ans.begin());
    if (ans.empty()) ans += '0';
    return ans;
}
string OR(string str1, string str2) {
    while (str1.size() < str2.size()) str1.insert(0, 1, '0');
    while (str2.size() < str1.size()) str2.insert(0, 1, '0');
    string ans;
    for (int i = 0; i < str1.size() || i < str2.size(); i++) {
        if (str1[i] == '1' || str2[i] == '1') ans += '1';
        else ans += '0';
    }
    while (ans[0] == '0') ans.erase(ans.begin());
    if (ans.empty()) ans += '0';
    return ans;
}
int main() {
    int T;
    cin >> T;
    getchar();
    for (int tt = 1; tt <= T; tt++) {
        string ans, text, tmp;
        stack <string> s;
        getline(cin, text);
        for (int i = 0; i < text.size(); ) {
            tmp.clear();
            while (text[i] != ' ' && i < text.size()) tmp += text[i++];
            if (i < text.size()) i++;
            if (tmp[0] == '1' || tmp[0] == '0') {
                while (!s.empty()) {
                    if (s.top() == "not") NOT(tmp);
                    else if (s.top() == "shl") SHL(tmp);
                    else if (s.top() == "shr") SHR(tmp);
                    else if (s.top() == "and") ans = AND(ans, tmp);
                    else if (s.top() == "xor") ans = XOR(ans, tmp);
                    else if (s.top() == "or") ans = OR(ans, tmp);
                    s.pop();
                }
                if (ans.empty()) ans = tmp;
            }
            else s.push(tmp);
        }
        while (!s.empty()) {
            if (s.top() == "not") NOT(tmp);
            else if (s.top() == "shl") SHL(tmp);
            else if (s.top() == "shr") SHR(tmp);
            else if (s.top() == "and") ans = AND(ans, tmp);
            else if (s.top() == "xor") ans = XOR(ans, tmp);
            else if (s.top() == "or") ans = OR(ans, tmp);
            s.pop();
        }
        while (ans[0] == '0') ans.erase(ans.begin());
        if (ans.empty()) ans += '0';
        cout << "Case " << tt << ": " << ans << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/white_yasha/article/details/79898946