-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; 
    }
    

我的问题是如何使用常规计算器等运算符启用用户计算任何值

例如 1+9*8-1+5 我希望我的程序这样做,但我不知道怎么做??:( :(

4

1 回答 1

0

编写计算器并不是一件容易的事,因为涉及的内容更多:

  1. 运算符优先级
  2. 分组
  3. 表达式的评估

运算符优先级
运算符优先级是操作的分组,例如在加减之前执行所有乘法和除法。计算器程序应该处理不带括号的优先级(分组)。

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

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

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

在网络和 StackOverflow 中搜索“c++ 计算器”以获取示例。

于 2014-11-03T19:41:28.620 回答