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 ...
随机推荐
- DedeCMS使用channelartlist循环,不能获取外部链接的解决办法
Dede在制作下拉菜单时,可以使用channelartlist循环调用顶级栏目和子栏目,但该标签不能获取外部链接, 下拉菜单时一段代码搞定多个顶级栏目和子级栏目的同时输出. <div> ...
- flash+xml无法显示中文的解决办法
flash+xml用来做图片动态浏览效果相当不错,被广泛运用于电子相册制作,很多朋友都会从网上下载一些相关的flash源码下载参考,但是经常发现在使用过程中,修改了xml文件中的英文后要么文本不显示, ...
- APNS push server端 SSL3.0 转 TLS (iPhone苹果推送服务)
(转载此文,请说明原文出处) 苹果的官方公布 Update to the Apple Push Notification Service October 22, 2014 The Apple Push ...
- 封装GetQueryString()方法来获取URL的value值
首先测试URL:http://192.168.1.82:8020/juzhong/daojishi.html?name=xiangruding&sex=nuuu&age=90 代码如下 ...
- HDOJ Oulipo 1686【KMP】
Oulipo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- JAVA学习第二十五课(多线程(四))- 单例设计模式涉及的多线程问题
一.多线程下的单例设计模式 利用双重推断的形式解决懒汉式的安全问题和效率问题 //饿汉式 /*class Single { private static final Single t = new Si ...
- Fragment小结
Fragment是Android3.0之后增加的新特性,通常人们叫它碎片.可是,我认为把它理解成一个View模块比較好,尽管它不是继承自View.假设阅读过源代码就知道它是内置View对象从而实现Vi ...
- Spring Resource框架体系介绍
Resource介绍 在使用spring作为容器进行项目开发中会有很多的配置文件,这些配置文件都是通过Spring的Resource接口来实现加载,但是,Resource对于所有低级资源的访问都不够充 ...
- rtems 4.11 IRQ (arm,beagle)
arm IRQ入口在 cpukit/score/arm/arm_exec_interrupt.S 中,其中BSP最关心就是 bl bsp_interrupt_dispatch 这句,看看beagle ...
- 近期公共祖先(LCA)——离线Tarjan算法+并查集优化
一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...