[C++] Need help in calculation of more than 2 numbers

Status
Not open for further replies.

a5hr1th

Right off the assembly line
I've made this Calculator which can only calculate for 2 inputs only. How should I write the program, so that, the user can give multiple inputs and the calculation is performed?

Code:
/*
Work of Ashrith Babu Rao, XII
Email:  a5hr1th.mb@gmail.com
(c) 2008
*/

#include <iostream.h>               
#include <conio.h>
#include <math.h>
int main()
{    clrscr();
      float P, R, T;
      int ch;
      float x,y;
      char ch1;
      do

     {

         clrscr();
         cout<<"CALCULATOR:\n----------\n\n";
      	cout<<"SIMPLE CALCULATIONS:\n-------------------\n1: Addition\n2:                  Subtraction\n3: Multiplication\n4: Division\n\n"<<endl;
         cout<<"SCIENTIFIC:\n----------\n5:  Power\n6:  Logarithm\n7:  Sin\n8:  Cos\n9:  Tan\n10: Sin Inverse\n11: Cos Inverse\n12: Tan Inverse\n"<<endl;
         cout<<"\nINTEREST CALCULATOR:\n-------------------\n13: Simple Interest\n14: Compound Interest\n\n";
      	cout<<"\nEnter Choice"<<endl;
         cin>>ch;

     	 	if(ch==1)
      	{  clrscr();
            cout<<"Addition\n--------\n\n";
            cout<<"Enter two numbers"<<endl;
            cin>>x>>y;
    	  		cout<<"\nAnswer: "<<(x+y);

         }
         if(ch==2)
      	{	clrscr();
            cout<<"Subtraction\n-----------\n\n";
         	cout<<"Enter two numbers"<<endl;
      		cin>>x>>y;
      		cout<<"\nAnswer: "<<(x-y);

         }
         if(ch==3)
      	{	clrscr();
            cout<<"Multiplication\n--------------\n\n";
         	cout<<"Enter two numbers"<<endl;
      		cin>>x>>y;
      		cout<<"\nAnswer: "<<(x*y);

         }
         if(ch==4)
      	{	clrscr();
            cout<<"Division\n--------\n\n";
         	cout<<"Enter two numbers"<<endl;
      		cin>>x>>y;
      		cout<<"\nAnswer: "<<(x/y);

         }
         if(ch==5)
         {  clrscr();
            cout<<"Power ** x^y **\n---------------\n\n";
         	cout<<"Enter two numbers"<<endl;
      		cin>>x>>y;
      		cout<<"\nAnswer: "<<pow(x,y);

         }
         if(ch==6)
      	{	clrscr();
            cout<<"Logarithm\n---------\n\n";
         	cout<<"Enter a number"<<endl;
      		cin>>x;
      	  	cout<<"\nAnswer: "<<log10(x);

         }
         if(ch==7)
      	{	clrscr();
            cout<<"Sine of X\n---------\n\n";
         	cout<<"Enter a value"<<endl;
      		cin>>x;
      	  	cout<<"\nAnswer: "<<asinl(x);

         }
         if(ch==8)
      	{	clrscr();
            cout<<"Cosine of X\n-----------\n\n";
         	cout<<"Enter a value"<<endl;
      		cin>>x;
      	  	cout<<"\nAnswer: "<<acosl(x);

         }
         if(ch==9)
      	{	clrscr();
            cout<<"Tangent of X\n------------\n\n";
         	cout<<"Enter a value"<<endl;
      		cin>>x;
      	  	cout<<"\nAnswer: "<<atanl(x);

         }
         if(ch==10)
      	{	clrscr();
            cout<<"Sine Inverse of X (Round off to nearest integer)\n------------------------------------------------\n\n";
         	cout<<"Enter a value"<<endl;
      		cin>>x;
      	  	cout<<"\nAnswer: "<<asin(x)*180/3.1416;

         }
         if(ch==11)
      	{	clrscr();
            cout<<"Cosine Inverse of X (Round off to nearest integer)\n--------------------------------------------------\n\n";
         	cout<<"Enter a value"<<endl;
      		cin>>x;
      	  	cout<<"\nAnswer: "<<acos(x)*180/3.1416;

         }
         if(ch==12)
      	{	clrscr();
            cout<<"Tangent Inverse of X (Round off to nearest integer)\n---------------------------------------------------\n\n";
         	cout<<"Enter a value"<<endl;
      		cin>>x;
      	  	cout<<"\nAnswer: "<<asin(x)*180/3.1416;

         }
         if(ch==13)
      	{	clrscr();
            cout<<"Simple Interest\n---------------\n\n";
            cout<<"Enter Principal Amount: ";
            cin>>P;
            cout<<"\nEnter Rate of Interest: ";
            cin>>R;
            cout<<"\nEnter Time Period in Years: ";
            cin>>T;
      	  	cout<<"Simple Interest = Rs. "<<(P*R*T)/100;

         }
         if(ch==14)
      	{	clrscr();
            cout<<"Compound Interest\n-----------------\n\n";
            cout<<"Enter Principal Amount: ";
            cin>>P;
            cout<<"\nEnter Rate of Interest: ";
            cin>>R;
            cout<<"\nEnter Time Period in Years: ";
            cin>>T;
      	  	cout<<"Compound Interest = Rs. "<<(P*pow((1+R/100),T)-P);

         }


      cout<<"\n\nEnter More?(Y/N)"<<endl;
      cin>>ch1;
      }while(ch1=='y');

      clrscr();
      cout<<"\n\nThank you for using!";
      cout<<"\n(c) Ashrith Babu Rao, 2008";
      getch();
}

