Help with modifying a program for evaluating arithmetic expressions
This is an assignment for a data structures class that I need a little help getting started with. We are given all of the code that I provided below and we are asked to modify it so that the program for evaluating arithmetic expressions will also allow the unary operation '-'. For example, for the expression "-1" the result is -1; for the expression "-(1+3)" the result is -4; etc.
I am not asking for the answer, I just need some direction in how to get started. Any help is much appreciated. Thank you.
Code java:
package cs272;
import java.util.Scanner;
import java.util.regex.Pattern;
import edu.colorado.collections.LinkedStack;
public class Evaluation {
/**
* @param args
*/
private static boolean TRACE = false;
public static final Pattern CHARACTER =
Pattern.compile("\\S.*?");
public static final Pattern UNSIGNED_DOUBLE =
Pattern.compile("((\\d+\\.?\\d*)|(\\.\\d+))([Ee][-+]?\\d+)?.*?");
public static void printStack(LinkedStack<String> st) {
LinkedStack<String> pr = st;
while (!pr.isEmpty()) {
System.out.print(pr.pop() +" ");
}
}
public static LinkedStack<String> reverseStack(LinkedStack<String> st) {
LinkedStack<String> rev = new LinkedStack<String>();
while (!st.isEmpty()) {
rev.push(st.pop());
}
return rev;
}
public static double calculate(LinkedStack<Double> operands, Character operation)
{
double op1 = operands.pop();
double op2 = operands.pop();
double result = 0;
if (TRACE) System.out.println("op1 "+op1 +" op2 "+op2 +" " + operation);
switch(operation)
{
case '+':
result = op2 + op1;
break;
case '-':
result = op2 - op1;
break;
case '*':
result = op2 * op1;
break;
case '/':
result = op2 / op1;
break;
}
return result;
}
public static double evaluation(LinkedStack<String> st)
{
LinkedStack<Double> operands = new LinkedStack<Double>();
String next;
while (!st.isEmpty())
{
next = st.pop();
if (TRACE) System.out.println("Processing "+next);
switch (next.charAt(0))
{
case '+':
case '-':
case '*':
case '/':
operands.push(calculate(operands, next.charAt(0)));
break;
default:
operands.push(Double.valueOf(next));
break;
}
}
return operands.pop();
}
public static LinkedStack<String> infix2postfix(String s) {
LinkedStack<String> results = new LinkedStack<String>();
LinkedStack<Character> operations = new LinkedStack<Character>();
Scanner input = new Scanner(s);
String next;
Character first;
while (input.hasNext())
{
if (input.hasNext(UNSIGNED_DOUBLE))
{
next = input.findInLine(UNSIGNED_DOUBLE);
results.push(next);
if (TRACE) System.out.println("Processing "+next);
}
else
{
next = input.findInLine(CHARACTER);
if (TRACE) System.out.println("Processing "+next+" size of operations stack "+operations.size());
first = next.charAt(0);
switch (first)
{
case '+': // Addition
case '-': // Substraction
// System.out.println(" ... size of operations stack before do-while "+operations.size());
while (!operations.isEmpty() && !operations.peek().equals('('))
{
Character op = operations.pop();
String sop = Character.toString(op);
results.push((String) sop);
}
// System.out.println(" ... size of operations stack after do-while "+operations.size());
operations.push(first);
break;
case '*': // Multiplication
case '/': // Division
while (!operations.isEmpty() &&
!(operations.peek().equals('(') ||
operations.peek().equals('+') ||
operations.peek().equals('-')
)
)
{
Character op = operations.pop();
String sop = Character.toString(op);
results.push(sop);
}
operations.push(first);
break;
case ')': // Right parenthesis
while (!operations.isEmpty() && !operations.peek().equals('('))
{
Character op = operations.pop();
String sop = Character.toString(op);
results.push(sop);
}
if (operations.isEmpty())
throw new IllegalArgumentException("Invalid Expression");
operations.pop();
break;
case '(': // Left parenthesis
operations.push(first);
break;
default : // Illegal character
throw new IllegalArgumentException("Illegal character");
}
}
// System.out.println(next);
}
while (!operations.isEmpty())
{
Character op = operations.pop();
String sop = Character.toString(op);
results.push(sop);
}
return results;
}
// evaluate a fully parenthesized arithmetic expression
public static void main(String[] args) {
// TODO Auto-generated method stub
String exp = args[0];
LinkedStack<String> rev;
System.out.println("Expression: "+exp);
LinkedStack<String> postfix = infix2postfix(exp);
rev = reverseStack(postfix);
printStack(rev.clone());
System.out.println("\nEvaluation: "+evaluation(rev));
}
}
Re: Help with modifying a program for evaluating arithmetic expressions
That's a lot of code. We have a ton of posts to answer here every day, so we don't have time to wade through that many lines of code without a more specific answer. I recommend you read through this: http://www.javaprogrammingforums.com...e-posting.html
Re: Help with modifying a program for evaluating arithmetic expressions
Well that's really not that much code, but that's fine if you can't help. I'll just leave it up in case somebody else can help and if not I'll try somewhere else. Thanks anyway.
Re: Help with modifying a program for evaluating arithmetic expressions
Quote:
Originally Posted by
rockout341
Well that's really not that much code, but that's fine if you can't help. I'll just leave it up in case somebody else can help and if not I'll try somewhere else. Thanks anyway.
Sure, it's not that much code for any particular project, but it is quite a bit of code for strangers to look at for free, in their spare time, at the cost of helping others in the time it takes to wade through it. We're here to help, but we could help 5 other people in the time it takes to look through your code. Is your time really that much more valuable than theirs, or ours? 200 lines is about 10 times longer than a good example should be, and you're going to hear this no matter where you go.
Re: Help with modifying a program for evaluating arithmetic expressions
Thanks for taking the time to give me that warning. I'll keep that in mind when I need help with anything with more then a few lines of code to use different resources then this forum, but I found help else where so thanks anyway.
Re: Help with modifying a program for evaluating arithmetic expressions
This forum is a fine resource for people willing to show some effort. You're going to get the same response at every technical forum, probably in a much ruder way, for exactly the reasons I outlined above. Good luck though.
More recommended reading: http://www.javaprogrammingforums.com...-get-help.html