2014-11-03 84 views
-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; 
    } 
    

我的問題是如何我可以使用戶計算值的任何值與像普通計算器運營計算值許多值與許多算術運算符C++

例如1 + 9 * 8-1 + 5我希望我的程序可以這樣做,但我不知道怎麼做? :(:(

+0

請格式化您的文章。小心些。 – 2014-11-03 19:08:08

+0

爲「C++計算器」搜索StackOverflow,並且會顯示大量示例。研究它們。 – 2014-11-03 19:33:43

回答

0

編寫計算器不是一件容易的事,因爲有更多的參與:

  1. 運算符優先級
  2. 分組
  3. 表達式的評估

運算符優先級
運算符優先級是運算的分組,例如執行所有乘法加減之前的分割和分割。計算器程序應該處理沒有括號(分組)的優先級。

分組
更高級的計算器將允許使用圓括號對錶達式進行分組。計算器應該能夠評估最內在的表達並向外工作。

求表達式
這意味着允許多於1位的數字,和數字和符號之間的空格。此外,一旦表達式解析,它需要評估。計算器應根據操作(由用戶指定)調用適當的功能。

我建議你允許用戶輸入一個表達式,並且你的程序以字符串的形式讀取它。然後您的程序將解析字符串,然後評估表達式。

搜索網絡和StackOverflow的「C++計算器」的例子。

+0

謝謝你回答我 但我有問題一些表達式必須做到從左到右我怎麼能這樣做這個例子3-9 + 8必須做到從左到右? – ebdaa3sea 2014-11-03 20:23:36

+0

如果您掃描從索引0開始的輸入字符串,則將從左向右掃描。 – 2014-11-03 20:54:50

+0

另一個問題如何聲明拖動變量作爲指針和創建(for循環)爲用戶添加數字和用於表達式的另一個(for循環) – ebdaa3sea 2014-11-03 22:11:52