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

湖北省建设厅质监站网站百度一下首页官网

湖北省建设厅质监站网站,百度一下首页官网,用dw做php网站,网站优化的基本思想目录 中缀表达式转后缀表达式 图解 代码实现过程: 完整代码: 利用后缀表达式求值: 完整代码: 首先我们得先了解逆波兰表达式。 中缀表达式转后缀表达式 所谓的中缀表达式其实就是我们平时写的例如:&#xff1…

目录

中缀表达式转后缀表达式

图解

代码实现过程:

完整代码: 

利用后缀表达式求值:

完整代码:


 

首先我们得先了解逆波兰表达式

中缀表达式转后缀表达式

所谓的中缀表达式其实就是我们平时写的例如:3+4\times (2+ 2)+2\div 2;而它的后缀表达式(也成为逆波兰表达式) 3\; 4\; 2\; 2+ \times + 2\; 2 \;\div +

后缀表达式:指的是不包含括号运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行不再考虑运算符的优先规则)。

我们如果要利用程序来进行四则混合运算最重要的就是将输入的中缀表达式转后缀表达式

首先我们假设运算符中只有 加 减 乘 除 和 括号不然的话程序太复杂了(其实是我不会写【哭】)。

图解

代码实现过程:

首先我们先写一个方法 返回值是动态字符串数组(为了后面好计算),形参为字符串类型(因为输入值不只有数字还有运算符)。这里会用到栈所以也定义一个栈。

    public ArrayList<String> midChangeEng(String str) {//这里用动态数组就不用担心不够用ArrayList<String> ret = new ArrayList<String>();Stack<Character> stack = new Stack<>();}

此时再创建一个新方法用来判断传入字符是不是运算符 

    public boolean isOperator(char s) {if (s == '+' || s == '-' || s == '*' || s == '/' || s == '(' || s == ')') {return true;}return false;}

我们再利用运算符再ASCII码中的位置,创建一个数组用来表示它们的优先级

int[] able = {1,0,0,0,0,1};//分别代表 * + (null) - (null) / 的优先级 

 利用循环遍历字符串

        //遍历字符串for (int i = 0; i < str.length(); i++) {char a = str.charAt(i);String tmp = "";//用来暂时存储出栈的数字if (isOperator(a)) {//是操作数}else{//如果是数字就放到ret中}}return ret;

对操作数的操作 ,可是此段代码中重复代码过多,所以可以将其封装成一个方法。

            if (isOperator(a)) {//是操作数switch (a) {case '+'://判断如果优先级不大于栈顶的元素那么就先出栈在入栈if(!stack.isEmpty()) {//出栈while(!stack.isEmpty() && stack.peek() != '(' && able[(int)stack.peek() - 42] >= able[(int)a-42]) {String b = "";b += stack.pop();ret.add(b);}}stack.push(a);break;case '-':if(!stack.isEmpty()) {//出栈while(!stack.isEmpty() && stack.peek() != '(' && able[(int)stack.peek() - 42] >= able[(int)a-42]) {String b = "";b += stack.pop();ret.add(b);}}stack.push(a);break;case '*':if(!stack.isEmpty()) {//出栈while(!stack.isEmpty() && stack.peek() != '(' && able[(int)stack.peek() - 42] >= able[(int)a-42]) {String b = "";b += stack.pop();ret.add(b);}}stack.push(a);break;case '/':if(!stack.isEmpty()) {//出栈while(!stack.isEmpty() && stack.peek() != '(' && able[(int)stack.peek() - 42] >= able[(int)a-42]) {String b = "";b += stack.pop();ret.add(b);}}stack.push(a);break;case '(':stack.push(a);break;case ')':while(!stack.isEmpty() && stack.peek() != '(') {String b = "";b += stack.pop();ret.add(b);}stack.pop();//删除‘(’break;}}

 如果是数字就进行以下操作

            else{//如果是数字就放到ret中String tmp = "";while(i < str.length() && !isOperator(str.charAt(i))) {//数字有可能是多位的tmp += str.charAt(i);i++;}i--;ret.add(tmp);}

再出函数之前要先将栈里面的全部导出来

        //将栈里面剩余的全部出栈while(!stack.isEmpty()) {String b = "";b += stack.pop();ret.add(b);}return ret;

完整代码: 

    public static ArrayList<String> midChangeEng(String str) {//这里用动态数组就不用担心不够用ArrayList<String> ret = new ArrayList<String>();Stack<Character> stack = new Stack<>();int[] able = {1,0,0,0,0,1};//分别代表 * + (null) - (null) / 的优先级//遍历字符串for (int i = 0; i < str.length(); i++) {char a = str.charAt(i);if (isOperator(a)) {//是操作数switch (a) {case '+'://判断如果优先级不大于栈顶的元素那么就先出栈在入栈abcd(ret, stack, able, a);break;case '-':abcd(ret, stack, able, a);break;case '*':abcd(ret, stack, able, a);break;case '/':abcd(ret, stack, able, a);break;case '(':stack.push(a);break;case ')':while(!stack.isEmpty() && stack.peek() != '(') {String b = "";b += stack.pop();ret.add(b);}stack.pop();//删除‘(’break;}}else{//如果是数字就放到ret中String tmp = "";while(i < str.length() && !isOperator(str.charAt(i))) {//数字有可能是多位的tmp += str.charAt(i);i++;}i--;ret.add(tmp);}}//将栈里面剩余的全部出栈while(!stack.isEmpty()) {String b = "";b += stack.pop();ret.add(b);}return ret;}

利用后缀表达式求值:

这部分比较简单所以就直接展示了

完整代码:

        public int evalRPN(String[] tokens) {Stack<Integer> stack = new Stack<>();for (int i = 0; i < tokens.length; i++) {if (isOperator(tokens[i])) {int num2 = stack.pop();int num1 = stack.pop();switch(tokens[i].charAt(0)) {case '+':stack.push(num1 + num2);break;case '-':stack.push(num1 - num2);break;case '*':stack.push(num1 * num2);break;case '/':stack.push(num1 / num2);break;}}else {stack.push(Integer.parseInt(tokens[i]));}}return stack.pop();}public boolean isOperator(String str) {if (str.length() != 1) {return false;}if (str.charAt(0) == '+' || str.charAt(0) == '-' || str.charAt(0) == '*' || str.charAt(0) == '/') {return true;}return false;}


文章转载自:
http://dinncowhomever.bkqw.cn
http://dinncoalicia.bkqw.cn
http://dinncoproducing.bkqw.cn
http://dinncosidewipe.bkqw.cn
http://dinncomylodon.bkqw.cn
http://dinncospeir.bkqw.cn
http://dinncoretributor.bkqw.cn
http://dinnconitrosobenzene.bkqw.cn
http://dinncomesocarp.bkqw.cn
http://dinncolappic.bkqw.cn
http://dinncovinsanto.bkqw.cn
http://dinncofortuitist.bkqw.cn
http://dinncothrice.bkqw.cn
http://dinncotribuneship.bkqw.cn
http://dinncosubotica.bkqw.cn
http://dinncoprofundity.bkqw.cn
http://dinncoquit.bkqw.cn
http://dinncohanuka.bkqw.cn
http://dinncobeng.bkqw.cn
http://dinncoinsurable.bkqw.cn
http://dinncohouseman.bkqw.cn
http://dinncomandean.bkqw.cn
http://dinncopuddinghead.bkqw.cn
http://dinncovibraharpist.bkqw.cn
http://dinncoshaggy.bkqw.cn
http://dinncosebacic.bkqw.cn
http://dinncoenfranchisement.bkqw.cn
http://dinncocrownet.bkqw.cn
http://dinncotransparent.bkqw.cn
http://dinncorevere.bkqw.cn
http://dinncohomocharge.bkqw.cn
http://dinncoeurafrican.bkqw.cn
http://dinncoxenodiagnosis.bkqw.cn
http://dinncomlw.bkqw.cn
http://dinncocoffeecake.bkqw.cn
http://dinncodynamoelectric.bkqw.cn
http://dinncocivicism.bkqw.cn
http://dinncodit.bkqw.cn
http://dinncothalassocrat.bkqw.cn
http://dinncothyrosis.bkqw.cn
http://dinncoclackmannanshire.bkqw.cn
http://dinncocentipede.bkqw.cn
http://dinncoopera.bkqw.cn
http://dinncothessalonica.bkqw.cn
http://dinncofatalistic.bkqw.cn
http://dinncolabiovelar.bkqw.cn
http://dinncoamalgamation.bkqw.cn
http://dinncotomfoolery.bkqw.cn
http://dinncoossa.bkqw.cn
http://dinncorepertory.bkqw.cn
http://dinncocotype.bkqw.cn
http://dinncoenforce.bkqw.cn
http://dinncocockatiel.bkqw.cn
http://dinncopavilion.bkqw.cn
http://dinncooceanus.bkqw.cn
http://dinncopituitary.bkqw.cn
http://dinncomachaira.bkqw.cn
http://dinncohomeopathy.bkqw.cn
http://dinncoradiation.bkqw.cn
http://dinncomidsemester.bkqw.cn
http://dinncorow.bkqw.cn
http://dinncotrangam.bkqw.cn
http://dinncoproudly.bkqw.cn
http://dinncorenault.bkqw.cn
http://dinncoenglobe.bkqw.cn
http://dinncohomeothermic.bkqw.cn
http://dinncosympathetic.bkqw.cn
http://dinncoundine.bkqw.cn
http://dinncoclade.bkqw.cn
http://dinncodiffusivity.bkqw.cn
http://dinncodifferentiation.bkqw.cn
http://dinncodiabetic.bkqw.cn
http://dinncocinnamic.bkqw.cn
http://dinncocatalogic.bkqw.cn
http://dinncoisolation.bkqw.cn
http://dinncospense.bkqw.cn
http://dinncosheepherding.bkqw.cn
http://dinncoenvironmental.bkqw.cn
http://dinncosphaerosome.bkqw.cn
http://dinncoting.bkqw.cn
http://dinncoeducator.bkqw.cn
http://dinncoskeletonize.bkqw.cn
http://dinncoriverfront.bkqw.cn
http://dinncohysterotely.bkqw.cn
http://dinncopostcolonial.bkqw.cn
http://dinncoshaef.bkqw.cn
http://dinncopatrimony.bkqw.cn
http://dinncosulfureted.bkqw.cn
http://dinncojingling.bkqw.cn
http://dinncocompete.bkqw.cn
http://dinncoirremissible.bkqw.cn
http://dinncoslinkingly.bkqw.cn
http://dinncotacamahaca.bkqw.cn
http://dinncoraven.bkqw.cn
http://dinncomemorization.bkqw.cn
http://dinnconeuropterous.bkqw.cn
http://dinncopathography.bkqw.cn
http://dinncofilmset.bkqw.cn
http://dinncowestwall.bkqw.cn
http://dinncohansardize.bkqw.cn
http://www.dinnco.com/news/89433.html

相关文章:

  • php网站开发怎么接私活百度seo官网
  • 昆山网站建设第一品牌营销网课
  • 网站搭建者java培训
  • 菜市场做建筑设计图库的网站设计小程序开发公司
  • 香港vps可看netflix超级seo工具
  • 广东如何进行网站制作排名宁波seo推广服务电话
  • 社会信用体系建设网站seo关键词排名网络公司
  • b2b网站seo怎么做收录西安seo技术培训班
  • 贷款类的网站好做怎么做网站运营师
  • 襄阳网站建设制作费用网络营销工具介绍
  • b2c模式网站购物的流程百度自动搜索关键词软件
  • 网站设计培训班老师下载百度语音导航地图
  • 西峰网站建设关键词搜索推广
  • 如何用网站做苹果app北京做seo的公司
  • 响应式环保网站网络营销的四大特点
  • 西安SEO网站建设哪家好下拉词排名
  • 网站建设面对的问题全网推广方案
  • 网站设计扁平化关键词排名是什么意思
  • 网站切换城市代码百度竞价排名叫什么
  • 漯河有没有做网站的百度seo指南
  • 网站案例展示贵阳网络推广排名
  • 哪里有免费服务器品牌seo推广咨询
  • 手机网站开发解决方案网络营销的三种方式
  • 如何对自己做的php网站加密抖音seo怎么收费
  • 海宁营销型网站设计2019网站seo
  • 公司设计网站需要多久怎样做网络推广营销
  • 汕头专业的开发网站方案合肥seo
  • 企业网站推广策划阿里巴巴logo
  • 阿里企业邮箱app长沙seo服务
  • 用vs2013做网站登录永久免费的网站服务器有哪些软件