gpt4 book ai didi

java - 计算具有运算符优先级的过程

转载 作者:行者123 更新时间:2023-12-01 15:47:56 25 4
gpt4 key购买 nike

我正在尝试计算一个算术表达式,该表达式以字符串形式输入(例如,( 5+4*5-1/8 ),结果为 3)。我输入一个表达式并将其转换为数组。第一的;结果将从第一个元素开始,并且会在循环中发生变化。但问题是运算符优先级。如何在循环中使用运算符存在?这是我的代码:

import java.util.Scanner;


public class HesapMakinesi {

private char value[];

private int count;

private Scanner str = new Scanner(System.in);

private String process;

HesapMakinesi() {

System.out.print("Enter the process ");

process = str.next();

//System.out.println(islem);

Initializer(process);

}

private void Initializer(String process) {

count = process.toCharArray().length;

value = new char [count];

int i;

System.arraycopy(process.toCharArray(), 0, value, 0, count);

//System.out.println(value);

if(value[0]=='-' || value[0]=='+' || value[0]=='/' || value[0]=='*' || // A process cannot start with an operator
value[count-1]=='-' || value[count-1]=='+' || value[count-1]=='/' || value[count-1]=='*') {

System.out.println("You have entered a wrong process.Please enter again!!!");

System.out.print("Enter the process: ");

process = str.next();

Initializer(process);

}



for(i=0; i<count; i++) { // A process cannot include a character except operators

if( value[i]!='+' && value[i]!='-' && value[i]!='*' && value[i]!='/' && value[i]!='(' && value[i]!=')' && !Character.isDigit(value[i]) ) {

System.out.println("You have entered a wrong process.Please enter again!!!");

System.out.print("Enter the process: ");

process = str.next();

Initializer(process);

}


}


for(i=0; i<count-1; i++) { // A process cannot have operators sequantially

if( !Character.isDigit(value[i]) && !Character.isDigit(value[i+1]) ) {

if( (value[i] == '+' && value[i+1] == '+' ) || (value[i] == '+' && value[i+1] == '-' ) || (value[i] == '+' && value[i+1] == '*' ) ||
(value[i] == '+' && value[i+1] == '/' ) ) {

System.out.println("You have entered a wrong process.Please enter again!!!");

System.out.print("Enter the process: ");

process = str.next();

Initializer(process);

}

else if( (value[i] == '-' && value[i+1] == '+' ) || (value[i] == '-' && value[i+1] == '-' ) || (value[i] == '-' && value[i+1] == '*' ) ||
(value[i] == '-' && value[i+1] == '/' ) ) {

System.out.println("You have entered a wrong process.Please enter again!!!");

System.out.print("Enter the process: ");

process = str.next();

Initializer(process);

}

else if( (value[i] == '*' && value[i+1] == '+' ) || (value[i] == '*' && value[i+1] == '-' ) || (value[i] == '*' && value[i+1] == '*' ) ||
(value[i] == '*' && value[i+1] == '/' ) ) {

System.out.println("You have entered a wrong process.Please enter again!!!");

System.out.print("Enter the process: ");

process = str.next();

Initializer(process);

}

else if( (value[i] == '/' && value[i+1] == '+' ) || (value[i] == '/' && value[i+1] == '-' ) || (value[i] == '/' && value[i+1] == '*' ) ||
(value[i] == '/' && value[i+1] == '/' ) ) {

System.out.println("You have entered a wrong process.Please enter again!!!");

System.out.print("Enter the process: ");

process = str.next();

Initializer(process);

}


}

}

//sCount();

}

/*private void Count(){

double result,temp;

int i;

for(i=0; i<count; i++) {

if( value[i]!= )

}


}*/

}

最佳答案

你不是这样做的。您需要在评估表达式之前对其进行解析。我建议您阅读Shunting-yard algorithm .

关于java - 计算具有运算符优先级的过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6760847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com