递归解析计算器java

nqwrtyyt  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(453)

我正在尝试用java创建一个递归解析计算器,用于加法、乘法和阶乘,但是在第一部分,我正在努力阅读用户输入,以便将输入拆分为数字和运算符。在调试时,我试着看它哪里出错了,我发现当“+”遍历if else语句时,它只是跳过了它。我真的不确定是什么问题,我最初尝试使用令牌,并分裂成子字符串,但它也不顺利,然后。任何帮助都将不胜感激。谢谢您

  1. package com.company;
  2. import java.util.Scanner;
  3. class Main {
  4. public static void main(String[] param) {
  5. String input = input("Please enter an expression");
  6. int n = input.length()-1;
  7. String[] splitter = input.split("(?<=\\G.)");
  8. split(input, n);
  9. //int result = calculate(input);
  10. //String[] splitter = input.split("(?<=\\G.)");
  11. }
  12. public static String split(String input, int n) {
  13. String[] splitter = input.split("(?<=\\G.)");
  14. System.out.println(splitter[n]);
  15. String symbol = splitter[n];
  16. if (symbol.equals("+")) {
  17. evalADD(n, splitter);
  18. }
  19. if (symbol.equals("*")) {
  20. evalMULT(n, splitter);
  21. }
  22. if (symbol.equals("!")) {
  23. evalFACT(n, splitter);
  24. }
  25. else if (Integer.parseInt(splitter[n]) >= 0 && Integer.parseInt(splitter[n]) <=9)
  26. {
  27. if (n != 0) {
  28. n = n - 1;
  29. split(input, n);
  30. }
  31. }
  32. if (n != 0)
  33. n = n - 1;
  34. split(input, n);
  35. return input;
  36. }
  37. public static int evalADD(int n, String [] splitter){
  38. int arg1;
  39. int arg2;
  40. int result;
  41. arg1 = Integer.parseInt(splitter[n+1]);
  42. arg2 = Integer.parseInt(splitter[n+2]);
  43. result = arg1 + arg2;
  44. return result;
  45. }
  46. public static int evalMULT(int n, String [] splitter){
  47. int arg1;
  48. int arg2;
  49. int result;
  50. arg1 = Integer.parseInt(splitter[n+1]);
  51. arg2 = Integer.parseInt(splitter[n+2]);
  52. result = arg1 * arg2;
  53. return result;
  54. }
  55. public static int evalFACT(int n, String [] splitter){
  56. int arg1;
  57. int arg2;
  58. int result;
  59. arg1 = Integer.parseInt(splitter[n+1]);
  60. arg2 = Integer.parseInt(splitter[n+2]);
  61. result = arg1 - arg2;
  62. return result;
  63. }
  64. public static String input(String message) {
  65. Scanner scanner = new Scanner(System.in);
  66. System.out.println(message);
  67. return (scanner.nextLine());
  68. }
  69. }
um6iljoc

um6iljoc1#

我注意到你在用 java.util.Scanner . 我写了一个脚本,它应该按照您的所有标准为您完成任务:

  1. import java.util.Scanner;
  2. class recursiveParsingCalculator {
  3. public static void main(String[] args) {
  4. Scanner scanner = new Scanner(System.in);
  5. // Ask user to input the expression
  6. System.out.println("Please input the expression");
  7. String userInput = scanner.nextLine();
  8. System.out.println(
  9. "And the final result is: " + recursiveCalculation(userInput, userInput.length() - 1, 0, 0, 0));
  10. scanner.close();
  11. System.exit(0);
  12. }
  13. // Identify the type of character at a specific position
  14. public static char charOfString(String userInput, int i) {
  15. return userInput.charAt(i);
  16. }
  17. /*
  18. * Position must be userInput.length() - 1 initially. currentResults, operand1
  19. * and operand2 are also meant to be initilized with 0.
  20. */
  21. public static int recursiveCalculation(String userInput, int position, int operand1, int operand2,
  22. int currentResults) {
  23. // If position is zero, just output the operand.
  24. if (position == 0) {
  25. if (Character.isDigit(charOfString(userInput, position))) {
  26. return charOfString(userInput, position) - '0';
  27. } else {
  28. System.out.println("Invalid input.");
  29. }
  30. }
  31. if (position > -1) {
  32. // Check if it is a number or an operator
  33. if (Character.isDigit(charOfString(userInput, position))) {
  34. operand1 = charOfString(userInput, position) - '0'; // First operand
  35. // Check if 2nd char is a number or an operator.
  36. if (Character.isDigit(charOfString(userInput, position - 1))) {
  37. operand2 = charOfString(userInput, position - 1) - '0';
  38. position = position - 1;
  39. }
  40. } else {
  41. // If it is an operator, then proceed to compute the results so far
  42. char operator = charOfString(userInput, position);
  43. // If it is a binary situation
  44. if (operator == '+' || operator == '*') {
  45. currentResults = binaryOperator(operator, operand1, operand2);
  46. operand2 = currentResults;
  47. }
  48. // If it is an unary situation
  49. else if (operator == '!') {
  50. if (currentResults == 0) {
  51. currentResults = operand1;
  52. }
  53. currentResults = unaryOperator(currentResults);
  54. operand2 = currentResults;
  55. } else {
  56. System.out.println("Invalid operator");
  57. return 0; // Return zero by default
  58. }
  59. }
  60. position = position - 1;
  61. }
  62. if (position > -1) {
  63. return recursiveCalculation(userInput, position, operand1, operand2, currentResults);
  64. } else {
  65. return currentResults;
  66. }
  67. }
  68. public static int binaryOperator(char operator, int operand1, int operand2) {
  69. switch (operator) {
  70. case '+':
  71. return operand1 + operand2;
  72. case '*':
  73. return operand1 * operand2;
  74. default:
  75. System.out.println("Invalid binary Operator");
  76. return 0; // Return zero by default
  77. }
  78. }
  79. // Calculate the factorial
  80. public static int unaryOperator(int operand) {
  81. if (operand <= 1)
  82. return 1;
  83. else
  84. return operand * unaryOperator(operand - 1);
  85. }
  86. }

用法示例:对于二进制运算符,输入+21,它将为您添加它们。一元输入!它将产生阶乘。现在,您可以使用一元和二元运算符尝试任何组合链和数字排列,它将递归地为您计算值。
例如,考虑输入*3+12:它将1和2相加,然后乘以3,最后计算出整个表达式的阶乘,从而得到预期的362880。

展开查看全部
7vux5j2d

7vux5j2d2#

为什么不将输入计算字符串赋给一个字符数组,然后遍历数组并匹配字符“+”、“-”、“*”?

相关问题