题意

模拟二进制数字的位运算

思路

手写 位运算函数

要注意几个坑点

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

一元运算符 运算的时候 要取消前导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. PS中把图片白色背景变成透明的方法

    用魔术橡皮擦擦去白色(调整容差,取消连续,点选白色部分),保存成png格式

  2. cordova 中de.sitewaerts.cordova.documentviewer 插件 看pdf图片缩略图与实际图片不一致

    //if (document == nil) // Unarchive failed so create a new ReaderDocument object //{ document = [[Re ...

  3. 关于 Android 平台开发相关的有哪些推荐书籍?

    转自:http://www.zhihu.com/question/19579609 作者:Shan Huang 链接:http://www.zhihu.com/question/19579609/an ...

  4. 转: java服务器端成长指南

    from:  http://m.blog.csdn.net/article/details?id=45797155 前言 这是一份针对新手的服务端开发入门与进阶指南.遇到问题及时问你的 mentor ...

  5. js 选项卡封装

    function tab(input,div){ for(var i = 0; i < input.length; i++){ input[i].index = i; input[i].oncl ...

  6. 对Date的扩展,将 Date 转化为指定格式的String

    <script language="javascript" type="text/javascript"><!-- /** * 对Date的扩 ...

  7. web 表单方式上传文件方法(不用flash插件)

    原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...

  8. MagicalRecord使用教程【转载】

    原文地址:http://www.ithao123.cn/content-96403.html 下面是在xcode5.1下ARC环境中的使用教程 1. 将 MagicalRecord 文件夹拖入到工程文 ...

  9. 02-cookie案例-显示用户上次访问网站的时间

    package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...

  10. Google论文BigTable拜读

    这周少打点dota2,争取把这篇论文读懂并呈现出来,和大家一起分享. 先把论文搞懂,然后再看下和论文搭界的知识,比如hbase,Chubby和Paxos算法. Bigtable: A Distribu ...