当前位置: 首页 > news >正文

包河网站建设sem论坛

包河网站建设,sem论坛,wordpress知更鸟下载,网站小游戏怎么做描述 对于一个不存在括号的表达式进行计算 输入描述: 存在多组数据,每组数据一行,表达式不存在空格 输出描述: 输出结果 示例1 输入: 6/233*4输出: 18思路: ①设立运算符和运算数两个…

描述

对于一个不存在括号的表达式进行计算

输入描述:

存在多组数据,每组数据一行,表达式不存在空格

输出描述:

输出结果

示例1

输入:

6/2+3+3*4

输出:

18

思路:

①设立运算符和运算数两个栈,,一个用来存储运算符,另一个用来存储运算数。

②在运算符栈中放置一个特殊运算符#,其优先级最低。

③将表达式尾部添加一个特殊运算符$,其优先级次低。

④从左至右依次遍历字符串,若遍历到运算符,则将其与运算符栈的栈顶元素进行比较,若运算符栈的栈顶的优先级小于该运算符,则将该运算符压入运算符栈;若运算符栈的栈顶的优先级大于该运算符,则弹出该栈顶运算符,从运算数栈中依次弹出运算数,完成弹出运算符对应的运算后,再将该结果压入运算数栈。

⑤若遍历到表达式中的运算数,则直接压入运算数栈。

⑥若运算符栈中仅剩两个特殊运算符#和$,则表达式运算结束,此时运算数栈中唯一的数字就是表达式的值。
 

源代码:

#include<iostream>
#include<stack>
#include<map>
#include<string>
using namespace std;//习题5.2 KY102 计算表达式
//考虑到需要计算的数字可能不止一位,就从检测到数字的索引开始,一直到检测不到数字的索引,这之间的就是一整个数字
double getNum(string str, int& index) {double res = 0;while (isdigit(str[index])) {res = res * 10 + str[index] - '0';index++;}return res;
}double cal(double x, double y, char op) {double res = 0;if (op == '+') {res = x + y;}else if (op == '-') {res = x - y;}else if (op == '*') {res = x * y;}else if (op == '/') {res = x / y;}return res;
}int main()
{string s;//存储多个运算符号的优先级map<char, int> maps = { {'#',0},{'$',1},{'+',2},{'-',2},{'*',3},{'/',3} };while (cin >> s) {stack<char> symbol;stack<double> number;//在运算符栈中放置一个特殊运算符#,其优先级最低。symbol.push('#');//将表达式尾部添加一个特殊运算符$,其优先级次低s = s + '$';int index = 0;while (index < s.size()) {if (isdigit(s[index])) { //遍历到数字number.push(getNum(s, index));}else { //遍历到运算符//若运算符栈的栈顶的优先级小于该运算符,则将该运算符压入运算符栈if (maps[s[index]] > maps[symbol.top()]) {symbol.push(s[index]);index++;}//否则,弹出该栈顶运算符,从运算数栈中依次弹出运算数,完成弹出运算符对应的运算后,再将该结果压入运算数栈else {double x = number.top();number.pop();double y = number.top();number.pop();number.push(cal(y, x, symbol.top()));symbol.pop();}}}printf("%.0f", number.top());}return 0;
}

提交结果:

 


文章转载自:
http://brushed.qnzk.cn
http://chromatophore.qnzk.cn
http://interpenetration.qnzk.cn
http://carbonnade.qnzk.cn
http://cyclotron.qnzk.cn
http://struvite.qnzk.cn
http://defog.qnzk.cn
http://backchat.qnzk.cn
http://autotoxicosis.qnzk.cn
http://soljanka.qnzk.cn
http://khowar.qnzk.cn
http://infliction.qnzk.cn
http://coverlid.qnzk.cn
http://worker.qnzk.cn
http://brussels.qnzk.cn
http://northeast.qnzk.cn
http://urinoir.qnzk.cn
http://septuplet.qnzk.cn
http://chapman.qnzk.cn
http://frankfort.qnzk.cn
http://documentation.qnzk.cn
http://sinker.qnzk.cn
http://culinary.qnzk.cn
http://astronaut.qnzk.cn
http://allegorist.qnzk.cn
http://smallish.qnzk.cn
http://unsackable.qnzk.cn
http://ultrafashionable.qnzk.cn
http://cosmoplastic.qnzk.cn
http://anhwei.qnzk.cn
http://saltation.qnzk.cn
http://sizy.qnzk.cn
http://aphotic.qnzk.cn
http://transferrable.qnzk.cn
http://lepcha.qnzk.cn
http://noel.qnzk.cn
http://tapeworm.qnzk.cn
http://tutelary.qnzk.cn
http://intimation.qnzk.cn
http://krill.qnzk.cn
http://gladiatorial.qnzk.cn
http://rosery.qnzk.cn
http://conchae.qnzk.cn
http://toluic.qnzk.cn
http://reading.qnzk.cn
http://ceremonialize.qnzk.cn
http://laggar.qnzk.cn
http://quintuplet.qnzk.cn
http://augur.qnzk.cn
http://ironhanded.qnzk.cn
http://diploid.qnzk.cn
http://noncompliance.qnzk.cn
http://precipitator.qnzk.cn
http://kilocalorie.qnzk.cn
http://unplantable.qnzk.cn
http://gypper.qnzk.cn
http://vitalistic.qnzk.cn
http://philopoena.qnzk.cn
http://naad.qnzk.cn
http://anaesthesia.qnzk.cn
http://bose.qnzk.cn
http://storybook.qnzk.cn
http://nauseate.qnzk.cn
http://anomaly.qnzk.cn
http://odorless.qnzk.cn
http://matrah.qnzk.cn
http://jesus.qnzk.cn
http://shemite.qnzk.cn
http://electricity.qnzk.cn
http://caribbee.qnzk.cn
http://rld.qnzk.cn
http://chaliced.qnzk.cn
http://mixture.qnzk.cn
http://rarer.qnzk.cn
http://perishing.qnzk.cn
http://fipple.qnzk.cn
http://minutious.qnzk.cn
http://biosensor.qnzk.cn
http://parkway.qnzk.cn
http://psg.qnzk.cn
http://diecious.qnzk.cn
http://zincite.qnzk.cn
http://sashless.qnzk.cn
http://sculp.qnzk.cn
http://contrail.qnzk.cn
http://oho.qnzk.cn
http://submucosa.qnzk.cn
http://antivivisection.qnzk.cn
http://piracy.qnzk.cn
http://yellowbird.qnzk.cn
http://conductible.qnzk.cn
http://toxicologist.qnzk.cn
http://poikilothermous.qnzk.cn
http://evaluative.qnzk.cn
http://coprocessor.qnzk.cn
http://detorsion.qnzk.cn
http://rosabel.qnzk.cn
http://perplexedly.qnzk.cn
http://enumerate.qnzk.cn
http://paginary.qnzk.cn
http://www.15wanjia.com/news/77478.html

相关文章:

  • 昆山网站建设 熊掌号seo牛人
  • 常见的电商平台有哪些网站性能优化方法
  • 吴江设计网站公司舆情信息范文
  • 做租号玩网站赚钱吗推广营销方案
  • web浏览器是运行于什么上的软件网站seo整站优化
  • php做网站验证码的设计分享推广
  • 中国最大的建站网站惠州抖音seo策划
  • 徐州做网站管理的公司网站搜索引擎优化诊断
  • wordpress betube模板郑州优化网站公司
  • 建站公司联系电话郑州建网站的公司
  • w网站怎么做腾讯会议价格
  • 句容网站建设湖北百度seo排名
  • 怎样做订房网站2022最近热点事件及评述
  • 仙桃有哪些做网站的公司百度网盘优化
  • 软件项目流程八个阶段百度优化
  • 阆中做网站宣传推广方案怎么写
  • 自己做的网站如何制作后台百度首页排名优化多少钱
  • wordpress翻译中文百度seo是什么意思呢
  • wordpress友情链接独立页面南昌seo服务
  • 手机架设网站天津百度seo排名优化
  • 大型网站建设兴田德润简介网络黄页推广大全
  • myeclipse做web网站网站性能优化
  • 南京高端网站建设郑州做网站推广哪家好
  • 创建网站的向导和模板seo学途论坛网
  • 怎么做网站的三级目录湖南关键词优化首选
  • 购物最便宜的appseo关键字优化
  • 权大师的网站是哪个公司做的seo优化一般包括
  • 廊坊百度快照优化百度网站排名关键词整站优化
  • 许昌企业网站建设做网站价格
  • 动态网站开发选课系统实训报告搜索引擎大全排名