一个简单的加法、减法、乘法和除法程序 一个简单的C++计算器程序,计算加法、减法、乘法和加法……/P> #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; } #包括 #包括 使用名称空间std; int main() { //输入声明为total和counter的两倍 双倍合计=0,计数器=0; //输入声明符号和Q作为字符 字符符号,Q=0; //以双精度输入声明值 双重价值; //A do..while将永远循环(或直到我们到达break语句) 做 { //当前值为0。 不能包含 双结果=sqrt(值);
将一个简单的加法、减法、乘法和除法程序 一个简单的C++计算器程序,计算加法、减法、乘法和加法……/P> #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; } #包括 #包括 使用名称空间std; int main() { //输入声明为total和counter的两倍 双倍合计=0,计数器=0; //输入声明符号和Q作为字符 字符符号,Q=0; //以双精度输入声明值 双重价值; //A do..while将永远循环(或直到我们到达break语句) 做 { //当前值为0。 不能包含 双结果=sqrt(值);,c++,calculator,C++,Calculator,将if(sign!=“Q”)替换为if(sign==“Q”)中断; 这将修复许多可能的错误 这将通过使用更少的缩进和大括号使代码更具可读性 编辑:正如有人提到的,您可能也应该检查小写。(如果(sign='Q'| | sign='Q')。表示平方根, #包括 双r=sqrt(e); 关于您的其他问题:请更好地缩进代码;-) 你确定你没有忘记在这条线后面加一个方块吗? if(sign!=“Q”) 最简单的方法,在步骤模式中使用调试器,你会理解!作为一个简单的练习,我已经重构了你的程序,使它更简
if(sign!=“Q”)替换为if(sign==“Q”)中断;
- 这将修复许多可能的错误
- 这将通过使用更少的缩进和大括号使代码更具可读性
编辑:正如有人提到的,您可能也应该检查小写。(如果(sign='Q'| | sign='Q')。表示平方根,
#包括
双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;
}
#包括
#包括
使用名称空间std;
int main()
{
//输入声明为total和counter的两倍
双倍合计=0,计数器=0;
//输入声明符号和Q作为字符
字符符号=0;
//以双精度输入声明值
双重价值;
//A do..while将永远循环(或直到我们到达break语句)
做
{
//当前值为0。
CUT“上面的程序编码没有错误”,你按下“Q”还是“Q”。你的代码只检查“Q”,我按下“Q”,程序没有退出。谢谢方根代码。你说的是在行之后加块?什么意思?对不起,我只是初学者,所以我不熟悉所有的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 = 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;
}