我的程序如下:
import java.util.*;
public class Operation
{
int n=25;
String exp=new String("");
//Stack s=new Stack();
public void conversion()
{
//System.out.print("输入表达式长度:");
//n=MyInput.readInt();
String infix;
System.out.print("输入一位数四则混合运算表达式:");
infix=MyInput.readString();
String exp=new String("");
Stack s=new Stack();
int i,j=0;
//String temp=new String("");
for(i=0;i<infix.length();i++)
{
// System.out.print(infix) ; if(infix.charAt(i) >= '0' && infix.charAt(i) <= '9') exp+=infix.charAt(i);
else
{
if(infix.charAt(i)=='(') s.push(infix.substring(i-1,i));
if(infix.charAt(i)==')')
{
while((String)s.peek()!="(")
{
exp+=(String)s.peek();
s.pop();
}
s.pop();
}
if(infix.charAt(i)=='*' || infix.charAt(i)=='/')
{
if(s.empty() || (String)s.peek()=="+" || (String)s.peek()=="-" || (String)s.peek()=="(")
s.push(infix.substring(i-1,i));
else
{
exp+=(String)s.peek();
s.pop();
s.push(infix.substring(i-1,i));
}
}
if(infix.charAt(i)=='+' || infix.charAt(i)=='-')
{ if(s.empty() || (String)s.peek()=="(") s.push(infix.substring(i-1,i));
else
{
exp+=(String)s.peek();
s.pop();
s.push(infix.substring(i-1,i));
}
}
}
}
while(!s.empty())
{
exp+=(String)s.peek();
s.pop();
}
exp="";
}
public void calculate()
{
Stack q=new Stack();
int a,b,i;
for(i=0;i<exp.length();i++)
{
if(exp.charAt(i) >= '0' && exp.charAt(i) <= '9')
//s.push(exp[i].charAt(0)-48);
q.push(exp.substring(i-1,i));
else
{
b=Integer.parseInt((String)q.peek());q.pop();
a=Integer.parseInt((String)q.peek());q.pop();
switch(exp.charAt(i))
{
case '+':{q.push(Integer.toString(a+b));break;}
case '-':{q.push(Integer.toString(a-b));break;}
case '*':{q.push(Integer.toString(a*b));break;}
case '/':{q.push(Integer.toString(a/b));break;}
}
}
}
System.out.print((String)q.peek());
} public static void main(String[] args)
{
Operation op=new Operation() ;
op.conversion();
op.calculate();
}
}// MyInput.java: Contain the methods for reading int, double, and
// string values from the keyboard
import java.io.*;
//输入函数
public class MyInput
{
// Read a string from the keyboard
public static String readString()
{
BufferedReader br
= new BufferedReader(new InputStreamReader(System.in), 1); // Declare and initialize the string
String string = ""; // Get the string from the keyboard
try
{
string = br.readLine();
}
catch (IOException ex)
{
System.out.println(ex);
} // Return the string obtained from the keyboard
return string;
} // Read an int value from the keyboard
public static int readInt()
{
return Integer.parseInt(readString());
} // Read a double value from the keyboard
public static double readDouble()
{
return Double.parseDouble(readString());
} // Read a byte value from the keyboard
public static byte readByte()
{
return Byte.parseByte(readString());
} // Read a short value from the keyboard
public static short readShort()
{
return Short.parseShort(readString());
} // Read a long value from the keyboard
public static long readLong()
{
return Long.parseLong(readString());
} // Read a float value from the keyboard
public static float readFloat()
{
return Float.parseFloat(readString());
}
}
程序编译已经没有错误了,运行起来就出错。
import java.util.*;
public class Operation
{
int n=25;
String exp=new String("");
//Stack s=new Stack();
public void conversion()
{
//System.out.print("输入表达式长度:");
//n=MyInput.readInt();
String infix;
System.out.print("输入一位数四则混合运算表达式:");
infix=MyInput.readString();
String exp=new String("");
Stack s=new Stack();
int i,j=0;
//String temp=new String("");
for(i=0;i<infix.length();i++)
{
// System.out.print(infix) ; if(infix.charAt(i) >= '0' && infix.charAt(i) <= '9') exp+=infix.charAt(i);
else
{
if(infix.charAt(i)=='(') s.push(infix.substring(i-1,i));
if(infix.charAt(i)==')')
{
while((String)s.peek()!="(")
{
exp+=(String)s.peek();
s.pop();
}
s.pop();
}
if(infix.charAt(i)=='*' || infix.charAt(i)=='/')
{
if(s.empty() || (String)s.peek()=="+" || (String)s.peek()=="-" || (String)s.peek()=="(")
s.push(infix.substring(i-1,i));
else
{
exp+=(String)s.peek();
s.pop();
s.push(infix.substring(i-1,i));
}
}
if(infix.charAt(i)=='+' || infix.charAt(i)=='-')
{ if(s.empty() || (String)s.peek()=="(") s.push(infix.substring(i-1,i));
else
{
exp+=(String)s.peek();
s.pop();
s.push(infix.substring(i-1,i));
}
}
}
}
while(!s.empty())
{
exp+=(String)s.peek();
s.pop();
}
exp="";
}
public void calculate()
{
Stack q=new Stack();
int a,b,i;
for(i=0;i<exp.length();i++)
{
if(exp.charAt(i) >= '0' && exp.charAt(i) <= '9')
//s.push(exp[i].charAt(0)-48);
q.push(exp.substring(i-1,i));
else
{
b=Integer.parseInt((String)q.peek());q.pop();
a=Integer.parseInt((String)q.peek());q.pop();
switch(exp.charAt(i))
{
case '+':{q.push(Integer.toString(a+b));break;}
case '-':{q.push(Integer.toString(a-b));break;}
case '*':{q.push(Integer.toString(a*b));break;}
case '/':{q.push(Integer.toString(a/b));break;}
}
}
}
System.out.print((String)q.peek());
} public static void main(String[] args)
{
Operation op=new Operation() ;
op.conversion();
op.calculate();
}
}// MyInput.java: Contain the methods for reading int, double, and
// string values from the keyboard
import java.io.*;
//输入函数
public class MyInput
{
// Read a string from the keyboard
public static String readString()
{
BufferedReader br
= new BufferedReader(new InputStreamReader(System.in), 1); // Declare and initialize the string
String string = ""; // Get the string from the keyboard
try
{
string = br.readLine();
}
catch (IOException ex)
{
System.out.println(ex);
} // Return the string obtained from the keyboard
return string;
} // Read an int value from the keyboard
public static int readInt()
{
return Integer.parseInt(readString());
} // Read a double value from the keyboard
public static double readDouble()
{
return Double.parseDouble(readString());
} // Read a byte value from the keyboard
public static byte readByte()
{
return Byte.parseByte(readString());
} // Read a short value from the keyboard
public static short readShort()
{
return Short.parseShort(readString());
} // Read a long value from the keyboard
public static long readLong()
{
return Long.parseLong(readString());
} // Read a float value from the keyboard
public static float readFloat()
{
return Float.parseFloat(readString());
}
}
程序编译已经没有错误了,运行起来就出错。
1.把所有(String)s.peek()=="?????"改成=====> "?????".equals((String)s.peek())
2.在做计算前,将Stack中的东西打印一下,看是不是你想像的.
1.String exp = new String("");
public void conversion() {
String exp=new String("");//method局部变量与class变量名字一样,到底想用哪一个
....
exp=""; //如果是calculate想用exp,为什么把它置空
}
public void calculate() {
for (i = 0; i < exp.length(); i++) {
}
}
2.public void calculate() {
//....
for (i = 0; i < exp.length(); i++) {
//....
q.push(exp.substring(i - 1, i));//i==0的时候substring为-1了
//....
}
}