我试图修复我的代码,这是一个使用堆栈和令牌类的Java中的简单计算器。每次我有一个十进制数作为答案我都有错误。例如,如果我输入11/2它将给我1.它是一个奇怪的错误,我不知道如何解决它,并想知道是否有人可以告诉我如何。谢谢你的帮助!
public class SimpleCalcSkeleton {
private enum TokenType {WS,LP,RP,NUM,OP};
private static class Token {
// instance variables
private String value;
private TokenType type;
// constructor
public Token(String v, TokenType t) {
this.value = v;
this.type = t;
}
// toString() is important for diagnostic output
public String toString() {
return ("[" + value + ";" + type.toString() + "]");
}
// getter or accessor methods for the instance vars or properties
// setter methods are not needed
public String getValue() {
return value;
}
public TokenType getType() {
return type;
}
}
private static TokenType getTokenType(char c) {
TokenType type=null;
if(c==' '){
type=TokenType.WS;
}
if(c=='('){
type=TokenType.LP;
}
if(c==')'){
type=TokenType.RP;
}
if((c=='+')||(c=='-')||(c=='*')||(c=='/')){
type=TokenType.OP;
}
if((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9')){
type=TokenType.NUM;
}
return type;
}
private static int getAssoc(Token token) {
String word = token.getValue();
int number =0;
if((word.equals('+'))||(word.equals('-'))){
number=1;
}
if((word.equals('*'))||(word.equals('/'))){
number=2;
}
return number;
}
private static ArrayList<Token> parse(String s) {
if(s==null||s.length()==0){
return null;
}
ArrayList<Token> list= new ArrayList<Token>();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
TokenType type = getTokenType(c);
String symbol= Character.toString(c);
if(type==null){
System.out.print("Error: null");
}
else if(!type.equals(TokenType.WS)){
list.add(new Token(symbol,type));
}
}
return list;
}
private static ArrayList<Token> infixToPostfix(ArrayList<Token> intokens) {
Stack astack = new Stack();
ArrayList<Token>outokens=new ArrayList<Token>();
for(int i=0;i<intokens.size();i++){
Token in= intokens.get(i);
if(in.getType()==TokenType.NUM){
outokens.add(in);
}
if(in.getType()==TokenType.LP){
astack.push(in);
}
if(in.getType()==TokenType.RP){
Token top=(Token)astack.peek();
while(top.getType()!=TokenType.LP){
astack.pop();
outokens.add(top);
top=(Token)astack.peek();
if(top.getType()==TokenType.LP){
astack.pop();
break;
}}}
if(in.getType()==TokenType.OP){
if(!astack.isEmpty()){
Token top = (Token)astack.peek();
while((top.getType()==TokenType.OP)&&(getAssoc(top)>=getAssoc(in))){
astack.pop();
outokens.add(top);
if(!astack.isEmpty())
top=(Token)astack.peek();
else break;
}}
astack.push(in);
}
}
while(!astack.isEmpty()){
Token top=(Token)astack.peek();
astack.pop();
outokens.add(top);
if(!astack.isEmpty())
top=(Token)astack.peek();
else break;
}
return outokens;
}
private static Token evalOp(Token op, Token t1, Token t2) {
String one = t1.getValue();
String two = t2.getValue();
String opener = op.getValue();
int output =0;
int first = Integer.parseInt(one);
int second = Integer.parseInt(two);
if(opener.equals("+")){
output = second+first;
}
if(opener.equals("-")){
output = second-first;
}
if(opener.equals("*")){
output = second*first;
}
if(opener.equals("/")){
output = second/first;
}
String last = Integer.toString(output);
Token total = new Token(last,TokenType.NUM);
return total;
}
private static String evalPostfix(ArrayList<Token> intokens) {
Stack right = new Stack();
for(int i=0;i<intokens.size();i++){
Token in=intokens.get(i);
if(in.getType()==TokenType.NUM)
right.push(in);
else if(in.getType()==TokenType.OP){
Token at = (Token) right.pop();
Token bat = (Token) right.pop();
Token cat = evalOp(in,at,bat);
right.push(cat);
}
}
return right.toString();
}
public static String evalExpression(String s) {
ArrayList<Token> infixtokens = parse(s);
System.out.println("\tinfix tokens: " + infixtokens);
ArrayList<Token> postfixtokens = infixToPostfix(infixtokens);
System.out.println("\tpostfix tokens: " + postfixtokens);
return evalPostfix(postfixtokens);
}
public static void commandLine() {
Scanner in = new Scanner(System.in);
while (true){
System.out.print("> ");
String word = in.nextLine();
if (word.equals("quit")) {
break;
}
else {
System.out.println(evalExpression(word));
}
}
}
}
class SimpleCalcTest {
public static void test() {
String[] inputs = {
"3*4 + 5",
"3 + 4*5",
"(1+1)*(5-2)",
"(3*4+5)*(3*3 + 4*4)"
};
for (int i=0; i<inputs.length; i++) {
String s = inputs[i];
System.out.println();
System.out.println();
System.out.println("test: input = " + s);
String r = SimpleCalcSkeleton.evalExpression(s);
System.out.println("\tresult = " + r);
}
}
public static void main(String[] args) {
SimpleCalcSkeleton.commandLine();
SimpleCalcTest.test();
}
}