个人开发流程(四则运算) --孙瑞瑞

设计一个简单的四则运算

1.计算简单的四则运算,并在控制台中输入结果

2.核心代码

复制代码
package program.example;
import java.util.Scanner;
import java.util.Stack;
import java.util.Arrays;
import java.util.Scanner;
import org.junit.Test;
 
//四则运算
public class YunSuan {
  private static Stack<Character> stack = new Stack<>();
  private static Stack<Character> stack_1 = new Stack<>();
  private static Stack<Character> stack_2 = new Stack<>();
  public static void main(String[] args) {
      String str = "3+(2-3/4)*5";
      try {
          Double b = calculate(str);
          System.out.println("运算结果为:"+b);
      }catch (Exception e){
          System.out.println("error");
      }
  }
  private static Double calculate(String str) throws Exception{
      //字符串转化为字符数组
      char[] arr = str.toCharArray();
      for(int i=0;i<arr.length;i++){
          //判断一个char是否为数字
          if(Character.isDigit(arr[i])){
              stack.push(arr[i]);
          }else if(arr[i] == '*'||arr[i] == '/'){
              while(!stack_1.empty()){
                  char ch = stack_1.pop();
                  if(ch == '('){
                      stack_1.push(ch);
                      break;
                  }else if(ch == '*' || ch == '/'){
                      stack.push(ch);
                  }else{
                      stack_2.push(ch);
                  }
              }
              while(!stack_2.empty()){
                  stack_1.push(stack_2.pop());
              }
              stack_1.push(arr[i]);
          } else if(arr[i] == '+'|| arr[i] == '-'){
              while(!stack_1.empty()){
                  char ch = stack_1.pop();
                  if(ch == '('){
                      stack_1.push(ch);
                      break;
                  }else if(ch == '*' || ch == '/'||ch == '+'|| ch == '-'){
                      stack.push(ch);
                  }else{
                      stack_2.push(ch);
                  }
              }while(!stack_2.empty()){
                  stack_1.push(stack_2.pop());
              }
              stack_1.push(arr[i]);
          }else if(arr[i] == '('){
              stack_1.push(arr[i]);
          }else if(arr[i] == ')'){
              char ch = stack_1.peek();
              while(ch != '('){
                  ch = stack_1.pop();
                  stack.push(ch);
              }
              stack.pop();
          }else{
              throw new Exception();
          }
      }
      while(!stack_1.empty()){
          stack.push(stack_1.pop());
      }
      //进行运算
      while(!stack.empty()){
          stack_2.push(stack.pop());
      }
      Stack<Double> s = new Stack<>();//用于最后计算的栈
      while(!stack_2.empty()){
          char ch = stack_2.pop();
          if(ch == '*' || ch == '/'||ch == '+'|| ch == '-'){
              double sum = 0;
              double num1= s.pop();
              double num2= s.pop();
              switch (ch){
                  case '*':sum = num2*num1;
                  break;
                  case '/':sum = num2/num1;
                  break;
                  case '+':sum = num2+num1;
                  break;
                  case '-':sum = num2-num1;
                  break;
              }
              s.push(sum);
          }else if (Character.isDigit(ch)){
              //使用函数
              s.push((double)Character.getNumericValue(ch));
          }else{
              throw new Exception();
          }
      }
      return s.pop();
  }
}
复制代码

3.运算结果输入:

 4.PSP对照表

5.总结

写代码的过程太过艰难,代码检测耗费时长太多,许多的方法都不太熟练,需要去网上查找资料。写的过程磕磕绊绊,遇到了许多的问题。但是第一次做个人技术流程,也学到了很多的东西。

原文地址:https://www.cnblogs.com/tqz521127/p/14643783.html