In: Computer Science
Please code the following, using the language java!
Build a simple calculator that ignores order of operations. This “infix” calculator will read in a String from the user and calculate the results of that String from left to right. Consider the following left-to-right calculations:
"4 + 4 / 2" "Answer is 4" //not 6, since the addition occurs first when reading from left to right
“1 * -3 + 6 / 3” “Answer is 1” //and not -1Start by copying the driver code below.
Read this program and notice the comments before you proceed –your assignments in the lecture section will require you to comment accordingly, or they will drop a complete letter grade! Begin tracing the program in main, and your code will go in the calculate() function, so start writing code there. Hints:
import ?; /* * InFixCalc, V0.0 (concept borrowed from Carol Zander's Infix Calculator) * Complete the calculate() function below to build a simple, infix * calculator that evaluates a compound expression from left to right, * regardless of operator precedence * * Example: " 1 * -3 + 6 / 3" * Execution: calculate 1 * -3 first, then -3 + 6 next, then 3 / 3 * last to obtain the value 1 * * Solution by */ public class InFixCalc { //example pattern: "3 + 5" //general pattern: <lhs='3'> <operation='+'> <rhs='5'> //extended pattern: ... //special case: //other special cases? public static void main(String[] args) { //String input = "4 + 4"; //String input = "4 + 4 / 2"; //String input ="1 * -3"; String input ="1 * -3 + 6 / 3"; //String input ="5"; //String input ="-5"; int answer = calculate(input); System.out.println("Answer is " + answer); } //preconditions: all binary operations are separated via a space //postconditions: returns the result of the processed string public static int calculate(String input) { int lhs,rhs; //short for left-hand & right-hand side char operation; /*todo: your name and code goes here*/ /*You need a Scanner(or StringTokenizer) to get tokens *Then you need a loop, and switch inside that loop*/ return lhs; } }
If you have any doubts, please give me comment...
import java.util.*;
/*
* InFixCalc, V0.0 (concept borrowed from Carol Zander's Infix Calculator)
* Complete the calculate() function below to build a simple, infix
* calculator that evaluates a compound expression from left to right,
* regardless of operator precedence
*
* Example: " 1 * -3 + 6 / 3"
* Execution: calculate 1 * -3 first, then -3 + 6 next, then 3 / 3
* last to obtain the value 1
*
* Solution by
*/
public class InFixCalc {
// example pattern: "3 + 5"
// general pattern: <lhs='3'> <operation='+'> <rhs='5'> //extended pattern: ...
// special case:
// other special cases?
public static void main(String[] args) { // String input = "4 + 4";
// String input = "4 + 4 / 2";
// String input ="1 * -3";
String input = "1 * -3 + 6 / 3";
// String input ="5";
// String input ="-5";
int answer = calculate(input);
System.out.println("Answer is " + answer);
}
// preconditions: all binary operations are separated via a space
// postconditions: returns the result of the processed string
public static int calculate(String input) {
int lhs, rhs; // short for left-hand & right-hand side
char operation;
/* todo: your name and code goes here */
/*
* You need a Scanner(or StringTokenizer) to get tokens Then you need a loop,
* and switch inside that loop
*/
Scanner scnr = new Scanner(input);
lhs = scnr.nextInt();
while(scnr.hasNext()){
operation = scnr.next().charAt(0);
rhs = scnr.nextInt();
switch(operation){
case '+':
lhs += rhs;
break;
case '-':
lhs -= rhs;
break;
case '*':
lhs *= rhs;
break;
case '/':
lhs /= rhs;
break;
}
}
return lhs;
}
}