使用许多算术运算符c ++计算很多值

时间:2014-11-03 19:03:59

标签: c++ math operators calc

  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; 
    }
    
  2. 我的问题是如何使用常规计算器

    等运算符为用户Calc启用任何值

    例如1 + 9 * 8-1 + 5我想我的程序是这样的,但我不知道如何? :( :(

1 个答案:

答案 0 :(得分:0)

编写计算器并非易事,因为涉及的内容更多:

  1. 运营商优先
  2. 分组
  3. 评估表达
  4. 运营商优先
    运算符优先级是操作的分组,例如在加法和减法之前执行所有乘法和除法。计算器程序应该处理优先级而不括号(分组)。

    <强>组合
    更高级的计算器将允许使用括号对表达式进行分组。计算器应该能够评估最内层的表达并向外工作。

    评估表达式
    这意味着允许超过1位数字,以及数字和符号之间的空格。此外,一旦表达式解析,就需要对其进行评估。计算器应根据操作(由用户指定)调用适当的函数。

    我建议您允许用户输入表达式,并且您的程序将其作为字符串读取。然后,您的程序将解析字符串,然后评估表达式。

    在网络和StackOverflow中搜索&#34; c ++计算器&#34;例如。