Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

calculator c++

#include <iostream>
#include <string>
#include <unistd.h>

using namespace std;

class Calculator
{
private:
    char i_numOperator;
    float i_a = 00.0f, i_b = 00.0f;
public:
    Calculator(char numOperator, float a, float b)
    {
        i_numOperator = numOperator;
        i_a = a;
        i_b = b;
    }

    float the_calculator()
    {
        switch (i_numOperator)
        {
        case '+':
            return (i_a + i_b);
            break;
        case '-':
            return (i_a - i_b);
            break;
        case '*':
            return (i_a * i_b);
            break;
        case '/':
            return (i_a / i_b);
            break;
        
        default:
            cout << "invalid operator please try again
";
            break;
        }
    }
};

int main() {
    char again = 'y';
    while(again == 'y' || again == 'Y')
    {
        int a = 00.0f, b = 00.0f;
        char op;

        cout << "first num
";
        cin >> a;
        cout << "operator
";
        cin >> op;
        cout << "second num
";
        cin >> b;

        Calculator calt(op, a, b);

        cout << "result : " << calt.the_calculator() << endl;

        cout << "do you want to use it again?(Y/N)
";
        cin >> again;
        system("clear");   
    }
    system("clear");
    cout << "ok.. bye!
";
    sleep(2);
    system("clear");

    return 0;
}

 
PREVIOUS NEXT
Tagged: #calculator
ADD COMMENT
Topic
Name
2+5 =