-5
  1. #ifndef CALC_H
    #define CALC_H
    
    
    class calc
    {
        double numb1;
        double numb2;
        public:
            calc();
            void FUN_SUM();
            void FUN_Subtraction();
            void FUN_Multiplication();
            void FUN_Division();
            void FUN_Power();
            void FUN_Squareroot();
            void FUN_Switch();
            void FUN_Loob();
    
    };
    
    #endif // CALC_H
    
    
    
    #include "calc.h"
    #include <iostream>
    #include <cmath>
    using namespace std;
    calc::calc()
    {
        numb1 = 0;
        numb2 = 0;
    }
    void calc::FUN_SUM()
    {
         cout << "Enter number 1 " << endl;
        cin >> numb1;
        cout << "Enter number 2 " << endl;
        cin >> numb2;
        double Result_Of_Sum;
        Result_Of_Sum = numb1+numb2;
        cout << "The result of Sum = " << Result_Of_Sum << endl;
    
    
    }
    
    void calc::FUN_Subtraction()
    {
         cout << "Enter number 1 " << endl;
     cin >> numb1;
     cout << "Enter number 2 " << endl;
     cin >> numb2;
     double Result_Of_Subtraction;
     Result_Of_Subtraction = numb1 - numb2;
     cout << "The result of Subtraction = " << Result_Of_Subtraction << endl;
    }
    void calc::FUN_Multiplication()
    {
         cout << "Enter number 1 " << endl;
        cin >> numb1;
        cout << "Enter number 2 " << endl;
        cin >> numb2;
        double Result_Of_Multiplication;
        Result_Of_Multiplication = numb1*numb2;
        cout << "The result of Multiplication = " << Result_Of_Multiplication << endl;
    
    }
    
    void calc::FUN_Division()
    {
        cout << "Enter number 1 " << endl;
        cin >> numb1;
        cout << "Enter number 2 " << endl;
        cin >> numb2;
        double Result_Of_Division ;
        Result_Of_Division = numb1/numb2;
        cout << "The result of Division = " << Result_Of_Division << endl;
    }
    
    void calc::FUN_Power()
    {
        cout << "Enter number 1 " << endl;
    cin >> numb1;
    cout << "Enter number 2 " << endl;
    cin >> numb2;
    double Result_Of_Power;
    Result_Of_Power = pow(numb1,numb2);
    cout << "The result of Power = " << Result_Of_Power << endl;
    }
    
    void calc::FUN_Squareroot()
    {
        cout << "Enter the tow number you want Square root \n";
        cin >> numb1;
    
        double Result_Of_Square_root;
        Result_Of_Square_root = sqrt(numb1);
        cout << "The result of Square root = " << Result_Of_Square_root << endl;
    }
    void calc::FUN_Switch()
    {
     int S;
        cout << "Enter the number you operator do you want do it  " << endl;
        cout << "1- Addition" << endl;
        cout << "2- Subtraction" << endl;
        cout << "3- Multiplication" << endl;
        cout << "4- Division" << endl;
        cout << "5- Power" << endl;
         cout << "6- Square Root" << endl;
          cout << "7- Exit" << endl;
        cin >> S;
    
        switch (S)
        {
        case 1:
    
            FUN_SUM();
            break;
    
        case 2:
    
            FUN_Subtraction();
            break;
    
        case 3:
    
            FUN_Multiplication();
            break;
    
        case 4:
    
            FUN_Division();
            break;
    
        case 5:
    
            FUN_Power();
            break;
    
        case 6:
    
            FUN_Squareroot();
            break;
    
        default :
            break;
    
        }
    }
    
    void calc::FUN_Loob()
    {
         char L;
    
        do
        {
    
            FUN_Switch();
    
     cout << "Do you want do another operator ( 'y' or 'n'?) \n";
            cin >> L;
    if (L== 'y' || L=='Y' || L=='n' || L=='N')
    
        continue;
    else
        cout << "you are enter roang later\n";
    
        }
    
        while (L == 'Y' || L == 'y' );
    
    }
    
    
    #include <iostream>
    #include "calc.h"
    using namespace std;
    
    int main()
    {
      cout << "Welcome to my simple Calculator\n";
    calc simple_calc;
    
    simple_calc.FUN_Loob();
    cout << "\n Tank you for use my App :) :) " << endl;
    
    return 0; 
    }
    

my question is how i can enable user Calc any value with operators like regular Calculator

for example 1+9*8-1+5 i want my program do like that but i don`t know how ?? :( :(

DJ Burb
  • 2,346
  • 2
  • 29
  • 38
ebdaa3sea
  • 25
  • 8

1 Answers1

0

Writing a calculator is not an easy task, as there is more involved:

  1. Operator Precedence
  2. Grouping
  3. Evaluating of expressions

Operator Precedence
Operator precedence is the grouping of operations, such as performing all multiplication and division before addition and subtraction. A calculator program should handle precedence without parenthesis (grouping).

Grouping
A more advanced calculator will allow grouping of expressions, using parenthesis. The calculator should be able to evaluate the inner most expression and work outward.

Evaluating of Expressions
This means allowing more than 1 digit numbers, and whitespace between numbers and symbols. Also, once the expression is parsed, it needs to be evaluated. The calculator should call the appropriate functions based on the operation (specified by the User).

I suggest you allow the User to enter an expression, and your program read it in as a string. Your program will then parse the string, then evaluate the expression(s).

Search the web and StackOverflow for "c++ calculator" for examples.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • thank you for answer me but i have question some Expressions must be do it from left to right how can i do it like this example 3-9+8 must be do it from left to right ?? – ebdaa3sea Nov 03 '14 at 20:23
  • If you scan your input string starting at index 0, you will be scanning from left to right. – Thomas Matthews Nov 03 '14 at 20:54
  • another question what about declare tow variable as pointer and create (for loop) to add number by user and another (for loop) for Expressions – ebdaa3sea Nov 03 '14 at 22:11
  • You really need to study the other examples. Too much to discuss on comments and StackOverflow. You could create a tree for the evaluation and parsing. – Thomas Matthews Nov 03 '14 at 22:22