Making a Calculator

import java.io.*;
public class Calcsi
{
public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“********* 100 Digit Calculator *********”);
System.out.println(“Enter the equation”);
String eq = br.readLine();  //enter the equation you want to solve.
int num1 = 0,  i,  j, k = 0, l = 0, flag = 0;
char a[] = new char[]{‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9’};  //array of characters to store the probable digits which maybe present in a no.
int num[] = new int[100];  //array for storing the nos in the equation
char symb[] = new char[100];   //array for storing the operands in the equation
if(eq.charAt(0) == ‘-‘)  //checking if the first no. is negative or not.
{
flag++;
i = 1;
} // if
else
i = 0;

loop1: for(; i < eq.length(); i++)
{
for(j = 0; j <= 9; j++)       //loop for checking the digits of a no.
{
if(eq.charAt(i) == a[j])
{

num1 = num1 * 10 + (int)(a[j] – 48);   //framing the no.
continue loop1;
}// if
}// loop 2

if(num1 != 0)
{

num[l] = num1;   //storing the first eq. no. in the array of nos.
l++;
num1 = 0;
if(flag > 0)     //if no. is negative
{
num[l – 1] = num[l – 1] – (num[l – 1] * 2);   //logic for converting a no from positive to negative.
flag = 0;
}// inner if
}// outer if

if(eq.charAt(i)== ‘+’)   // checking the next operation is addition or not
{
symb[k] = ‘+’;   //storing the operand in the operand array
k++;
if(eq.charAt(i + 1) == ‘-‘)  //checking if the next no in the eq. is negative or not
{
flag++;
i++;
continue;  //Going up to frame the next no.
}// inner if
}// outer if
if(eq.charAt(i)== ‘-‘)   //checking if the next operation is negative or not
{
symb[k] = ‘-‘;
k++;
if(eq.charAt(i + 1) == ‘-‘)   // Checking if the next no is negative or not
{
flag++;
i++;
continue; // Going up to frame the next no.
}// inner if
}// outer if
if(eq.charAt(i)== ‘/’)    // Checking if the next operation is division or not
{
symb[k] = ‘/’;
k++;
if(eq.charAt(i + 1) == ‘-‘) // Checking if the next no. is negative or not
{
flag++;
i++;
continue; // Going up to frame the next no.
}// inner if
}// outer if
if(eq.charAt(i)== ‘*’)  // Checking if the next operation is multiplication
{
symb[k] = ‘*’;
k++;
if(eq.charAt(i + 1) == ‘-‘)  // Checking if the next no is negative
{
flag++;
i++;
continue;  // Going up to frame the next number.
}// inner if
}// outer if
if(eq.charAt(i)== ‘=’)  //Checking if the next operation to perform is to get the answer
{
symb[k] = ‘=’;
k++;

}// if
}// loop 1
int sum = num[0];
for(i = 1; i < l; i++) // Loop for finding the answer
{
if(symb[i-1] == ‘+’)
sum = sum + num[i];  // Adding

if(symb[i-1] == ‘-‘)
sum = sum – num[i];  // Subtracting

if(symb[i-1] == ‘*’)
sum = sum * num[i];  // Multiplying

if(symb[i-1] == ‘/’)
sum = sum / num[i];  // Dividing
}// for

if(symb[i – 1] == ‘=’) // Finding the answer
System.out.print(sum);
}// Main
}// Class

Advertisements

2 thoughts on “Making a Calculator”

  1. You’re so awesome! I do not believe I have read anything like this before. So good to discover another person with some genuine thoughts on this topic. Really.. thanks for starting this up. This web site is something that is needed on the internet, someone with a bit of originality!

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s