hihoCoder 简单计算器
数据结构的入门题,理解倒是不复杂,用两个栈就行(一个存数字一个存符号)。对于我这样的弱弱没事练练编码能力还是不错的。
注意运算优先级即可。(过两天回科大了,下次再做题也不知道何时,ACM生涯两铜收场orz)
题目链接:
http://hihocoder.com/contest/hihointerview11/problem/3
代码:
#include <bits/stdc++.h> using namespace std;
typedef long long int64; stack<int64> sn;
stack<char> sc; const int maxn = + ;
char str[maxn]; char op[] = { '+', '-', '*', '/', '(', ')', '#' }; int getOpId(char op){
if(op == '+')
return -;
else if(op == '-')
return -;
else if(op == '*')
return -;
else if(op == '/')
return -;
else if(op == '(' )
return -;
else if(op == ')' )
return -;
else if(op == '#')
return -;
else if(op == '&')
return -;
} vector<int64> v; int getPrio(char op){
switch(op){
case '+': return ;
case '-': return ;
case '*': return ;
case '/': return ;
case '(': return ;
case '&': return ;
default: return ;
}
} int64 cal(int64 a, int64 b, char opt){
if(opt == '+'){
return a+b;
}else if(opt == '-'){
return a-b;
}else if(opt == '*'){
return a*b;
}else if(opt == '/'){
return a/b;
}
} int main(void){
scanf("%s", str);
int len = strlen(str);
str[len++] = '#'; for(int i = ; i < len; ++i){
if('' <= str[i] && str[i] <= ''){
int64 t = ;
while('' <= str[i] && str[i] <= ''){
t = t * + (str[i] - '');
i++;
}
i--;
v.push_back(t);
}else{
v.push_back(getOpId(str[i]));
}
//cout << v.back() << " ";
}
//cout << endl; int sz = v.size();
sc.push('&');
for(int i = ; i < sz; ++i){
if(v[i] >= ){
sn.push(v[i]);
//cout << "push " << v[i] << endl;
}else{
char c = op[-v[i]-];
if(c == '('){
sc.push('(');
//cout << "push " << '(' << endl;
}else if(c == ')'){
while(sc.top() != '('){
int64 b = sn.top(); sn.pop();
int64 a = sn.top(); sn.pop();
char opt = sc.top(); sc.pop();
sn.push(cal(a, b, opt));
//cout << "cal " << a << " " << opt << " " << b << endl;
}
sc.pop();
//cout << "pop " << '(' << endl;
}else{
int prio1 = getPrio(c);
int prio2; while(prio1 <= (prio2 = getPrio(sc.top()))){
//cout << "prio1: " << prio1 << " prio2: " << prio2 << endl;
int64 b = sn.top(); sn.pop();
int64 a = sn.top(); sn.pop();
char opt = sc.top(); sc.pop();
sn.push(cal(a, b, opt));
//cout << "cal " << a << " " << opt << " " << b << endl;
}
sc.push(c);
//cout << "push " << c << endl;
}
}
} //cout << "haha" << endl;
printf("%lld\n", sn.top()); return ;
} /**
5*(3+8*2)-(5+6*2-7)*2
100*(2+12)-(20/3)*2
(8+2*3/4-4)*(3-2+5/2*3)*(3-4+3)/6
*/
hihoCoder 简单计算器的更多相关文章
- 1.C#WinForm基础制作简单计算器
利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...
- 菜鸟学Android编程——简单计算器《一》
菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...
- PAT 06-1 简单计算器
想看一般简单计算器实现的看客不好意思了,这不是你想要点东西,此处题设为“只能进行加减乘除”.“都是整数”.”优先级相同“和"从左到右".此题来自PAT(http://www.pat ...
- php大力力 [005节] php大力力简单计算器001
2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...
- PHP实现简单计算器
<!--简单的计算器--> <!DOCTYPE html> <html> <head> <title>PHP实现简单计算器</titl ...
- c#部分---网吧充值系统;简易的闹钟;出租车计费;简单计算器;对战游戏;等额本金法计算贷款还款利息等;随机生成10个不重复的50以内的整数;推箱子;
网吧充值系统namespace ConsoleApplication1 { class Program { struct huiyuan { public string name; public st ...
- JavaWeb学习记录(二十)——Model1模式(javaBean+jsp)实现简单计算器案例
¨JSP技术提供了三个关于JavaBean组件的动作元素,即JSP标签,它们分别为: ¨<jsp:useBean>标签:用于在JSP页面中查找或实例化一个JavaBean组件. ¨< ...
- 一个用WPF做的简单计算器源代码
一.界面设计XAML代码 <Window x:Class="fengjisuanqi.MainWindow" xmlns="http://schemas.micro ...
- hdu 1237 简单计算器
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1237 简单计算器 Description 读入一个只包含 +, -, *, / 的非负整数计算表达式, ...
随机推荐
- 多线程-实现Runnable接口
当一个任务或者函数多个线程同时调用时仅仅继承Thread是不行的.需要实现Runnable接口. 好处: 1.将线程的任务从线程的子类中分离出来,进行了单独的封装. 按照面向对象的思想将任务封装成对象 ...
- servlet学习总结(一)——HttpServletRequest(转载)
原文地址:http://www.cnblogs.com/xdp-gacl/p/3798347.html 一.HttpServletRequest介绍 HttpServletRequest对象代表客户端 ...
- SQL几种常用的函数
函数的种类: 算数函数(数值计算的函数) 字符串函数(字符串操作的函数) 日期函数(用来进行日期操作的函数) 转换函数(用来转换数据类型和值的函数) 聚合函数(用来进行数据聚合的函数) 算数函数(+- ...
- std::vector遍历
std::vector是我在标准库中实用最频繁的容器.总结一下在遍历和创建vector时需要注意的一些地方. 在不考虑线程安全问题的前提下,在C++11中有五种遍历方式. 方式一 for (size_ ...
- POJ3984——迷宫问题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31616 Accepted: 18100 Descriptio ...
- HDU-1864&&HDU-2602(01背包问题)
DP-01背包问题例题 输入处理有点恶心人,不过处理完后就是简单的DP了 从头开始dp[i]表示从0开始到i的最优结果,最后从都边里dp数组,求得最大的报销额. 对于每个i都要从头维护最优结果.(二刷 ...
- scrapy——8 scrapyd使用
scrapy——8 scrapyd使用 什么是scrapyd 怎么安装scrapyd 如何使用scrapyd--运行scrapyd 如何使用scrapyd--配置scrapy.cfg 如何使用s ...
- 【codeforces 801C】Voltage Keepsake
[题目链接]:http://codeforces.com/contest/801/problem/C [题意] 有n个设备 你想同时使用 第i个设备每分钟消耗ai点电量,一开始有bi点电量 你有一个充 ...
- noip模拟赛 同余方程组
分析:这道题一个一个枚举都能有70分...... 前60分可以用中国剩余定理搞一搞.然而并没有枚举分数高......考虑怎么省去不必要的枚举,每次跳都只跳a的倍数,这样对前面的式子没有影响,为了使得这 ...
- python在Linux中安装虚拟环境,区别python2和python3,分别安装模块
安装虚拟环境的时候遇到的问题,解决的过程很闹心,在这里简单直接的分享出来,就是为了解决问题. 安装虚拟环境(须在联网状态下) $ sudo pip install virtualenv $ sudo ...