编辑-我修改了代码,并用新代码替换了原来的工作,但仍然遇到类似的问题
我要学习的数据结构课程是我的第一门编程课程,所以我有点不了解。第一个项目真的让我大吃一惊。它是一个反向波兰符号计算器。它或多或少是完整的,只是很多错误。我已经花了几个小时来调整我的代码,但是当我解决一个问题时,就会释放出另一个问题。我要为自己可怕的编程技能向我道歉。
公共类ReversePolishStack {
class SinglyLinkList {
Node head = null;
public void push(float newData) {
Node cache = this.head;
this.head = new Node(newData, cache);
}
public float pop() {
float out = this.head.data;
head = head.next;
return out;
}
public void add(float num1, float num2) {
num1 = pop();
num2 = pop();
push(num1+num2);
}
public void sub(float num1, float num2) {
num1 = pop();
num2 = pop();
push(num2-num1);
}
public void div(float num1, float num2) {
num1 = pop();
num2 = pop();
push(num2/num1);
}
public void mult(float num1, float num2) {
num1 = pop();
num2 = pop();
push(num1*num2);
}
class Node {
public float data;
public Node next;
public Node(float data, Node next) {
this.data = data;
this.next = next;
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ReversePolishStack rps = new ReversePolishStack();
SinglyLinkList sll = rps.new SinglyLinkList();
String entry;
do
{
System.out.print("Enter Expression:\n");
Scanner in = new Scanner(System.in);
entry =in.nextLine();
StringTokenizer st = new StringTokenizer(entry," ");
String s1;
int count = 0;
while (st.hasMoreElements())
{
if (entry.length()<4) {// for an error message not enough input
System.out.print("Not enough input"); break;
}
else if (count>1 && sll.head.next==null) {
System.out.print("Not enough operands"); break;
}
s1 = st.nextToken();
if((s1.equals("+") || s1.equals("-") || s1.equals("*") || s1.equals("/"))) {
if(s1.equals("+"))
sll.add(sll.head.data, sll.head.next.data);
else if(s1.equals("-"))
sll.sub(sll.head.data, sll.head.next.data);
else if(s1.equals("/")) {
if (sll.head.data==0) {
System.out.println("Division by Zero enounterd."); break;
}
sll.div(sll.head.data, sll.head.next.data);
}
else if(s1.equals("*"))
sll.mult(sll.head.data, sll.head.next.data);
else
System.out.print("Unrecognized input");break;
}
else {
sll.push(Float.parseFloat(s1));
}
count++;
}
System.out.println(sll.head.data);
sll.pop();
} while(entry.equals("0")== false); // typeing a single zero terminates
System.out.print("Thanks for using my RPN Calculator!");
}
}
我已经有一段时间了,我敢肯定,每次修复我也添加到卷积中的错误的尝试都是我的代码。任何帮助,将不胜感激!
最佳答案
首先,您应该从main方法中取出已定义的类。
之后,您将得到一个错误,因为已经创建了类singlyLinkList
(singlyLinkList sll = new singlyLinkList();
)的实例,而没有创建外部类(ReversePolishStack
)。
参见参考文献this link about nested classes。
我还将Node
类放入singlyLinkList
类中(顺便说一句,您应将此类重命名为SinglyLinkList
,并且首字母大写)。
我没有涉及您代码的逻辑,但是至少在这些修复之后,您的代码将进行编译:
public class ReversePolishStack {
class singlyLinkList {
Node head = null;
public void push(float newData) {
Node cache = this.head;
this.head = new Node(newData, cache);
}
public float pop() {
float out = this.head.data;
head = head.next;
return out;
}
class Node {
public float data;
public Node next;
public Node(float data, Node next) {
this.data = data;
this.next = next;
}
}
}
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
float number1;
float number2;
ReversePolishStack rps = new ReversePolishStack();
singlyLinkList sll = rps.new singlyLinkList();
System.out.print("Enter Expression:\n");
// from here will be the same as you wrote
}
}
我还建议您从主要方法中提取程序的逻辑,并在
ReversePolishStack
中创建方法,以便使用oop概念。希望这是有用的!
再见!