Calculator in java


H

HelpMe

I have made a postfix calculator and I want to make it infix using the
shunting-yard algorithm and no other way.Can anyone please help?Help
me to make modification only in the main function.

My Program is:-

import java.io.*;
import java.util.StringTokenizer;

abstract class Expr{
abstract public double value();
abstract public String toString();
}

class Min2Expr extends Expr {
private Expr left;
private Expr right;
public static final String symbol = "m";

Min2Expr(Expr l,Expr r) {
left = l;
right = r;
}

public double value() {
if((left.value() - right.value()) > 0)
return right.value();
else
return left.value();
}

public String toString() {
return "(" + left.toString() + ")" + symbol + "(" + right.toString() +
")";
}
}
class Max2Expr extends Expr {
private Expr left;
private Expr right;
public static final String symbol = "M";

Max2Expr(Expr l,Expr r) {
left = l;
right = r;
}

public double value() {
if((left.value() - right.value()) < 0)
return right.value();
else
return left.value();
}

public String toString() {
return "(" + left.toString() + ")" + symbol + "(" + right.toString() +
")";
}
}
class UnaryMinusExpr extends Expr {
private Expr left;
private Expr right;
public static final String symbol = "u";

UnaryMinusExpr(Expr null,Expr r) {
left = null;
right = r;
}

public double value() {
return (-1)*right.value();
}

public String toString() {
return symbol +" "+ right.toString() ;
}
}

class MinusExpr extends Expr {
private Expr left;
private Expr right;
public static final String symbol = "-";

MinusExpr(Expr l,Expr r) {
left = l;
right = r;
}

public double value() {
return left.value() - right.value();
}

public String toString() {
return "(" + left.toString() + ")" + symbol + "(" + right.toString() +
")";
}
}
class PlusExpr extends Expr {
private Expr left;
private Expr right;
public static final String symbol = "+";

PlusExpr(Expr l,Expr r) {
left = l;
right = r;
}

public double value() {
return left.value() + right.value();
}

public String toString() {
return "(" + left.toString() + ")" + symbol + "(" + right.toString() +
")";
}
}

class MultExpr extends Expr {
private Expr left;
private Expr right;
public static final String symbol = "*";

MultExpr(Expr l,Expr r) {
left = l;
right = r;
}

public double value() {
return left.value() * right.value();
}

public String toString() {
return "(" + left.toString() + ")" + symbol + "(" + right.toString() +
")";
}
}

class DivExpr extends Expr {
private Expr left;
private Expr right;
public static final String symbol = "/";

DivExpr(Expr l,Expr r) {
left = l;
right = r;
}

public double value() {
double dl= right.value();
if(dl == 0) throw new ArithmeticException("division by 0 in" +
toString());
return left.value() / right.value();
}

public String toString() {
return "(" + left.toString() + ")" + symbol + "(" + right.toString() +
")";
}
}
class ConstExpr extends Expr {
private double val;

ConstExpr() {
val = 0;
}
ConstExpr(double d) {
val = d;
}
public double value() {
return val;
}
public String toString() {
return Double.toString(val);
}
}

class ParseException extends Exception {
ParseException(String s) {
super(s);
}
}


public class Calculator {
private Expr history[];
private static final String[] symbols = { PlusExpr.symbol,
MinusExpr.symbol, DivExpr.symbol,
MultExpr.symbol,Min2Expr.symbol,Max2Expr.symbol,UnaryMinusExpr.symbol };

Calculator() {
history = new Expr[20];
}
Calculator(int h) {
history = new Expr[h];
}

public static Expr parseExpr(String s) throws ParseException {
StringTokenizer st = new StringTokenizer(s);
Expr [] exprList = new Expr[s.length()];
int count = 0;
while(st.hasMoreTokens()) {
String t = st.nextToken();
int i=0;
while(i < symbols.length) {
if(symbols.equals(t)) break;
i++;
}
if(i >= symbols.length) {
try{
exprList[count++] = new ConstExpr(Double.parseDouble(t));
}
catch(NumberFormatException e) {
throw new ParseException("parse error at: " + t);
}
continue;
}

Expr r = exprList[--count];
Expr l = exprList[--count];
switch(i) {
case 0 : exprList[count++] = new PlusExpr(l,r);
break;

case 1 : exprList[count++] = new MinusExpr(l,r);
break;

case 2 : exprList[count++] = new DivExpr(l,r);
break;

case 3 : exprList[count++] = new MultExpr(l,r);
break;

case 4 : exprList[count++] = new Min2Expr(l,r);
break;

case 5 : exprList[count++] = new Max2Expr(l,r);
break;

case 6 : exprList[count++] = new UnaryMinusExpr(null,r);
break;


default : //unreachable
}
}

return exprList[0];
}

public static void main(String[] args) {
Calculator c = new Calculator();

if(args.length > 0) {
for(int i =0; i < args.length;i++) {
try {
Expr e = Calculator.parseExpr(args);
System.out.println(e + "=" + e.value());
}
catch(ParseException e) {
System.out.println(e);
}
}
return;
}

BufferedReader bis = new BufferedReader(new
InputStreamReader(System.in));
String s = "";
while(! s.equals(".")) {
System.out.print("Postfix expr?(to end type .):");
try{
s=bis.readLine();
Expr e = parseExpr(s);
System.out.println(e + "=" + e.value());
}
catch(IOException e) {
System.out.println("Error while reading: " + e);
}
catch(ParseException e) {
System.out.println(e);
}
}
}
}
 
Ad

Advertisements

E

EJP

HelpMe said:
I have made a postfix calculator and I want to make it infix using the
shunting-yard algorithm and no other way.Can anyone please help?Help
me to make modification only in the main function.
Can't be done. The modifications required are required elsewhere.
 
H

HelpMe

Can't be done. The modifications required are required elsewhere.

See,I have already implemented stack and queue.I want to make their
use here.Please help.
 
E

EJP

HelpMe said:
See,I have already implemented stack and queue.I want to make their
use here.Please help.

Unless I misunderstood, you specified that the modification can only be
made in the main() function. That makes it impossible.
 
H

HelpMe

Unless I misunderstood, you specified that the modification can only be
made in the main() function. That makes it impossible.

O.K.Can there be any other possibility.I mean without modifying the
main function.Please help soon.
 
Ad

Advertisements

Ad

Advertisements


Top