And before someone asks, I use Borland C++ v5.02
 

Faun

Wahahaha~!
Staff member
covert infix expression to postfix/prefix and then evaluate postfix/prefix expression:
*scriptasylum.com/tutorials/infix_postfix/algorithms/postfix-evaluation/index.htm



lol
 
Last edited:

redhat

Mad and Furious
Create a do-while loop that ends when the user enters a particular char. eg "=".
This way,

if(ch==1)
{
clrscr();
cout << "Addition\n--------\n\n";
cout << "Enter the numbers"<<endl;
double x, sum;
do
{
cin >> x;
sum += x;
}while(x != 0.0)

cout << "\nAnswer: " << sum;
 

dheeraj_kumar

Legen-wait for it-dary!
Get the user to enter a large string like "2+4+5+6-9/5*3" and parse the expression :) that should be more than impressive for your school staff :)
 

redhat

Mad and Furious
^^ Pretty good technique
Had it been Java i would have used it... but since im very new to c++, i dont know how to parse and break a string.
If you could please help with the syntax??
 

Faun

Wahahaha~!
Staff member
^^lol...you have to take care of precedence and brackets too, if any

A good technique is to first convert it to prefix or postfix form and then evaluate easily using simple stack
 

cooldip10

In the zone
Yaar the coding you have done looks good.. but I suggest you one thing.. (Have completed XIIth in 2007 with Computer Science :lol:)
Advice: Instead of using so many (if.. else) controls .. use a single switch(ch1) control.. This way you will have a better organised programme to look @.
Secondly, use functions buddy.. e.g for Addition use a function named Add().
I tell you teachers really get impressed with these techniques I've told you.. worked for me! Will definitely work for you..

BTW which school r u frm??
 

dheeraj_kumar

Legen-wait for it-dary!
Yep, As T159 said, the best way to evaluate is to convert it to postfix, and use a stack to evaluate it. I'm sure you would have learnt postfix and stacks at school. I remember learning that.
 

QwertyManiac

Commander in Chief
Here's an explaination of stacks and a simple stack implementation in C that you can use for this purpose, as instructed already by many above.
 

Vishal Patil

Linux all the way
put to loop to read more numbers and exit the loop when last number entered is zero.
Caution: Enter the message to enter 0 to exit.

You can add subtract and multiply more than 2 numbers by this method.
 

Quiz_Master

* Teh Flirt King *
[offtopic] @qwertmaniac and @t159 Do you really think that in India they teach Prefix/Postfix at school level ? [/offtopic]

@a5hr1th Use descriptive variable names dude! Its considered a good programming practice.

Anyways since in using this prefix-postfix thingy.. You can look at my code.. see if this makes any sense to you... If not, I suggest don't bother. Its not worth it.

Code:
/*
Coded By n00bish_king_of_Jellula aka Quiz_Master.
*/

#include <iostream>
#include <stack> 
#include <string>
using namespace std;

void main()
{
    int i, selection = 1;
    string postfixExp;
    char token;
    float value, value1, value2;
    stack<float> s; //Declare a stack of floats

    while (selection != 0)
    {
        cout << "1. Evaluate a postfix expression" << endl;
        cout << "0. Exit " << endl;
        cout << "Enter the number for the option: ";

        cin >> selection;
        switch(selection)
        {
            case 1: cout << "Evaluate a postfix expression\n";
                    cout << "Enter the expression: ";
                    cin >> postfixExp;
                    i = 0;
                    token = postfixExp[i];
                    while((i < postfixExp.size()) && (token != '='))
                    {
                        if(isdigit(token))
                        {
                            value = token - '0';
                            s.push(value);
                        }
                        else
                        {
                            value2 = s.top();
                            s.pop();
                            value1 = s.top();
                            s.pop();
                            switch(token)
                            {
                                case '+': value = value1 + value2;
                                          break;
                                case '-': value = value1 - value2;
                                          break;
                                case '*': value = value1*value2;
                                          break;
                                case '/': value = value1/value2;
                                          break;
                            }
                            s.push(value);
                        }
               i++;
               token = postfixExp[i];
                    }
                    value = s.top();
                    s.pop();
                    cout << postfixExp << " " << value << endl;         
                    break;

            case 0: cout << "Exiting the program\n";
                    break;

            default: cout << "Invalid option\n";
                    break;
        }
     cout << endl;
    }
}



Opps sorry.. I forgot..I am no more visiting this forum! Ignore this please! I was NOT even here... An AI spyware is impersonating me! :p
 

Faun

Wahahaha~!
Staff member
^^Yeah I think its in 12th class book, postfix prefix is used in stack chapter
 
Status
Not open for further replies.
Top Bottom