一个简单的加减乘除程序
一个简单的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”退出,它将不停地显示最后的结果。 .一遍又一遍,一遍又一遍。 。 。无论如何,任何人都知道如何在程序中添加平方根。 。
A simple C++ calculator program to calculate addition, subtraction, multiplication and addition...
#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;
}
The coding for the above program has no error but if i press "Q" to quit,it will display the last result non-stop. .Over and over and over and over again. . . Anyway,anyone know how to add square root to the program. .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
if (sign != 'Q') ...
替换为if (sign == 'Q') break;
编辑:正如有人提到的,你可能也应该检查小写。 (如果(符号=='Q'||符号=='q'))。
Replace
if (sign != 'Q') ...
byif (sign == 'Q') break;
EDIT: as someone mentionned, you should probably be checking lowercase too. (if (sign == 'Q' || sign == 'q')).
作为一个简单的练习,我重构了您的程序以使其更简单。我不保证它有效,但它应该为您提供一个良好的基础:
要点:
char
和数字)double
的转换可能会失败,可能会输入0
As a simple exercise I have refactored your program to make it simpler. I don't promise it works, but it should give you a nice foundation to build on:
The main points:
char
and numbers)double
could fail,0
might be entered对于平方根,
#include <数学>
双 r=sqrt(e);
关于您的其他问题:请更好地缩进您的代码;-)
您确定没有忘记在此行后面放置一个块吗?
if (sign != 'Q')
最简单的方法,在单步模式下使用调试器,您就会明白!
For square root,
#include <math>
double r=sqrt(e);
Concerning your other issue : please indent your code better ;-)
Are you sure you didn't forget to put a block after this line ?
if (sign != 'Q')
Simplest way, use a debugger in step mode and you'll understand!