题意

模拟二进制数字的位运算

思路

手写 位运算函数

要注意几个坑点

一元运算符的优先级 大于 二元

一元运算符 运算的时候 要取消前导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;
}
}

UVA - 11954 Very Simple Calculator 【模拟】的更多相关文章

  1. uva 1567 - A simple stone game(K倍动态减法游戏)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=4342">题目链接:uva 1567 - ...

  2. uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟

    由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...

  3. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  4. UVA 213 Message Decoding 【模拟】

    题目链接: https://cn.vjudge.net/problem/UVA-213 https://uva.onlinejudge.org/index.php?option=com_onlinej ...

  5. uva 725 Division(暴力模拟)

    Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...

  6. UVA 327 -Evaluating Simple C Expressions(栈)

    Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C expre ...

  7. UVa 679 小球下落 简单模拟题,树

    题目大意:给你一个完全二叉树,并且给他们编号,编号规则为左子树为2*k,右子树为2*k+1,每一个节点 上都有一个开关,初始时开关都处于关闭状态,小球碰到节点就会改变该点的开关的状态.然后给你I个小球 ...

  8. UVa 11988 Broken Keyboard(数组模拟链表)

    题目链接: https://cn.vjudge.net/problem/UVA-11988 /* 问题 将一段文本经过一定的规则处理后输出,规则就是[表示home键,表示光标跳到行首,]表示end键, ...

  9. 【UVA】1595 Symmetry(模拟)

    题目 题目     分析 理清思路,上模拟.     代码 #include <bits/stdc++.h> using namespace std; const int maxn=100 ...

随机推荐

  1. Android 使用意图播放本地视频

    Android播放视频的方式有三种: 一.使用意图播放,调用本地安装的播放器,选择一个进行播放. 二.使用VideoView播放(VideoView事实上是对MediaPlayer的封装,使用起来非常 ...

  2. Shell脚本之:变量

    与编译型语言不同,shell脚本是一种解释型语言. 执行这类程序时,解释器(interpreter)需要读取我们编写的源代码(source code),并将其转换成目标代码(object code), ...

  3. 修改Oracle SGA,以提高oracle性能

    在正常情况下,查询非常慢. 1.检查SGA大小,以DBA身份连接到oracle数据库,输入show sga. 2.如果SGA过小,请修改其大小 修改SGA必须保持的原则 1).sga_target不能 ...

  4. A008-drawable资源

    关于drawable资源笔者之前有写过两篇文章: Android-自己定义图像资源的使用(1) Android-自己定义图像资源的使用(2) 这里笔者就不做过多的赘述.我们从实际开发的角度去理解这个知 ...

  5. C语言的##

    比如说我定义一个宏:#define DECLARE_DYNAMIC(class_name) \public:static CRuntimeClass class##class_name; \virtu ...

  6. 优志愿前端数据加密破解-python

    # coding=utf-8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "etiaky.sett ...

  7. ubuntu16.04--在标题栏显示网速

    有时感觉网络失去响应,就通过Ubuntu 14.04自带的系统监视器程序来查看当前网速,但是这样很不方便,遂打算让网速显示在标题栏,那样就随时可直观的看到.一番搜索尝试后,成功实现!同时也实现了CPU ...

  8. python中strip()函数的理解

    1.strip()函数 函数原型 声明:s为字符串.rm为要删除的字符序列 s.strip(rm) :删除s字符串中开头.结尾处.位于 rm删除序列的字符 s.lstrip(rm) :删除s字符串中开 ...

  9. Hadoop起源

    本文来自Doug Cutting为<Hadoop权威指南>所作之序,感觉读一下还是挺有收获的. Hadoop 起源于Nutch项目.我们几个人有一段时间一直在尝试构建一个开源的Web搜索引 ...

  10. 10个经典Java面试题

    1.Java的HashMap是怎样工作的? HashMap是一个针对数据结构的键值.每一个键都会有对应的值.关键是识别这种值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...