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

耒阳住房与建设局网站自创网站

耒阳住房与建设局网站,自创网站,怎么把自己做的网站发布,原生app开发工具目录 中缀表达式转后缀表达式 图解 代码实现过程: 完整代码: 利用后缀表达式求值: 完整代码: 首先我们得先了解逆波兰表达式。 中缀表达式转后缀表达式 所谓的中缀表达式其实就是我们平时写的例如:&#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://www.hkea.cn/news/487348/

相关文章:

  • 哪些网站教做生物实验今日新闻联播
  • 铜川市住房和城乡建设局网站信息流广告哪个平台好
  • 太原市建设交易中心网站首页百度手机助手app安卓版官方下载
  • 昆山网站建设网站建设郑州网络推广哪个好
  • 瑜伽网站设计国外推广网站
  • 什么网站做国外批发百度推广自己怎么做
  • 网站管理工具百度推广可以自己开户吗
  • 三水网站制作中山做网站推广公司
  • ysl网站设计论文郑州seo地址
  • 做食品的网站设计要注意片多多可以免费看电视剧吗
  • 网站排名推广自己怎么做长沙seo代理商
  • 手机网站改版公司加盟关键词优化排名查询
  • html5 图片网站建设企业网站多少钱
  • 企业网站定制开发流程网络营销的概念及特点
  • 做火影网站背景图农村电商平台有哪些
  • 国内html5网站建设seo兼职工资一般多少
  • 青海西宁网站建设公司百度网络推广
  • 服装公司网站设计百度站长收录入口
  • 做搜索关键词任务网站网站维护是什么意思
  • 2018什么做网站百度网盘网页版入口
  • 深圳福田大型商城网站建设石家庄最新疫情最新消息
  • 网站版面结构chatgpt 网站
  • 网站后期推广是谁来做广州百度推广开户
  • 不上此网站枉做男人免费制作网站平台
  • 防红短链接生成佛山抖音seo
  • 网站建设php带数据库模板站长工具四叶草
  • 做网站客户拖着不验收店铺推广渠道有哪些方式
  • 站群系统哪个好用怎样进行seo推广
  • 淄博网站建设方案网络推广是做什么的
  • 网站建设销售工作职责seo平台怎么样