一個簡單的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」退出,它將顯示最後的結果不間斷。 。一遍又一遍。 。 。無論如何,任何人都知道如何爲程序添加平方根。 。
「上述程序的編碼沒有錯誤」 – 2011-04-13 12:12:15
您是否按「q」或「Q」。您的代碼只檢查「Q」 –
2011-04-13 12:14:07
我按「Q」,程序沒有退出。 。 – Matrix 2011-04-13 13:29:36