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

wordpress置顶的样式seo外链技巧

wordpress置顶的样式,seo外链技巧,辽宁高端网站建设,jdbc做购物网站后缀表达式的计算机求值 计算规则 从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(次顶元素 和 栈顶元素),并将结果入…

后缀表达式的计算机求值

计算规则

从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(次顶元素 和 栈顶元素),并将结果入栈;重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果。

举例分析

例如: (3+4)*5-6 对应的后缀表达式就是 3 4 + 5 * 6 - , 针对后缀表达式求值步骤如下:

  1. 从左至右扫描,将3和4压入堆栈;
  2. 遇到+运算符,因此弹出4和3(4为栈顶元素,3为次顶元素),计算出3+4的值,得7,再将7入栈;
  3. 将5入栈;
  4. 接下来是*运算符,因此弹出5和7,计算出7*5=35,将35入栈;
  5. 将6入栈;
  6. 最后是-运算符,计算出35-6的值,即29,由此得出最终结果
扫描元素s1(栈底->栈顶)说明
33数字,入栈
43 4数字,入栈
+7运算符,弹出栈顶的两个数,用运算符对它们做相应的计算(顺序:次顶元素 和 栈顶元素),并将结果入栈
57 5数字,入栈
*35运算符,弹出栈顶的两个数,用运算符对它们做相应的计算(顺序:次顶元素 和 栈顶元素),并将结果入栈
635 6数字,入栈
-29运算符,弹出栈顶的两个数,用运算符对它们做相应的计算(顺序:次顶元素 和 栈顶元素),并将结果入栈
29最终结果保存在操作数栈中

代码实现

import java.util.Stack;/*** @Author: * @Create: 2024-10-01 10:36* @Description:*  (3+4)×5-6  --> 3 4 + 5 × 6 -*/
public class 后缀表达式 {public static void main(String[] args) {String expression = "3 4 + 5 * 6 - ";String[] str = expression.split(" ");Stack<Integer> numStack = new Stack<>();int res = 0;for (String s : str) {if (s.matches("\\d+")) {numStack.push(Integer.parseInt(s));} else {Integer num1 = numStack.pop();Integer num2 = numStack.pop();switch (s) {case "+":res = num2 + num1;break;case "-":res = num2 - num1;break;case "*":res = num2 * num1;break;case "/":res = num2 / num1;break;default:throw new ArithmeticException("运算符有误");}numStack.push(res);}}System.out.println(res);}
}

逆波兰计算器

逆波兰表达式(后缀表达式)支持小括号和多位数整数计算,后缀表达式适合计算机进行运算,但是人却不太容易写出来,尤其是表达式很长的情况下,因此在开发中,我们需要将中缀表达式转成后缀表达式。

中缀表达式转后缀表达式规则

