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;
}
}
UVA - 11954 Very Simple Calculator 【模拟】的更多相关文章
- uva 1567 - A simple stone game(K倍动态减法游戏)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=4342">题目链接:uva 1567 - ...
- uva 327 Evaluating Simple C Expressions 简易C表达式计算 stl模拟
由于没有括号,只有+,-,++,--,优先级简单,所以处理起来很简单. 题目要求计算表达式的值以及涉及到的变量的值. 我这题使用stl的string进行实现,随便进行练手,用string的erase删 ...
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...
- UVA 213 Message Decoding 【模拟】
题目链接: https://cn.vjudge.net/problem/UVA-213 https://uva.onlinejudge.org/index.php?option=com_onlinej ...
- uva 725 Division(暴力模拟)
Division 紫书入门级别的暴力,可我还是写了好长时间 = = [题目链接]uva 725 [题目类型]化简暴力 &题解: 首先要看懂题意,他的意思也就是0~9都只出现一遍,在这2个5位数 ...
- UVA 327 -Evaluating Simple C Expressions(栈)
Evaluating Simple C Expressions The task in this problem is to evaluate a sequence of simple C expre ...
- UVa 679 小球下落 简单模拟题,树
题目大意:给你一个完全二叉树,并且给他们编号,编号规则为左子树为2*k,右子树为2*k+1,每一个节点 上都有一个开关,初始时开关都处于关闭状态,小球碰到节点就会改变该点的开关的状态.然后给你I个小球 ...
- UVa 11988 Broken Keyboard(数组模拟链表)
题目链接: https://cn.vjudge.net/problem/UVA-11988 /* 问题 将一段文本经过一定的规则处理后输出,规则就是[表示home键,表示光标跳到行首,]表示end键, ...
- 【UVA】1595 Symmetry(模拟)
题目 题目 分析 理清思路,上模拟. 代码 #include <bits/stdc++.h> using namespace std; const int maxn=100 ...
随机推荐
- C#面试基础题1
1.简述 private. protected. public. internal 修饰符的访问权限.(C++中没有internal) private : 私有成员, 在类的内部才可以访问 ,也就是类 ...
- 【GLSL教程】(二)在OpenGL中使用GLSL 【转】
http://blog.csdn.net/racehorse/article/details/6616256 设置GLSL 这一节讲述在OpenGL中配置GLSL,假设你已经写好了顶点shader和像 ...
- context:exclude-filter spring事宜【经典-转】
context:exclude-filter spring事务 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行. ...
- MySQL具体解释(14)----------事务处理
前言:前一篇文章关于事务处理的博文没有写清楚,读起来非常晦涩.非常难理解,所以有整理了一些资料,帮助理解.见谅! 关于MySQL事务处理学习记 START TRANSACTION COMMIT ROL ...
- 使用HTML5制作简单的RPG游戏
很久以前就想着做一个游戏,但什么都不会又不知道从哪里开始,胡乱找来一些书籍和资料结果太深奥看不懂,无奈只能放弃.这一弃就是十多年,倥偬半生,眼看垂垂老矣,还是没能有什么成果. 近年来游戏引擎越来越多, ...
- 基于UML的需求分析和系统设计个人体会
阅读了http://www.uml.org.cn/oobject/201405123.asp文章之后,对使用UML进行系统的需求分析和设计有了一个基础的理解.在此做一下整理. 1.项目开始阶段 项 ...
- Uboot的串口下载文件命令:loads / loadb / loady
1. loads loads [ off ] 通过串口,下载S-Rec文件到off位置 loads命令可以通过串口线下载S-Record格式文件. 2. loadb loadb [ off ] [ b ...
- 华为云测平台服务再升级!华为M5系列平板调测能力正式上线!
6月1日,华为M5系列平板设备兼容性测试和远程真机调试功能在华为终端开放实验室正式上线!助力您的产品在大屏适配上快人一步! 华为终端开放实验室DevEco平台现已提供基于华为M5系列平板设备的兼 ...
- 【转载】【selenium+Python WebDriver】之元素定位
总结: 感谢: “煜妃”<Selenuim+Python之元素定位总结及实例说明> “Huilaojia123”<selenium WebDriver定位元素学习总结> “上海 ...
- 五分钟了解 Service Mesh
1 背景 1.1 多语言 微服务理念是提倡不同业务使用最适合它的语言开发,现实情况也确实如此,尤其是AI的兴起,一般大型互联网公司存在 C/C++.Java.Golang.PHP.Pyth ...