UVA - 11954 Very Simple Calculator 【模拟】

这里写图片描述

题意
模拟二进制数字的位运算

思路
手写 位运算函数
要注意几个坑点
一元运算符的优先级 大于 二元
一元运算符 运算的时候 要取消前导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;
    }
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、资源1项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值