我怎样才能完成这项工作,有没有办法优化它?
How can I make this work and is there a way to optimize this?
我想用c++做一个计算器,又不想我的计算器只能做2个数的计算,所以我“做了”2个运算符计算器。另外,我想知道是否有一种方法可以不用两个 switch 语句来使机器和程序员更容易阅读。
#include <iostream>
int top1 ;
int main()
{
char operatr1, operatr2;
float num1, num2, num3, num4 ;
std::cout << "Please choose the first operator ( +, -, *, /):";
std::cin >> operatr1 ;
std::cout << "Please choose the second operator ( +, -, *, /):";
std::cin >> operatr2;
std::cout << "Please enter three numbers: " << std::endl;
std::cin >> num1 >> num2 >> num3;
switch (operatr1) {
case '+':
int top1{ num1 + num2 };
break;
case '-':
int top1{ num1 - num2 };
break;
case '*':
int top1{ num1 * num2 };
break;
case '/':
int top1{ num1 / num2 };
break;
default:
std::cout << "Error! The operator is not correct" << std::endl << "The operators are ( +, -, *, / ).";
break;
}
switch (operatr2) {
case '+':
int top2{ top1 + num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '-':
int top2{ top1 - num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '*':
int top2{ top1 * num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '/':
int top2{ top1 / num3 };
std::cout << "The answer is:" << " " << top2;
break;
default:
std::cout << "Error! The operator is not correct" << std::endl << "The operators are ( +, -, *, / ).";
break;
}
}
在 switch 语句外定义 top1
。您定义的全局 top1
没有您期望的值,因为您在每个 case
语句中定义了一个新的 top1
。您的编译器应该就此警告您(实际上,我认为在这种情况下这应该是一个错误)。
此外,由于 num1
到 num4
是浮点数,因此您的 top1
和 top2
实际上也应该是 float
,而不是 int
s
实际上,从你的全局变量中删除 int top1
,然后在你的 main:
float top1 = 0;
switch ( operatr1 ) {
case '+':
top1 = num1 + num2;
break;
// ... continue with other cases here
}
然后,对于您的第二个 switch
语句,top1
将可见并具有正确的值。
你可以使用一个函数:
int Evaluate(const char operator,
const int num1,
const int num2)
{
bool is_valid_operator = true;
int result = 0;
switch (operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '/':
// Note: integer division.
result = num1 / num2;
break;
case '*':
result = num1 * num2;
break;
default:
result = 0;
is_valid_operator = false;
break;
}
if (is_valid_operator)
{
std::cout << "Result of " << num1 << " " << operator << " " << num2 << "\n";
std::cout << result << "\n";
}
return result;
}
您的主要内容将简化为:
//...
int top1 = Evaluate(operatr1, num1, num2);
int top2 = Evaluate(operatr2, top1, num3);
我想用c++做一个计算器,又不想我的计算器只能做2个数的计算,所以我“做了”2个运算符计算器。另外,我想知道是否有一种方法可以不用两个 switch 语句来使机器和程序员更容易阅读。
#include <iostream>
int top1 ;
int main()
{
char operatr1, operatr2;
float num1, num2, num3, num4 ;
std::cout << "Please choose the first operator ( +, -, *, /):";
std::cin >> operatr1 ;
std::cout << "Please choose the second operator ( +, -, *, /):";
std::cin >> operatr2;
std::cout << "Please enter three numbers: " << std::endl;
std::cin >> num1 >> num2 >> num3;
switch (operatr1) {
case '+':
int top1{ num1 + num2 };
break;
case '-':
int top1{ num1 - num2 };
break;
case '*':
int top1{ num1 * num2 };
break;
case '/':
int top1{ num1 / num2 };
break;
default:
std::cout << "Error! The operator is not correct" << std::endl << "The operators are ( +, -, *, / ).";
break;
}
switch (operatr2) {
case '+':
int top2{ top1 + num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '-':
int top2{ top1 - num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '*':
int top2{ top1 * num3 };
std::cout << "The answer is:" << " " << top2;
break;
case '/':
int top2{ top1 / num3 };
std::cout << "The answer is:" << " " << top2;
break;
default:
std::cout << "Error! The operator is not correct" << std::endl << "The operators are ( +, -, *, / ).";
break;
}
}
在 switch 语句外定义 top1
。您定义的全局 top1
没有您期望的值,因为您在每个 case
语句中定义了一个新的 top1
。您的编译器应该就此警告您(实际上,我认为在这种情况下这应该是一个错误)。
此外,由于 num1
到 num4
是浮点数,因此您的 top1
和 top2
实际上也应该是 float
,而不是 int
s
实际上,从你的全局变量中删除 int top1
,然后在你的 main:
float top1 = 0;
switch ( operatr1 ) {
case '+':
top1 = num1 + num2;
break;
// ... continue with other cases here
}
然后,对于您的第二个 switch
语句,top1
将可见并具有正确的值。
你可以使用一个函数:
int Evaluate(const char operator,
const int num1,
const int num2)
{
bool is_valid_operator = true;
int result = 0;
switch (operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '/':
// Note: integer division.
result = num1 / num2;
break;
case '*':
result = num1 * num2;
break;
default:
result = 0;
is_valid_operator = false;
break;
}
if (is_valid_operator)
{
std::cout << "Result of " << num1 << " " << operator << " " << num2 << "\n";
std::cout << result << "\n";
}
return result;
}
您的主要内容将简化为:
//...
int top1 = Evaluate(operatr1, num1, num2);
int top2 = Evaluate(operatr2, top1, num3);