国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > Java > 正文

Java實(shí)現(xiàn)計(jì)算器的代碼

2024-07-14 08:41:16
字體:
供稿:網(wǎng)友

用Java 實(shí)現(xiàn)的計(jì)算器,原理看代碼注釋,具體內(nèi)容如下

public class MyCalculator { public static void main(String[] args) {  String obj = "6+(8/2)+6/3+1*8 + 30";  ArrayList<String> arrayList = strFormat(obj);  System.out.println(obj + "=" + calculator(arrayList)); } /**  * 采用后續(xù)表達(dá)式計(jì)算結(jié)果  * 1、當(dāng)前字符串為數(shù)字時(shí),直接入棧  * 2、當(dāng)前字符串為計(jì)算符時(shí),取出棧中的前兩個(gè)計(jì)算  * 3、將計(jì)算結(jié)果再放入到棧中,棧中最后剩余的一個(gè)元素就是要求的結(jié)果  */ private static int calculator(ArrayList<String> obj) {  ArrayList<String> result = transform(obj);  System.out.println(result);  Stack<Integer> stack = new Stack<>();  for (int i = 0; i < result.size(); i++) {   String symbol = result.get(i);   if (isDigital(symbol)) { //數(shù)字直接入棧    stack.push(Integer.parseInt(symbol));   } else { // 處理操作符    int num1, num2;    num1 = stack.pop(); //取出兩個(gè)數(shù)    num2 = stack.pop();    switch (symbol) {     case "+":      stack.push(num2 + num1);      break;     case "-":      stack.push(num2 - num1);      break;     case "*":      stack.push(num2 * num1);      break;     case "/":      stack.push(num2 / num1);      break;     default:      break;    }   }  }  return stack.pop(); } /**  * 中序遍歷改為后續(xù)遍歷  */ private static ArrayList<String> transform(ArrayList<String> arrayList) {  Stack<String> stack = new Stack<>();  ArrayList<String> result = new ArrayList<>();  for (int index = 0; index < arrayList.size(); index++) {   String symbol = arrayList.get(index);   if (isDigital(symbol)) { //如果是數(shù)字直接輸出    result.add(symbol);   } else if (symbol.equals(")")) {    String tmp;    while (!(tmp = stack.pop()).equals("(")) { // 匹配成功后停止     result.add(tmp);    }   } else {    if (stack.isEmpty()) {     stack.push(symbol);     continue;    }    String tmp = stack.peek();    while (outPriority(symbol) <= inPriority(tmp)) { //優(yōu)先級(jí)小于棧內(nèi)優(yōu)先級(jí),一直出棧     result.add(tmp);     stack.pop();     if (stack.isEmpty()) {      break;     }     tmp = stack.peek();    }    stack.push(symbol);   }  }  //將剩余的出棧  while (!stack.isEmpty()) {   result.add(stack.pop());  }  return result; } /**  * 首先對(duì)String 進(jìn)行格式化 轉(zhuǎn)化成ArrayList  * @param src 3*5+8;  * @return ArrayList 3 * 5 + 8  */ private static ArrayList<String> strFormat(String src) {  if (src == null || src.equals("")) {   return null;  }  ArrayList<String> arrayList = new ArrayList<>();  StringBuilder comChar = new StringBuilder();  for (int i = 0; i <src.length(); i++) {   char ch = src.charAt(i);   if (ch == ' ') {    continue; //去除空格   }   if (!Character.isDigit(ch)) {    if (!comChar.toString().trim().equals("")) {     arrayList.add(comChar.toString().trim());     comChar.delete(0, comChar.length());    }    arrayList.add(ch + "");    continue;   }   comChar.append(ch);  }  if (!comChar.toString().trim().equals("")) {   arrayList.add(comChar.toString().trim());  }  return arrayList; } /**  * 判斷是否為數(shù)字  * @param symbol 782 或者 + - * /  * @return true or false  */ private static boolean isDigital(String symbol) {  return !symbol.equals("+") && !symbol.equals("-")    && !symbol.equals("*") && !symbol.equals("/")    && !symbol.equals("(") && !symbol.equals(")"); } private static int inPriority(String ch) {  switch (ch) {   case "+":   case "-":    return 2;   case "*":   case "/":    return 4;   case ")":    return 7;   case "(":    return 1;   default:    return 0;  } } private static int outPriority(String ch) {  switch (ch) {   case "+":   case "-":    return 3;   case "*":   case "/":    return 5;   case ")":    return 1;   case "(":    return 7;   default:    return 0;  } }}

以上全部為本篇文章的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲周县| 海伦市| 泌阳县| 兖州市| 武义县| 宁河县| 保山市| 桐庐县| 化德县| 湘西| 红安县| 曲周县| 隆德县| 积石山| 满洲里市| 蛟河市| 武宁县| 武夷山市| 江津市| 原平市| 开远市| 霍州市| 长岛县| 玉树县| 文成县| 师宗县| 广汉市| 镇坪县| 历史| 阿拉善右旗| 长葛市| 武宁县| 本溪市| 通江县| 遂平县| 延庆县| 绩溪县| 策勒县| 临西县| 汤原县| 德安县|