小胖 發問時間: 電腦與網際網路程式設計 · 1 0 年前

如何用C++寫出一個calculator的程式?

現在正在學C++~有點搞不懂這題該怎麼寫= =''

我現在學的方法是說用loop的!

然後我們教的方法是用cin跟cout的

所以請教我這種的方法><

請會的大大教我一下喔><

Write a Program that models a simple calculator. Each data entry line should consist of a valid operator (from the list below), and the right-hand operand. Assume that the left-hand operand is the accumulated value in the calculator, with an initial value of 0.0.

Acceptable operators:

+ ..Add

- ..Subtract

* ..Multiply

/ ..Divide

^ ..Power (raise left operand to the power of the right operand)

q or = ..Quit

Your Calculator should display the accumulated value after each operation. A sample run might be:

+ 5.0

Result so far is 5.00

^ 2

Result so far is 25.00

/ 2.0

Result so far is 12.50

Q 0

The final result is 12.50

Include (define and call) at least THREE functions:

•a function that displays instructions to the user.

•a function do_next_op() that has 3 input parameters (the operator, the operand, and the current accumulated value), and returns the new value for the accumulated value. An alternative implementation may use 2 input parameters (operator and operand), and 1 input/output parameter (the accumulated value)

•at least one other function - of your choice! Make sure that it does something useful.

先給十點~真的解答問題我會在多給^^

拜託各位了

3 個解答

評分
  • Aiolus
    Lv 5
    1 0 年前
    最佳解答

    #include <iostream>

    #include <iomanip>

    #include <cstdlib>

    #include <math.h>

    using namespace std;

    double do_next_op(char op,double value,double in){

    switch(op){

    case '+':

    return in+value;

    case '-':

    return in-value;

    case '*':

    return in*value;

    case '/':

    return in/value;

    case '^':

    return pow(in,value);

    }

    return in;

    }

    void showIntro(){

    cout << "+ ..Add\n- ..Subtract\n* ..Multiply\n/ ..Divide\n^ ..Power\n";

    }

    int main(){

    double in=0,input;

    char op;

    cout << fixed << setprecision(2); //設定小數點輸出

    showIntro();

    while(true){

    cin>> op;

    cin >> input;

    if(op=='q' || op=='Q') break;

    in = do_next_op(op,input,in);

    cout << "Result so far is " << in << "\n";

    }

    cout << "The final result is " << in << "\n";

    system("pause");

    return 0;

    }

    ※注意

    •at least one other function - of your choice! Make sure that it does something useful.

    這個我沒寫OAO 請自己寫

  • 1 0 年前

    我也知道點數不能吃!我只是希望有人能幫忙而已....lol

    至於問老師有用的話我也不會來這邊問了= =''

  • 1 0 年前

    the last i checked, 點數不能吃,不能當消費卷來用.你還是去拜託老師比較快.

    2009-05-11 16:40:04 補充:

    有個好心的幫你起個頭了,剩下的想辦法做完吧.

    問老師沒用,有可能是你問的方法不對,或者是老師有問題.

    如果你用在這裡的方式問,完全看不出你有自己花任何時間去嘗試solve,我是你老師的話,你問老師是真的沒用.

    但面對面真的比較好教.

還有問題?馬上發問,尋求解答。