Alphard 0 Newbie Poster

Hi guys!!! I'm a nineteen-year-old student from the Philippines and I'm really new at programming Java. I'm having difficulties how to solve a problem that our professor made.

Her restrictions are the following:

Assume the following rules of precedence for expressions:
Highest is addition (+)
then subtraction (-)
then multiplication (*)
then division (/)


Evaluate inputs such as:
(a) 1+4*3-2/2 Output: 5*3-2/2 = 5*1/2 = 5/2 = 2.5
(b) 5-2/2+1*2 Output: 5-2/3*2 = 3/3*2 = 3/6 = 0.5
(c) 3-2+4-2+5 Output: 3-6-7 = -3 - 7 = -10


Please show solution.


Output up to 5 decimal places if long.

So what I did, I coded a syntax that would do the typical function of a simple calculator. But as written at the restrictions above, that MDAS(operator precedence) wouldn't be followed. Instead of MDAS, ASMD, would be the precedence.

Here is the code that I made:

import java.io.*;
public class Calculator {
public static final int PLUSOP = '+';
public static final int MINUSOP = '-';
public static final int MULTOP = '*';
public static final int DIVOP = '/';
public static final int LPAREN = '(';
public static final int RPAREN = ')';
public static final int NUMTOK = StreamTokenizer.TT_NUMBER;
public static final int EOLTOK = StreamTokenizer.TT_EOL;
public static final int ERRTOK = -99;


public static double Value;     // value of the arithmetic expression
public static int CurTok;
public static StreamTokenizer ST =
new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));


public static void main(String[] args) throws IOException {
ST.ordinaryChar('/');
Value = 0;
System.out.println("Expression calculator");
System.out.println("Enter an expression: ");
CurTok = NextToken();
while (CurTok != StreamTokenizer.TT_EOF) {
Value=expr();
System.out.println("=" + Value);
Value = 0;
System.out.print("Answer");
CurTok = NextToken();
}
}
public static int NextToken() throws IOException {
return ST.nextToken();
}
public static String tok2str(int tok) {
switch(tok) {
case PLUSOP: return "\"+\"";
case MINUSOP: return "\"-\"";
case NUMTOK: return "number";
}
return "Unknown token: " + tok;
}
public static void match(int tok) throws IOException {
if (CurTok == tok)
CurTok = NextToken();
else {
System.out.println("Error: " + tok2str(tok) + " expected");
System.exit(1);
}
}
public static double expr() throws IOException {
Value += term();
while(CurTok != EOLTOK){
switch(CurTok) {
case PLUSOP:
match(PLUSOP);
Value += term();
break;
case MINUSOP:
match(MINUSOP);
Value -= term();
break;
default:
return Value;
}
}
return Value;
}
public static double factor() throws IOException {
if (CurTok == NUMTOK) {
double save;
save = ST.nval;
match(NUMTOK);
return save;
}
else
if (CurTok == MINUSOP)
return expr();
else
if (CurTok == LPAREN) {
double save;
match(LPAREN);
save = expr();
match(RPAREN);
System.out.print(save);
return save;
}
double save;
save = ST.nval;
match(NUMTOK);
return save;
}
public static double term() throws IOException {
double prod = 1;
prod *= factor();
while (CurTok != PLUSOP || CurTok != MINUSOP) {
switch(CurTok) {
case MULTOP:
match(MULTOP);
prod *= factor();
break;
case DIVOP:
match(DIVOP);
prod /= factor();
break;
default:
return prod;
}
}
return prod;
}
}import java.io.*;
public class Calculator {
public static final int PLUSOP = '+';
public static final int MINUSOP = '-';
public static final int MULTOP = '*';
public static final int DIVOP = '/';
public static final int LPAREN = '(';
public static final int RPAREN = ')';
public static final int NUMTOK = StreamTokenizer.TT_NUMBER;
public static final int EOLTOK = StreamTokenizer.TT_EOL;
public static final int ERRTOK = -99;


public static double Value;     // value of the arithmetic expression
public static int CurTok;
public static StreamTokenizer ST =
new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));


public static void main(String[] args) throws IOException {
ST.ordinaryChar('/');
Value = 0;
System.out.println("Expression calculator");
System.out.println("Enter an expression: ");
CurTok = NextToken();
while (CurTok != StreamTokenizer.TT_EOF) {
Value=expr();
System.out.println("=" + Value);
Value = 0;
System.out.print("Answer");
CurTok = NextToken();
}
}
public static int NextToken() throws IOException {
return ST.nextToken();
}
public static String tok2str(int tok) {
switch(tok) {
case PLUSOP: return "\"+\"";
case MINUSOP: return "\"-\"";
case NUMTOK: return "number";
}
return "Unknown token: " + tok;
}
public static void match(int tok) throws IOException {
if (CurTok == tok)
CurTok = NextToken();
else {
System.out.println("Error: " + tok2str(tok) + " expected");
System.exit(1);
}
}
public static double expr() throws IOException {
Value += term();
while(CurTok != EOLTOK){
switch(CurTok) {
case PLUSOP:
match(PLUSOP);
Value += term();
break;
case MINUSOP:
match(MINUSOP);
Value -= term();
break;
default:
return Value;
}
}
return Value;
}
public static double factor() throws IOException {
if (CurTok == NUMTOK) {
double save;
save = ST.nval;
match(NUMTOK);
return save;
}
else
if (CurTok == MINUSOP)
return expr();
else
if (CurTok == LPAREN) {
double save;
match(LPAREN);
save = expr();
match(RPAREN);
System.out.print(save);
return save;
}
double save;
save = ST.nval;
match(NUMTOK);
return save;
}
public static double term() throws IOException {
double prod = 1;
prod *= factor();
while (CurTok != PLUSOP || CurTok != MINUSOP) {
switch(CurTok) {
case MULTOP:
match(MULTOP);
prod *= factor();
break;
case DIVOP:
match(DIVOP);
prod /= factor();
break;
default:
return prod;
}
}
return prod;
}
}

P.S Thank you if you would be able to help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.