簡體   English   中英

一個簡單的加,減,乘和除法程序

[英]A simple addition,substraction,multiplication and division program

一個簡單的C ++計算器程序,用於計算加法,減法,乘法和加法...

#include <iostream>
#include <string>
using namespace std;

int main()
{
    //input declarations as doubles for total and counter
    double total = 0, counter =0;
    //input declarations sign and Q as character
    char sign, Q = 0;
    //input declaration value as double
    double value;



        //A do..while will loop forever (or until we hit the break statement)
        do 
        {
            //The current value is 0.
            cout << "Result :"<<" "<< total << '\n';

            //Please enter an operation
            cout << "Please enter an operation and number : ";
        cin >> sign;

        //If the operation is Q, the program will end.
        if (sign != 'Q')

        cin >> value;
            cin.ignore();


            // If the value input is <=0, you can't divide anything by zero. 
            if (value <= 0)
            {
                cout << "Unknown Operator " << sign <<'\n' ;
            }

            //Otherwise procede with the calulator program
            else
            {
                //If the operation is equal to '+', then the total is added.
                if (sign == '+')
                { 
                    total += value;
                }

                // If the operation is equal to '-', then the value is subtracted from the previous number input. 
                else
                {
                    if (sign == '-')
                    {
                        total -= value;
                    }

                    // If the operation is equal to '*', then the value is multiplied to the previous number input. 
                    else
                    {
                        if (sign == '*')
                        {
                            total *= value;
                        }

                        // If the operation is equal to '/', then the value is divided by the previous number input.
                        else 
                        {
                            if ((sign == '/')&&(value != 0))
                            {
                                total /= value;
                            }
                        }
                    }
                }
            }
        }



            //While the operation is not equal to 'Q', the program will run.
            while (sign != 'Q');



        return 0;
}

上面程序的編碼沒有錯誤,但是如果我按“ Q”退出,它將不停地顯示最后的結果。 一遍又一遍又一遍。 無論如何,任何人都知道如何在程序中添加平方根。

if (sign != 'Q') ...替換為if (sign == 'Q') break;

  • 這將修復許多可能的錯誤。
  • 這將通過減少縮進和花括號來使代碼更具可讀性。

編輯:正如有人提到的那樣,您可能也應該檢查小寫字母。 (如果(sign =='Q'|| sign =='q'))。

#include <math.h>
double result = sqrt (value);

對於平方根, #include <math> double r=sqrt(e);

關於其他問題:請更好地縮進代碼;-)

您確定您不會忘記在此行之后放一個方塊嗎? if (sign != 'Q')

最簡單的方法是,在步進模式下使用調試器,您將了解!

作為一個簡單的練習,我對您的程序進行了重構以使其更簡單。 我不保證它會起作用,但是它應該為您提供良好的基礎:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  //input declarations as doubles for total and counter
  double total = 0, counter =0;
  //input declarations sign and Q as character
  char sign = 0;
  //input declaration value as double
  double value;

  //A do..while will loop forever (or until we hit the break statement)
  do 
  {
    //The current value is 0.
    cout << "Current result: " << total << '\n';

    //Please enter an operation
    cout << "Please enter an operation (or Q to quit) and number: ";
    cin >> sign;

    //If the operation is Q, the program will end.
    if (sign == 'Q' || sign == 'q') {
      cout << "See you!\n";
      break;
    }

    // If the input cannot be transformed into a double
    // abort loop and try again
    if (!(cin >> value)) {
      cerr << "Invalid value entered, try again\n";
      continue;
    }

    switch(sign)
    {
    case '+': total += value; break;
    case '-': total -= value; break;
    case '*': total *= value; break;
    case '/':
      if (value == 0) {
        cerr << "Divide by 0 prevented!\n";
      } else {
        total /= value; break;
      }
    default:
      cerr << "Unknown sign: " << sign << "\n";
    }

  } while (true);

  return 0;
}

要點:

  • 盡可能避免縮進,這會影響可讀性(建議繼續/中斷)
  • 盡可能使用一個開關( char和數字都可以)
  • 必要時檢查錯誤:從用戶輸入到double的轉換可能失敗,可能輸入0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM