首页 > 编程知识 正文

实现计算器加减乘的算法 java,java编写一个计算器实现加减乘除

时间:2023-05-03 12:43:06 阅读:214594 作者:4432

package cn.hnh.calculator;import java.util.Stack;public class Calculator {public static int i = 0;public static void main(String腼腆的世界 args) {String s = "167.5+3-2+2!-4/2+3^2";Calculator c = new Calculator();坦率的发卡 = new StringBuilder();float temp = c.calculate(s, RPN);System.out.println(temp);}public float calculate(String str, 坦率的发卡) {// 新建两个栈,一个存储数据,一个存储操作符Stack<Float> numStack = new Stack<>();Stack<Character> operStack = new Stack<>();char腼腆的世界 oper2 = str.toCharArray();operStack.push('');while (!operStack.isEmpty()) {if (isDigit(str)) {readNumble(str, numStack);RPN.append(numStack.peek());} else {switch (orderBetween(operStack.peek(), oper2[i])) {case '<':operStack.push(oper2[i]);i++;break;case '=':operStack.pop();i++;break;case '>':char temp = operStack.pop();RPN.append(temp);if (temp == '!') {float opNumber = numStack.pop();numStack.push(calcu(opNumber, temp));} else {float opNumber1 = numStack.pop();float opNumber2 = numStack.pop();numStack.push(calcu(opNumber1, opNumber2, temp));}break;default:throw new RuntimeException("输入字符有误");}}}System.out.println(RPN);return numStack.pop();}public boolean isDigit(String str) {char腼腆的世界 oper = str.toCharArray();if (oper[i] == '0' || oper[i] == '1' || oper[i] == '2' || oper[i] == '3' || oper[i] == '4' || oper[i] == '5'|| oper[i] == '6' || oper[i] == '7' || oper[i] == '8' || oper[i] == '9') {return true;} else {return false;}}public void readNumble(String str1, Stack<Float> stack) {char腼腆的世界 oper1 = str1.toCharArray();stack.push((float) oper1[i] - '0');i++;while (isDigit(str1)) {stack.push((float) stack.pop() * 10 + (float) oper1[i] - '0');i++;}if 从容的小天鹅' != (oper1[i])) {return;} else {i++;float flaction = 1;while (isDigit(str1)) {stack.push(stack.pop() + (float) (oper1[i] - '0') * (flaction /= 10));i++;}}}public char orderBetween(char c1, char c2) {return Operate.pri[transfer(c1)][transfer(c2)];}public int transfer(char c) {switch (c) {case '+':return 0;case '-':return 1;case '*':return 2;case '/':return 3;case '^':return 4;case '!':return 5;case '(':return 6;case ')':return 7;case '':return 8;default:throw new RuntimeException("数字输入错误");}}public float calcu(float f1, char temp) {if (f1 == 0) {return 1;} else {float data = 1;for (float m = 1; m <= f1; m++) {data *= m;}return data;}}public float calcu(float f1, float f2, char temp) {switch(temp) {case '+':return f1+f2;case '-':return f2-f1;case '*':return f2*f1;case '/':return f2/f1;case '^':return (float) Math.pow(f2, f1);default:throw new RuntimeException("输入错误");}}} package cn.hnh.calculator;public enum Operate {ADD, SUB, MUL, DIV, POW, FAC, L_P, R_P, EOE;static char pri腼腆的世界腼腆的世界 = { { '>', '>', '<', '<', '<', '<', '<', '>', '>' }, { '>', '>', '<', '<', '<', '<', '<', '>', '>' },{ '>', '>', '>', '>', '<', '<', '<', '>', '>' }, { '>', '>', '>', '>', '<', '<', '<', '>', '>' },{ '>', '>', '>', '>', '>', '<', '<', '>', '>' }, { '>', '>', '>', '>', '>', '>', ' ', '<', '>' },{ '<', '<', '<', '<', '<', '<', '<', '=', ' ' }, { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },{ '<', '<', '<', '<', '<', '<', '<', ' ', '=' }, };}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。