-1

好的,所以我实际上已经用 C++ 编程了很长一段时间了,但我目前对一些可能非常明显的事情感到困惑。为了好玩,我决定写一个基本的计算器。加法,减法,乘法,除法,一大堆。正如您在下面看到的,我有一个名为 selection 的 int 变量,用于查找 1、2、3 或 4。一旦选择,它将调用相应的函数。但是,我决定我希望能够随时键入“帮助”来显示帮助。我怎样才能做到这一点?我知道我可以简单地选择一个字符串,但我觉得这只会给问题贴上创可贴(对未来的问题没有帮助)。我想随时获得“帮助”。但是,使用另一个 if() 语句来捕获“帮助”

请帮助我,我确信这很简单,但由于某种原因我无法弄清楚!

#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;
}
4

4 回答 4

5

I would just change choice to a std::string

std::string   choice;
std::cin >> choice;

if (choice == "1")    { .... }
if (choice == "help") { .... }

But I would also change the if statements structure.
Rather than a list of if statements I would use a map. That maps the command to a function call.

#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();

}
于 2013-10-15T21:41:46.207 回答
1

我喜欢使用命令模式来处理这样的事情。(https://en.wikipedia.org/wiki/Command_pattern

基本上你有一个Command类/接口,例如:

abstract class Command
{
    virtual void run() = 0;
}

然后有不同的命令继承自:

class HelpCommand : Command
{
     virtual void run()
     {
         // do stuff
     }
}

然后在从用户获取命令的处理程序中,您将拥有一个stringto的映射Command。因此,当用户输入命令(例如“帮助”)时,它会从地图中获取适当的命令,然后调用其run()方法。

unordered_map< string, Command* > commands;
commands[ "help" ] = new HelpCommand();

// ...

// get input
commands[ input ]->run();
于 2013-10-15T22:18:03.713 回答
0

To do it the way you described, you'd first need to read your input into a stringm check if 'help' was entered and if not, if the input can be parsed as an integer. Then you'd parse the integer and call your other functions as normal.

于 2013-10-15T21:40:39.010 回答
0

如果您说在该程序中使用字符串就像在问题上贴了创可贴,并且可能会导致将来出现问题,那么为什么不制作另一个字符串变量,然后如果可行的话,稍后将其转换为数字。这是我为此目的修改的代码。

例子 :

#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.. : ";
}

请注意,出于效率和审美原因,我已经修改了您的一些代码;)

于 2018-03-20T07:46:08.943 回答