Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 19-12-2008, 11:59 AM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Aug 2008
Posts: 8
Default [C++] Need help in calculation of more than 2 numbers


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
a5hr1th is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 19-12-2008, 12:20 PM   #2 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
Default Re: [C++] Need help in calculation of more than 2 numbers

covert infix expression to postfix/prefix and then evaluate postfix/prefix expression:
http://scriptasylum.com/tutorials/in...tion/index.htm



lol
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono

Last edited by Faun; 19-12-2008 at 12:26 PM.
Faun is offline  
Old 19-12-2008, 03:01 PM   #3 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default Re: [C++] Need help in calculation of more than 2 numbers

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;
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
redhat is offline  
Old 19-12-2008, 06:02 PM   #4 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: [C++] Need help in calculation of more than 2 numbers

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
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 19-12-2008, 06:48 PM   #5 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default Re: [C++] Need help in calculation of more than 2 numbers

^^ 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??
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
redhat is offline  
Old 20-12-2008, 12:16 AM   #6 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
Default Re: [C++] Need help in calculation of more than 2 numbers

^^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
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is offline  
Old 20-12-2008, 06:56 AM   #7 (permalink)
In The Zone
 
cooldip10's Avatar
 
Join Date: Mar 2005
Location: New Delhi
Posts: 493
Default Re: [C++] Need help in calculation of more than 2 numbers

Yaar the coding you have done looks good.. but I suggest you one thing.. (Have completed XIIth in 2007 with Computer Science )
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??
__________________
Everything is Possible
cooldip10 is offline  
Old 20-12-2008, 09:33 AM   #8 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: [C++] Need help in calculation of more than 2 numbers

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.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Old 20-12-2008, 05:01 PM   #9 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: [C++] Need help in calculation of more than 2 numbers

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.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 29-12-2008, 12:21 PM   #10 (permalink)
Linux all the way
 
Join Date: Mar 2008
Location: Rasayani
Posts: 155
Default Re: [C++] Need help in calculation of more than 2 numbers

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.
__________________
At times life takes a U turn and you get back where you were...
Vishal Patil is offline  
Old 30-12-2008, 09:55 PM   #11 (permalink)
* Teh Flirt King *
 
Quiz_Master's Avatar
 
Join Date: Dec 2005
Location: Originally From : Ratlam M.P., Currently in: Hyderabad
Posts: 972
Default Re: [C++] Need help in calculation of more than 2 numbers

[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!
__________________
World is just a Quizzical Reality : Quiz_Master//Ashwin :D

Blog: http://ashwinsaxena.com/blog - Tech, Life and Other Things.
Quiz_Master is offline  
Old 31-12-2008, 03:03 PM   #12 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
Default Re: [C++] Need help in calculation of more than 2 numbers

^^Yeah I think its in 12th class book, postfix prefix is used in stack chapter
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Port Bandwidth Calculation in Java sachin_kothari Programming 1 13-03-2008 03:02 PM
MS Excel: Formula for calculation in entire column g_goyal2000 Software Q&A 3 27-04-2007 01:59 PM
IP range calculation?????? taken Software Q&A 2 13-07-2006 11:18 AM
Usb pin numbers beyondthegracefgod Peripherals 5 07-11-2004 02:51 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:03 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2