查看完整版本: 作業想不透
頁: [1]

j830529g 發表於 2016-12-3 10:47 AM

作業想不透

用Dev-C++來寫程式
要用 if 和 else 寫一個簡易計算機( + - * / )程式
首先加法部分已完成,但( - * / )每次執行都是用加的,想不出該如何修改
請各位大大能給我建議,謝謝。
程式碼:
#include <cstdlib>
#include <iostream>


using namespace std;


int main(int argc, char *argv[])
{
    int a;
    int b;
    char c;
   
    cout << "輸入整數:";
    cin >> a;
   
    cout << "輸入整數:";
    cin >> b;
   
    cout << "輸入運算符號:";
    cin >> c;
   
    if(a + b)
    {
     cout << "答案:" << a + b << endl;
    }
    else if(a - b)
    {
     cout << "答案:" << a - b << endl;
    }
    else if(a * b)
    {
     cout << "答案:" << a * b << endl;
    }
    else
    {
     cout << "答案:" << a / b << endl;
    }
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

scottcheng 發表於 2016-12-4 01:14 AM

本帖最後由 scottcheng 於 2016-12-4 01:15 AM 編輯

你的  if 不是應該要判斷 運算符號嗎?  例如  if (c == '+')  then cout < a+b < endl
以此類推..

你用 if (a + b) ...等等, 當然這個 always true..只有 + 會被跑

j830529g 發表於 2016-12-4 10:10 AM

scottcheng 發表於 2016-12-4 01:14 AM static/image/common/back.gif
你的  if 不是應該要判斷 運算符號嗎?  例如  if (c == '+')  then cout < a+b < endl
以此類推..



好的,我知道了。
謝謝大大的教導。
頁: [1]