gpt4 book ai didi

Java按空格分隔行读取并插入到树中

转载 作者:行者123 更新时间:2023-12-01 21:11:29 25 4
gpt4 key购买 nike

我正在尝试读取以空格分隔的用户输入并将值插入到树中。一切正常,直到我有两位数整数。当我希望它首先读取两个整数然后将两位数整数插入树中时,它似乎正在读取每个整数字符并插入树中

例如。当用户输入 9 3 + 7 * 时,效果很好,但是,当用户输入 10 3 + 7 * 时,它会先插入 10 作为 1,然后插入 0。我希望它插入 10。(这是等式)

这是我到目前为止所拥有的:

在我的主课

public static void main(String [] args){
Scanner scan = new Scanner(System.in);
ExpressionTree et = new ExpressionTree();
System.out.println("\nEnter equation: ");
et.buildTree(scan.nextLine());
}

在我的 buildTree 方法中..

public void buildTree(String eqn){
for(int i = 0; i <= eqn.legnth()-1; i++)
insert(eqn.charAt(i)); //how to insert 10 instead of 1 and then 0??
}

我知道我的 buildTree 方法有问题,但我不确定需要做什么来读取两位数字符..

完整代码:

package expressiontreetest;

import java.util.Scanner;


class ExpressionTree {
class TreeNode {
char data;
TreeNode left, right;

/** constructor **/
public TreeNode(char data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class StackNode {
TreeNode treeNode;
StackNode next;
public StackNode(TreeNode treeNode) {
this.treeNode = treeNode;
next = null;
}
}

private static StackNode top;
public ExpressionTree() {
top = null;
}
public void clear() {
top = null;
}
private void push(TreeNode ptr) {
if (top == null) top = new StackNode(ptr);
else {
StackNode nptr = new StackNode(ptr);
nptr.next = top;
top = nptr;
}
}
private TreeNode pop() {
if (top == null) throw new RuntimeException("Underflow");
else {
TreeNode ptr = top.treeNode;
top = top.next;
return ptr;
}
}
private TreeNode peek() {
return top.treeNode;
}
private void insert(char val) {
try {
if (isDigit(val)) {
TreeNode nptr = new TreeNode(val);
push(nptr);
}
else if (isOperator(val)) {
TreeNode nptr = new TreeNode(val);
nptr.left = pop();
nptr.right = pop();
push(nptr);
}
}
catch(Exception e) {
System.out.println("Invalid Expression");
}
}
//if it is a valid digit return true
private boolean isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
//if it is a valid operator return true
private boolean isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}

private int toDigit(char ch) {
return ch - '0';
}

//starting from index 0 increment the index after char has been inserted into the tree
//and terminate the for loop once we have reached equation legnth
public void buildTree(String eqn) {
// String [] eqnSubparts = eqn.split(" ");
// System.out.print("\neqnSubparts: " + eqnSubparts);

for (int i = 0; i <= eqn.length() - 1; i++) {
// System.out.print("\neqnSubparts: " + eqnSubparts[i]);
insert(eqn.charAt(i));
}
}

public double evaluate() {
return evaluate(peek());
}

public double evaluate(TreeNode ptr) {
if (ptr.left == null && ptr.right == null) return toDigit(ptr.data);
else {
double result = 0.0;
double left = evaluate(ptr.left);
double right = evaluate(ptr.right);
char operator = ptr.data;
//switch statment for oper
switch (operator) {
case '+':
result = left + right;
break;
case '-':
result = left - right;
break;
case '*':
result = left * right;
break;
case '/':
result = left / right;
break;
default:
result = left + right;
break;
}
return result;
}
}

}

/** class ExpressionTreeTest **/
public class ExpressionTreeTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System. in );
System.out.println("Expression Tree Test");

/** make object of ExpressionTree **/
ExpressionTree et = new ExpressionTree();

System.out.println("\nEnter equation in prefix form");



//String line = scan.nextLine();
//System.out.print("First digit : " + line);
et.buildTree(scan.nextLine());


System.out.print("\nInput : ");

System.out.println("\n\nEvaluated Result : " + et.evaluate());
}
}

最佳答案

用户输入的每组字符之间是否有空格?也就是说是不是像10空格3空格+空格7。如果不是的话,尝试以这种格式输入,然后就可以在空格上分割输入了

关于Java按空格分隔行读取并插入到树中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41073287/

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