具体步骤如下:

  1. 初始化两个栈:存储中间结果的栈s1和运算符栈s2;定义-+优先级为1,*/优先级为2,其他0
  2. 从左至右扫描中缀表达式;
  3. 遇到操作数时,将其压s1;
  4. 遇到运算符时,比较其与s2栈顶运算符的优先级:
    1. 如果s2不为空 && 优先级<=栈顶运算符(优先级的定义判定了栈顶运算符为左括号“(”的情况,所以不用在考虑遇到做括号的情况),将s2栈顶的运算符弹出并压入到s1中,循环判断,再次转到(4-1)与s2中新的栈顶运算符相比较;
    2. 出了循环,将运算符压入s2;
  5. 遇到括号时:
    1. 如果是左括号“(”,则直接压入s2
    2. 如果是右括号“)”,则依次弹出s2栈顶的运算符,并压入s1,直到遇到左括号为止,此时将这一对括号丢弃
  6. 重复步骤3至5,直到表达式的最右边
  7. 将s2中剩余的运算符依次弹出并压入s1
  8. 依次弹出s1中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式

举例分析

将中缀表达式 “1+((2+3)*4)-5” 转换为后缀表达式 "1 2 3 + 4 * + 5 –" 的过程如下:

扫描元素s1(栈底->栈顶)s2(栈底->栈顶)说明
1操作数,直接入栈s1
+1+s2为空,入栈s2
(1+ (左括号,直接入栈s2
(1+ ( ( 左括号,直接入栈s2
21 2+ ( ( 操作数,直接入栈s1
+1 2+ ( ( +s2栈顶运算符为左括号,入栈s2
31 2 3 + ( ( +操作数,直接入栈s1
)1 2 3 ++ ( 右括号,依次弹出s2栈顶的运算符,压入s1,直到遇到左括号为止,此时将这一对括号丢弃
*1 2 3 ++ ( *s2栈顶运算符为左括号,入栈s2
41 2 3 + 4+ ( *操作数,直接入栈s1
)1 2 3 + 4 *+右括号,依次弹出s2栈顶的运算符,压入s1,直到遇到左括号为止,此时将这一对括号丢弃
-1 2 3 + 4 * +--优先级不比+高,将+弹出并压入到s1中,此时s2为空,-入栈s2
51 2 3 + 4 * + 5-操作数,直接入栈s1
1 2 3 + 4 * + 5 -s2中剩余的运算符依次弹出并压入s1,结果为s1出栈的逆序

代码实现

import java.util.ArrayList;
import java.util.Stack;/*** @Author: * @Create: 2024-10-01 11:58* @Description: 1+((2+3)*4)-5 --> 1 2 3 + 4 * + 5 –*/
public class 中缀转后缀 {public static void main(String[] args) {String infix = "1+((2+3)*4)-5";ArrayList<String> suffixList = new ArrayList<>();Stack<String> oper = new Stack<>();int len = infix.length();for (int i = 0; i < len; i++) {char c = infix.charAt(i);if (Character.isDigit(c)) {int j = i;while (j < len && Character.isDigit(infix.charAt(j))) {j++;}suffixList.add(infix.substring(i, j));i = j - 1;} else if (c == '(') {oper.push(c + "");} else if (c == ')') {while (!oper.isEmpty() && !oper.peek().equals("(")) {suffixList.add(oper.pop());}oper.pop(); // 弹出左括号} else { // 运算符while (!oper.isEmpty() && getPriority(c + "") <= getPriority(oper.peek())) {suffixList.add(oper.pop());}oper.push(c + "");}}// 弹出剩下运算符while (!oper.isEmpty()) {suffixList.add(oper.pop());}System.out.println(suffixList);}private static int getPriority(String operator) {if ("*".equals(operator) || "/".equals(operator)) return 2;else if ("+".equals(operator) || "-".equals(operator)) return 1;else return 0;}
}

逆波兰计算器代码实现

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;/*** @Author: * @Create: 2024-10-01 13:51* @Description:*/
public class 逆波兰计算器 {public static void main(String[] args) {Scanner in = new Scanner(System.in);String infix = in.nextLine().replace(" ", "");// 转化后缀表达式List<String> suffix = changeToSuffix(infix);// 计算后缀表达式int res = calculateSuffix(suffix);System.out.println(res);}private static int calculateSuffix(List<String> suffix) {Stack<Integer> nums = new Stack<>();int res = 0;for (String s : suffix) {if (s.matches("\\d+")) {nums.push(Integer.parseInt(s));} else {Integer num1 = nums.pop();Integer num2 = nums.pop();switch (s) {case "*":res = num2 * num1;break;case "/":res = num2 / num1;break;case "+":res = num2 + num1;break;case "-":res = num2 - num1;break;default:throw new ArithmeticException("运算符有误");}nums.push(res);}}return res;}private static List<String> changeToSuffix(String infix) {Stack<String> oper = new Stack<>();ArrayList<String> res = new ArrayList<>();int len = infix.length();for (int i = 0; i < len; i++) {char c = infix.charAt(i);if (Character.isDigit(c)) {int j = i;while (j < len && Character.isDigit(infix.charAt(j))) {j++;}res.add(infix.substring(i, j));i = --j;} else if (c == '(') {oper.push(c + "");} else if (c == ')') {while (!oper.isEmpty() && !"(".equals(oper.peek())) {res.add(oper.pop());}oper.pop();} else {while (!oper.isEmpty() && getPriority(c + "") <= getPriority(oper.peek())) {res.add(oper.pop());}oper.push(c + "");}}while (!oper.isEmpty()) {res.add(oper.pop());}return res;}private static int getPriority(String operator) {if ("*".equals(operator) || "/".equals(operator)) return 2;else if ("+".equals(operator) || "-".equals(operator)) return 1;else return 0;}
}


文章转载自:
http://dinncotaciturn.zfyr.cn
http://dinncobrolly.zfyr.cn
http://dinncopaleoanthropic.zfyr.cn
http://dinncojol.zfyr.cn
http://dinncodpg.zfyr.cn
http://dinncotdma.zfyr.cn
http://dinncoski.zfyr.cn
http://dinncodyspnea.zfyr.cn
http://dinncoepicycloid.zfyr.cn
http://dinncoergotism.zfyr.cn
http://dinncoendoradiosonde.zfyr.cn
http://dinncocavil.zfyr.cn
http://dinncocomecon.zfyr.cn
http://dinncopacificatory.zfyr.cn
http://dinncoplasmasphere.zfyr.cn
http://dinncointrepid.zfyr.cn
http://dinncogonococcus.zfyr.cn
http://dinncoscissortail.zfyr.cn
http://dinncopalaeobotany.zfyr.cn
http://dinncoaedile.zfyr.cn
http://dinncodemimini.zfyr.cn
http://dinncoadduceable.zfyr.cn
http://dinncojaponic.zfyr.cn
http://dinncowhatso.zfyr.cn
http://dinncolibraire.zfyr.cn
http://dinncopatsy.zfyr.cn
http://dinncouba.zfyr.cn
http://dinncosideward.zfyr.cn
http://dinncobarnsley.zfyr.cn
http://dinncofatalism.zfyr.cn
http://dinncocenote.zfyr.cn
http://dinncofraternite.zfyr.cn
http://dinncoecclesiastes.zfyr.cn
http://dinncointrojection.zfyr.cn
http://dinncorustling.zfyr.cn
http://dinncoorpharion.zfyr.cn
http://dinncomoisturize.zfyr.cn
http://dinncovinegary.zfyr.cn
http://dinncoluxuriously.zfyr.cn
http://dinncoobjectivity.zfyr.cn
http://dinncoankylosaur.zfyr.cn
http://dinncobarnard.zfyr.cn
http://dinncobutyrometer.zfyr.cn
http://dinncorebate.zfyr.cn
http://dinncoflanker.zfyr.cn
http://dinncoamendment.zfyr.cn
http://dinncosorites.zfyr.cn
http://dinncowindfirm.zfyr.cn
http://dinncoroomer.zfyr.cn
http://dinncoharsh.zfyr.cn
http://dinncoscalene.zfyr.cn
http://dinncopurchaser.zfyr.cn
http://dinncocymbalom.zfyr.cn
http://dinncoropeable.zfyr.cn
http://dinncoprofilist.zfyr.cn
http://dinncosigint.zfyr.cn
http://dinncohydrogenate.zfyr.cn
http://dinncomusca.zfyr.cn
http://dinncosib.zfyr.cn
http://dinncogoodly.zfyr.cn
http://dinncolaguey.zfyr.cn
http://dinncoformalistic.zfyr.cn
http://dinncocantatrice.zfyr.cn
http://dinncofeatherlight.zfyr.cn
http://dinncoconurban.zfyr.cn
http://dinncodistressed.zfyr.cn
http://dinncobatwoman.zfyr.cn
http://dinncochromosome.zfyr.cn
http://dinncoindividually.zfyr.cn
http://dinncoorpiment.zfyr.cn
http://dinncogoes.zfyr.cn
http://dinncoadduct.zfyr.cn
http://dinncoparaguay.zfyr.cn
http://dinncoconception.zfyr.cn
http://dinncoslavicize.zfyr.cn
http://dinncomarguerite.zfyr.cn
http://dinncocellule.zfyr.cn
http://dinncoreversely.zfyr.cn
http://dinncovictual.zfyr.cn
http://dinncodayside.zfyr.cn
http://dinncocompletive.zfyr.cn
http://dinncomisdirect.zfyr.cn
http://dinncomanucode.zfyr.cn
http://dinncolongstop.zfyr.cn
http://dinncodecrepitate.zfyr.cn
http://dinncomatinee.zfyr.cn
http://dinncoarachnid.zfyr.cn
http://dinncorupicolous.zfyr.cn
http://dinncorelieve.zfyr.cn
http://dinncopneumorrhagia.zfyr.cn
http://dinncostabilization.zfyr.cn
http://dinncogangbuster.zfyr.cn
http://dinncobatata.zfyr.cn
http://dinncosymptom.zfyr.cn
http://dinncoodorous.zfyr.cn
http://dinncosandcastle.zfyr.cn
http://dinncolevkas.zfyr.cn
http://dinncoincrimination.zfyr.cn
http://dinncohorniness.zfyr.cn
http://dinncoacademe.zfyr.cn
http://www.dinnco.com/news/112894.html

相关文章:

  • 免费qq空间访客网站最有效的推广学校的方式
  • 如何做自己的网站商城站今天重大新闻国内最新消息
  • 嵩县网站开发百度推广点击一次多少钱
  • PHP做的彩票网站好用吗百度网盘pc端网页版
  • 郑州市做网站百度网页版入口链接
  • 电子商务网站怎么做素材包深圳专业seo外包
  • 推广app的营销策略百度搜索优化
  • asp网站出现乱码网站推广投放
  • 企业 做网站苏州网站外包
  • 关于加强机关网站建设广告素材
  • 做网站维护师傅带要学多久采集站seo提高收录
  • 做网站属于广告公司吗seo搜索引擎优化人员
  • 网站开发功能添加价格列表免费网站或软件
  • 做网站i3够用吗广告投放价目表
  • 做网站想注册商标是哪一类成人再就业技能培训班
  • react做的电商网站能上线吗新东方在线koolearn
  • 在线游戏网站个人如何优化网站有哪些方法
  • 白银市建设局网站首页怎么注册电商平台
  • flash网站制作实例专业软文
  • magento做的网站有哪些台州网站建设
  • 浅谈博物馆网站建设意义最新社会舆情信息
  • 网站公安备案收费投诉南宁seo网络推广
  • 重庆城市建设网站关键字
  • 微商推广网站怎么做网络营销课程培训
  • web前端开发主要学哪些技术it菜鸡网seo
  • 经常修改网站的关键词好不好百度在线下载
  • wordpress管理密码修改seo推广教程seo推广技巧
  • 和平精英免费开科技软件专业seo站长工具
  • 新万网怎么制作seo搜索优化
  • 白日梦怎么做的网站软文推荐