Results 41 to 50 of 50
- 02-16-2012, 09:28 AM #41
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: Need help on a calculator project
Right.. Let's go through, step by step how you would add a value to any variable the user types in.
1: Analyse the string for some combination of "^[a-zA-Z]=[0-9]+$". If you aren't familiar with regex, it makes life a lot easier for what you're doing.
So basically all i've said is scan a string for a letter, followed by an equals sign, followed by a number. So in your string, you find a letter. All you do is look to the right of the letter. If it's another letter, it's still part of the variable title. If it's an equals sign, you know it's an assignment operation. So there is only going to be one equals sign, so you keep looking to the right. Then you find some number. Keep looking too the right until it ceases to be a numeric value.
2: extract the variable title and value.
So now you've found where they are, you store them in some temporary variables. Next, and this bit is up to you, you can either do as Josah suggested and literally create the variables, or do as others have suggested and implement a hashmap. That is as far as im going though. The program is your own, and you should make decisions about how it runs. I hope this gets you on the right track :)
- 02-16-2012, 09:44 AM #42
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help on a calculator project
Cool, that way you can't do e.g. xyz = 1+2 for several reasons; that solution is severly crippled.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-16-2012, 05:56 PM #43
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
Re: Need help on a calculator project
true, but if we, for example, capped variable names at one letter, then you could construct an array on the fly to store each variable name. then check what's going on on the other side of the equals sign, and if there's another operator, call a piece of code that will extract the addition operation from a string, and pass it the substring from the "=" onwards. Your way works nicely as well, and I think both of them are good solutions. I just posed one idea to get him going because I like how this guy is sticking to the same thread, not asking for us to just give him the answer and his explanations are really quite lucid :)
- 02-22-2012, 09:35 PM #44
Member
- Join Date
- Feb 2012
- Posts
- 26
- Rep Power
- 0
Re: Need help on a calculator project
Alright, I successfully implemented exponents (and exponents of exponents), for example, 2^2^2 gives the correct answer (16), and I think I am going to hold off on the assignment operator for now, my top priority now is figuring out how to implement double negatives. I want the program to be able to take, for example, --5-3+-3 and give -1 as an answer.
Here's what I have:
Calculator.java
ExpressionDialog.javaJava Code:import java.util.*; public class Calculator { String expstring; int cursor; public Calculator() { expstring = null; } public Calculator(String instring) { expstring = instring; cursor = 0; } public void setExpression(String estring) { expstring = estring; cursor = 0; } public float assignmentOp(HashMap hashMap) // { cursor = 0; float result = evalExp(); while (nextOperator('=')) { char operator = getOperator(); float term = evalExp(); if (operator == '=') //used to append the term as a value to the appropriate key. { hashMap.put(String.valueOf(result), term); } else System.err.println("Please enter a valid string to be used as a variable"); } return result; } public float evalExp() { if (expstring == null) { System.err.println("There is no expression to evaluate."); System.err.println("Call setExpression() before calling getResult()."); return 0; } float result = evalTerm(); while (nextOperator('+')) { char operator = getOperator(); float term = evalTerm(); if (operator == '+') result = result + term; else System.err.println("Invalid operator: " + operator); } while (nextOperator('-')) { char operator = getOperator(); float term = evalTerm(); if (operator == '-') result = result - term; else System.err.println("Invalid operator: " + operator); } return result; } private float evalTerm() { float result = calcExponent(); while (nextOperator('*')) { char operator = getOperator(); float term = calcExponent(); if (operator == '*') result = result * term; else break; } while (nextOperator('/')) { char operator = getOperator(); float term = calcExponent(); if (operator == '/') result = result / term; else break; } return result; } private float calcExponent() { float result = getOperand(); while (nextOperator('^')) { char operator = getOperator(); float term = getOperand(); if (operator == '^') { float result2 = 1; for (short i = 0; i < term; i++) result2 = result2 * result; result = result2; } else break; } return result; } private float getOperand() { if (cursor >= expstring.length()) return 0; String opstring = ""; char nextchar = expstring.charAt(cursor); if (nextchar == '-') { opstring = opstring + '-'; cursor++; nextchar = expstring.charAt(cursor); } while (cursor < expstring.length() && !isOperator(nextchar)) { opstring = opstring + nextchar; cursor++; if (cursor < expstring.length()) nextchar = expstring.charAt(cursor); } return Float.parseFloat(opstring); } private char getOperator() { char operator = expstring.charAt(cursor); cursor++; return operator; } private boolean nextOperator(char op) { if (cursor < expstring.length() && expstring.charAt(cursor) == op) return true; else return false; } private boolean isOperator(char c) { String opset = "+*-/=^"; return (opset.indexOf(c) >= 0); } }
I'm pretty sure all I would have to do is, in getOperand, keep scanning the line to see if the next character after the - is another -, and if it is, cancel out both - signs. I'm just not sure how to code it.Java Code:import java.util.*; import java.io.*; class ExpressionDialog { Scanner keyreader; Calculator calc; PrintStream logfile; HashMap hashMap = new HashMap(); //creates a new hashmap that will store the variable and the number as key/value pairs public ExpressionDialog() { keyreader = new Scanner(System.in); calc = new Calculator(); } public void run() { String expression; float result = 0; showMenu(); expression = getNextExpression(); while(!expression.equals("quit")) { if (expression.equals("log off") && (logfile != null)) { logfile.close(); } else if (expression.equals("log")) { //Not very clean, prompt should be on the same line as the user input //Also adds a space and => 0.0 at the start of the file, this isn't clean and ideally should be changed System.out.println("Enter in a filename"); setLogFile(keyreader.next()); } //prints the value of a key if it is entered into the prompt. /*else if (expression.equals("")) { String next = keyreader.next(); System.out.println("=> " + hashMap.get(next)); }*/ else { calc.setExpression(expression); try { result = calc.assignmentOp(hashMap); //passes the hashmap to assignmentOp System.out.println("=> " + result); if (logfile != null) { logfile.println(expression); logfile.println("=> " + result); } } catch (NumberFormatException e) { System.out.println("Incorrect entry"); } } expression = getNextExpression(); } if (logfile != null) logfile.close(); } private String getNextExpression() { System.out.print("? "); return keyreader.nextLine(); } private void showMenu() { System.out.println("<expression> //Enter a simple arithmetic expression"); System.out.println("log <filename> //Turn on file logging to the specified output file"); System.out.println("log off //Turn file logging off"); System.out.println("quit //To terminate the program"); } private void setLogFile(String filename) { try { logfile = new PrintStream(filename); } catch (Exception e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { ExpressionDialog dialog = new ExpressionDialog(); if (args.length > 0) dialog.setLogFile(args[0]); dialog.run(); } }
- 02-22-2012, 09:49 PM #45
Re: Need help on a calculator project
How do you evaluate that? Where would you put the ()s?--5-3+-3 and give -1 as an answer.
Doing it this way:
-(-5-3)+(-3) gives 5
-(-5)-3+(-3) gives -1Last edited by Norm; 02-22-2012 at 10:01 PM. Reason: Correcting ()s
- 02-22-2012, 09:54 PM #46
Member
- Join Date
- Feb 2012
- Posts
- 26
- Rep Power
- 0
Re: Need help on a calculator project
well, I want the program to evaluate it as --5 (which would be 5) minus 3 (which would be 2) plus a negative 3 (which would be -1)
- 02-22-2012, 10:01 PM #47
Re: Need help on a calculator project
Correctly placed ()s help.
- 02-22-2012, 10:13 PM #48
Member
- Join Date
- Feb 2012
- Posts
- 26
- Rep Power
- 0
Re: Need help on a calculator project
oh, sorry. it would be -(-5)-3+(-3)
- 02-23-2012, 01:38 PM #49
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 02-24-2012, 05:32 AM #50
Member
- Join Date
- Feb 2012
- Posts
- 26
- Rep Power
- 0
Similar Threads
-
Need help with Calculator
By Joshua4missions in forum New To JavaReplies: 4Last Post: 12-10-2011, 02:44 AM -
Creating a project in eclipse from existing project
By Suraiya in forum New To JavaReplies: 1Last Post: 10-08-2011, 09:14 AM -
Help in a calculator
By Ayannie in forum New To JavaReplies: 6Last Post: 01-04-2011, 08:21 PM -
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks