题目1101:计算表达式
来源:互联网 发布:虎视眈眈单人动作数据 编辑:程序博客网 时间:2024/06/05 02:44
#include "iostream"#include "stdio.h"#include <vector>#include <cstring>#include <algorithm>#include <string>#include <string.h>#include <stack>using namespace std;//1101int op(int c){ if(c=='*'||c=='/') return 2; if(c=='+'||c=='-') return 1; else return 0;}bool isDig(char c){ if(c>='0'&&c<='9') return true; else return false;}int cal(int x,int y,char c){ if(c=='+') return x+y; if(c=='-') return x-y; if(c=='*') return x*y; if(c=='/') return x/y;}void fuck(char s[]){ stack<int> a; stack<char> ch; while(!a.empty()) a.pop(); while(!ch.empty()) ch.pop(); int i,t,l=strlen(s); char c; ch.push('#'); s[l++]='#'; for(i=0;i<l;i++){ if(isDig(s[i])){ sscanf(s+i,"%d",&t); a.push(t); while(isDig(s[i+1])) i++; } else{ if(ch.empty()) ch.push(s[i]); else{ c=ch.top(); if( op(s[i])-op(c) > 0){ ch.push(s[i]); } else{ while( op(c)-op(s[i]) >=0 ){ if(ch.top()=='#') {cout<<a.top()<<endl;return;} int y=a.top();a.pop(); int x=a.top();a.pop(); a.push(cal(x,y,c)); ch.pop(); c=ch.top(); } ch.push(s[i]); } } } } }char s[10010]; int main(){ //freopen("input.txt","r",stdin); while(scanf("%s",s)!=EOF){ fuck(s); } return 0;}/************************************************************** Problem: 1101 User: cust123 Language: C++ Result: Accepted Time:0 ms Memory:1532 kb****************************************************************/
0 0
- 题目1101:计算表达式
- 题目1101:计算表达式
- 题目1101:计算表达式
- 题目1101:计算表达式
- 题目1101:计算表达式
- 题目1101:计算表达式
- 题目1101:计算表达式
- 题目1101:计算表达式
- 题目1101:计算表达式[RE]
- 29-题目1101:计算表达式
- 九度 题目1101:计算表达式
- 九度OJ题目1101:计算表达式
- 题目29:计算表达式
- 题目1101 :计算表达式 (运算符操作)方法比较简洁
- leetcode刷题-题目2:逆波兰表达式计算
- 1101计算表达式
- 九度1101:计算表达式
- 表达式计算
- 十六 JdbcTemplate一
- Bluetooth on
- jquery升级之坑
- Struts2总结笔记之一
- 创建数据库命名规则
- 题目1101:计算表达式
- 03:采药
- Android学习笔记之Transition——Transition简单动画(1)
- 每日打卡 2017.04.16 前缀和
- 大盗阿福
- 版本比较
- Spring在基于java的配置中使用prop属性
- MySQl之最全且必会的sql语句
- 第18章 类加载机制与反射-JDK动态代理