好吧,所以我实际上已经用C ++进行了一段时间的编程,但是我现在对那些可能非常显而易见的东西感到难过。我决定写一个基本的计算器以获得乐趣。加,减,乘,除,整数。正如你在下面看到的,我有一个名为choice的int变量,寻找1,2,3或4.一旦选择,它将调用相应的函数。但是,我决定我希望能够随时输入“帮助”来表示帮助。我怎样才能做到这一点?我知道我可以简单地选择一个字符串,但我觉得这只会在问题上设置一个绑定(对未来的问题没有帮助)。我想随时抓住“帮助”。但是,使用另一个if()语句来捕获“帮助”显然会给我一个错误 - 因为选择是一个int。
请帮助我,我相信这很简单,但由于某些原因我无法理解!
#include <iostream>
int firstnum;
int secondnum;
int multiplication(){
std::cout << "Multiplication chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum * secondnum << "." << std::endl;
}
int division(){
std::cout << "Division chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << std::endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum / secondnum << "." << std::endl;
}
int addition(){
std::cout << "Addition chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << std::endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum + secondnum << "." << std::endl;
}
int subtraction(){
std::cout << "Subtraction chosen. Please enter first number." << std::endl;
std::cin >> firstnum;
std::cout << "Please enter second number." << std::endl;
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum - secondnum << "." << std::endl;
}
int main(){
int choice;
std::cout << "Calculator." << std::endl;
std::cout << "Multiplication: 1. Division: 2. Addition: 3. Subtraction: 4. Help: help." << std::endl;
std::cin >> choice;
if(choice == 1){
multiplication();
}
if(choice == 2){
division();
}
if(choice == 3){
addition();
}
if(choice == 4){
subtraction();
}
////if the user types "help" it will show help.
return 0;
}
答案 0 :(得分:5)
我只想将选择更改为std :: string
std::string choice;
std::cin >> choice;
if (choice == "1") { .... }
if (choice == "help") { .... }
但我也会改变if语句结构
而不是if statements
的列表,我会使用地图。将命令映射到函数调用。
#include <iostream>
#include <map>
#include <functional>
int one()
{
std::cout << "one\n";
}
int two()
{
std::cout << "two\n";
}
int main()
{
std::map<std::string, std::function<int()> > action = {{"one", one}, {"two", two}};
auto act = action.find("one");
act->second();
}
答案 1 :(得分:1)
我喜欢使用命令模式这样的东西。 (https://en.wikipedia.org/wiki/Command_pattern)
基本上你有Command
类/接口,例如:
abstract class Command
{
virtual void run() = 0;
}
然后有不同的命令继承自:
class HelpCommand : Command
{
virtual void run()
{
// do stuff
}
}
然后在从用户获取命令的处理程序中,您将拥有string
到Command
的地图。因此,当用户输入命令(例如'help')时,它会从地图中获取相应的命令,然后调用其run()
方法。
unordered_map< string, Command* > commands;
commands[ "help" ] = new HelpCommand();
// ...
// get input
commands[ input ]->run();
答案 2 :(得分:0)
要按照您描述的方式进行操作,您首先需要将输入读入stringm检查是否输入了'help',如果没有,则输入可以解析为整数。然后你将解析整数并正常调用其他函数。
答案 3 :(得分:0)
如果你说通过在该程序中使用字符串就像在问题上放一个乐队助手一样,并且可能会导致未来的问题,为什么不制作另一个字符串变量,然后将其转换为数字如果它是#s可行的。 这是我为此目的而修改的编码。
示例:
#include <iostream>
#include <string>
#include <sstream.h>
int firstnum;
int secondnum;
void multiplication();
void division();
void addition();
void subtraction();
void help();
int main(){
int choice=NULL;
std::string input;
std::cout << "Calculator." << std::endl;
std::cout << "1. Multiplication: 2. Division: 3. Addition: 4. Subtraction: 5. Help: help." << std::endl;
std::cout << "Input : ";
std::cin >> input;
if ((input == "help")||(input == "Help")||(input == "HELP")) { choice=5; }
else {
istringstream intinput(input);
intinput>>choice;
}
switch(choice){
case 1: multiplication(); break;
case 2: division(); break;
case 3: addition(); break;
case 4: subtraction(); break;
case 5: help(); break;
}
////if the user types "help" it will show help.
return 0;
}
// BEGIN FUNCTIONS!--------------------------------------
void multiplication(){
std::cout << "Multiplication chosen. Please enter first number : ";
std::cin >> firstnum;
std::cout << "Please enter second number : ";
std::cin >> secondnum;
std::cout << "Your answer is : " << firstnum * secondnum << "." << std::endl;
}
void division(){
std::cout << "Division chosen. Please enter first number : ";
std::cin >> firstnum;
std::cout << "Please enter second number : ";
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum / secondnum << "." << std::endl;
}
void addition(){
std::cout << "Addition chosen. Please enter first number : ";
std::cin >> firstnum;
std::cout << "Please enter second number : ";
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum + secondnum << "." << std::endl;
}
void subtraction(){
std::cout << "Subtraction chosen. Please enter first number : ";
std::cin >> firstnum;
std::cout << "Please enter second number : ";
std::cin >> secondnum;
std::cout << "Your answer is: " << firstnum - secondnum << "." << std::endl;
}
void help(){
std::cout << "A simple help menu.. : ";
}
请注意,由于效率和美学原因,我修改了一些代码; )