-
March 7th, 2011, 09:27 AM
#1
calculator! need help!! thanks!!
I need to make a calculator program where the user inputs a mathematical term ( 1-2+3*4/5)
then when the user presses enter the result will be displayed.
for example:
Enter numbers to be calculated: 3+4*3
Result: 15
Here is my program. It works but only for 1 term (3*4) or (923 / 37). When i enter multiple operators (23*34+545-45) it only shows the result of the last term of the input (in this case it prints out 500). Im having a hard time doing the program specially with the precedence of the operators. Can someone help me pls? Thanks..
import java.io.*;
public class Calcu
{
public static void main(String[]args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
int num1, num2, ctr = 0, sum, diff, prod, quo, result = 0;
System.out.print("Enter numbers to calculate: ");
input = br.readLine();
String arr[] = new String[input.length()];
for(int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
String s = Character.toString(c);
arr[i] = s;
if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/"))
ctr++;
}
int size = ctr + (ctr+1), j = 0;
String arr1[] = new String[size];
for(int i = 0; i < size; i ++)
{
arr1[i] = "";
}
for(int i = 0; i != arr.length; i++) {
if(arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/"))
{
j++;
arr1[j] = arr1[j].concat(arr[i]);
j++;
}
else
{
arr1[j] = arr1[j].concat(arr[i]);
}
}
for(int i = 0; i < size; i++)
{
if(arr1[i].equals("*"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
prod = num1 * num2;
result = prod;
arr1[i-1] = Integer.toString(result);
}
if(arr1[i].equals("/"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
quo = num1 / num2;
result = quo;
arr1[i-1] = Integer.toString(result);
}
if(arr1[i].equals("+"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
sum = num1 + num2;
result = sum;
arr1[i-1] = Integer.toString(result);
}
if(arr1[i].equals("-"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
diff = num1 - num2;
result = diff;
arr1[i-1] = Integer.toString(result);
}
}
System.out.print(result);
}
}
-
March 7th, 2011, 10:12 AM
#2
Re: calculator! need help!! thanks!!
Use code tags when posting code and add comments to your code so we understand what you are trying to do.
-
March 7th, 2011, 10:28 AM
#3
Re: calculator! need help!! thanks!!
Code:
import java.io.*;
public class Calcu
{
public static void main(String[]args) throws IOException
{
//variables
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
int num1, num2, ctr = 0, sum, diff, prod, quo, result = 0;
//getting user input
System.out.print("Enter numbers to be calculated: ");
input = br.readLine();
//declaring 1st array for user input
String arr[] = new String[input.length()];
//switching string to char to string and store it in the array
for(int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
String s = Character.toString(c);
arr[i] = s;
//counter for the size of next array
if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/"))
ctr++;
}
//size of arr1 and declaration
int size = ctr + (ctr+1), j = 0;
String arr1[] = new String[size];
//loop for arr1 element that assigns an empty string
for(int i = 0; i < size; i ++)
{
arr1[i] = "";
}
//sorting operands and operators and string them to arr1
for(int i = 0; i != arr.length; i++)
{
if(arr[i].equals("+") || arr[i].equals("-") || arr[i].equals("*") || arr[i].equals("/"))
{
j++;
arr1[j] = arr1[j].concat(arr[i]);
j++;
}
else
{
arr1[j] = arr1[j].concat(arr[i]);
}
}
//performing the operators
for(int i = 0; i < size; i++)
{
if(arr1[i].equals("*"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
prod = num1 * num2;
result = prod;
arr1[i-1] = Integer.toString(result);
}
if(arr1[i].equals("/"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
quo = num1 / num2;
result = quo;
arr1[i-1] = Integer.toString(result);
}
if(arr1[i].equals("+"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
sum = num1 + num2;
result = sum;
arr1[i-1] = Integer.toString(result);
}
if(arr1[i].equals("-"))
{
num1 = Integer.parseInt(arr1[i-1]);
num2 = Integer.parseInt(arr1[i+1]);
diff = num1 - num2;
result = diff;
arr1[i-1] = Integer.toString(result);
}
}
//prints out result
System.out.print(result);
}
}
sorry it is my first time posting here...hope this is ok
-
March 7th, 2011, 10:53 AM
#4
Re: calculator! need help!! thanks!!
My that's a complex way of attempting to solve the problem and it won't handle operator precedence.
I suggest you google for something like "evaluating math expressions" and read a few articles on the standard approaches to solving this type of problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|