Infix to postfix without '(' and ')'
#include<iostream>
#include<stack>
#include<string>
#include<deque>
using namespace std; char compare(char tp, char op)
{
if (((tp == '+' || tp == '-') && (op == '*' || op == '/')) || tp == '#')
return '<';
return '>';
} int main()
{
stack<char>num;
stack<char>oper; oper.push('#'); string s;
cin >> s; for (int i = 0; i<s.length(); i++)
{
if (s[i] == '0' || s[i] == '1' || s[i] == '2' || s[i] == '3' || s[i] == '4' || s[i] == '5' || s[i] == '6' || s[i] == '7' || s[i] == '8' || s[i] == '9')
num.push(s[i]);
else
{
char comp = compare(oper.top(), s[i]);
if (comp == '<')
oper.push(s[i]);
else if (comp == '>')
{
num.push(oper.top());
oper.pop();
oper.push(s[i]);
}
}
} while (oper.top() != '#')
{
num.push(oper.top());
oper.pop();
} deque<char> d; while (num.size() != 0)
{
d.push_front(num.top());
num.pop();
} for (auto i = d.begin(); i != d.end(); i++)
cout << *i; cout << endl; return 0;
}
Infix to postfix without '(' and ')'的更多相关文章
- Infix to postfix conversion 中缀表达式转换为后缀表达式
Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...
- Infix to Postfix Expression
Example : Infix : (A+B) * (C-D) ) Postfix: AB+CD-* 算法: 1. Scan the infix expression from left to rig ...
- infix to postfix 完整版
#include<iostream> #include<stack> #include<string> #include<deque> using na ...
- Infix to postfix 用stack模板,表达式没有括号
#include<stack> #include<iostream> #include<string> using namespace std; //优先级判断 c ...
- Data Structure Stack: Infix to Postfix
http://geeksquiz.com/stack-set-2-infix-to-postfix/ #include <iostream> #include <vector> ...
- swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- 第一章-第一题(小学生四则运算)--By郭青云
1.项目需求 a) 除了整数以外,还要支持真分数的四则运算. (例如: 1/6 + 1/8 = 7/24) b) 让程序能接受用户输入答案,并判定对错. 最后给出总共 对/错 的数量. c) 逐步扩 ...
- Swift 对比学习 (二)
书接上回,可以作为参数和返回值的函数数型,以及嵌套函数,绝对继承了动态语言的优良传统: 函数嵌套了,那必然少不了闭包问题,Swift的闭包表达式语法也蛮有趣的. { (paraeeters) -> ...
- swift学习笔记之-高级运算符
//高级运算符 import UIKit /*高级运算符(Advanced Operators):位运算符.溢出运算符.优先级和结合性.运算符函数.自定义运算符 位运算符: 1.位运算符可以操作数据结 ...
随机推荐
- Racket中使用Y组合子
关于Y组合子,网上已经介绍很多了,其作用主要是解决匿名lambda的递归调用自己. 首先我们来看直观的递归lambda定义, 假设要定义阶乘的lambda表达,C#中需要这么定义 Func<in ...
- ap143 添加复位和重启按钮
1.修改匹配的文件mach-ap143.c 定义按键对应的GPIO(根据原理图来) #define AP143_GPIO_BTN_RESET 12 添加按钮的初始化消息 注册定时轮询按钮动作的函数 2 ...
- 更新UI
//1. this.Invoke(new ThreadStart(delegate { textBox1.AppendText(" + "\r\n"); })); //2 ...
- OpenGL编译问题随手记
1.error C2381: "exit" : 重定义:__declspec(noreturn) 不同 编译OpenGL Red Book 的例子时出现错误, stdl ...
- 利用Runtime给UITextView添加占位符(新方法)
以前一直使用自定义UITextView通过通知中心来自定义placeHolder,最近看到这个方法,感觉更好 UITextView *textView = [[UITextView alloc]in ...
- magento产品批量导出导入
magento产品批量导出导入 博客分类: WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra ExcelMobile配置管理XML ...
- 第一百二十四节,JavaScriptCookie与存储
JavaScriptCookie与存储 学习要点: 1.cookie 2.cookie局限性 3.其他存储 随着Web越来越复杂,开发者急切的需要能够本地化存储的脚本功能.这个时候,第一个出现的方案: ...
- linux统计单词数
sort +awk+uniq 统计文件中出现次数最多的前10个单词 实例 cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sor ...
- dplyr 数据操作 常用函数(4)
接下来我们继续了解一些dplyr中的常用函数. 1.ranking 以下各个函数可以实现对数据进行不同的排序 row_number(x) ntile(x, n) min_rank(x) dense_r ...
- Gentoo网络管理方法总结
OpenRC/netifrc Netifrc is a collection of modules created to configure and manage network interfaces ...