Jump to content

Recommended Posts

Posted

[C++] Calculator

 

 

 

(cpp):
#include <iostream>

using namespace std;

//addition
float addition( float num1, float num2)
{ return num1 + num2; }

//subtraction
float subtraction(float num1, float num2)
{return num1-num2;}

//multiplication
float multiplication(float num1, float num2)
{return num1*num2;}

//division
float division(float num1, float num2)
{return num1/num2;}


//main function
int main()
{
    float number1;
    float number2;
    int choice;
    cout<<"What would you like to do?(1=add, 2=subtract, 3=multiply, 4=divide)"<<endl;
    cout<<"Choice:"<<endl;
    cin>>choice;

    if(choice==1)
    {//addition
    cout<<"What is your first number?(addition)"<<endl<<"Number:";
    cin>>number1;
    cout<<"What would you like to add?"<<endl<<"Number:";
    cin>>number2;
    cout<<"Your Answer is:"<<addition(number1, number2)<<endl;}
   else
   if(choice==2)
    {//subtraction
    cout<<"What is your first number(subtraction)?"<<endl<<"Number:";
    cin>>number1;
    cout<<"What number would you like to subtract?"<<endl<<"Number:";
    cin>>number2;
    cout<<"Your Answer is:"<<subtraction(number1, number2)<<endl;}
  else
  if(choice==3)
    {//multiplication
    cout<<"What is your first number(Multiplication)?"<<endl<<"Number:";
    cin>>number1;
    cout<<"What number would you like to multiply?"<<endl<<"Number:";
    cin>>number2;
    cout<<"Your Answer is:"<<multiplication(number1, number2)<<endl;}
  else
    //division
    cout<<"What is your first number(Divide)?"<<endl<<"Number:";
    cin>>number1;
    cout<<"What number would you like to Divide by?"<<endl<<"Number:";
    cin>>number2;
    cout<<"Your Answer is:"<<division(number1, number2)<<endl;

    system("PAUSE");
    return 0;
}

 

Source code: Download

 

 

 

Calculator-2

(cpp):
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int  left, right;   // Operands
    char oper;          // Operator
    int  result;        // Resulting value

    while (cin >> left >> oper >> right) {
        switch (oper) {
            case '+': result = left + right; 
                      break;
            case '-': result = left - right; 
                      break;
            case '*': result = left * right; 
                      break;
            case '/': result = left / right; 
                      break;
            default : cout << "Bad operator '" << oper << "'" << endl;
                      continue;  // Start next loop iteration.
        }
        cout << result << endl << endl;
    }

    return 0;
}

 

 

 

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...