如何在C ++中“重置”一个函数?

时间:2018-01-05 13:33:09

标签: c++14

    #include <iostream>

double getUserInput()
{
    std::cout << "Input a number: ";
    double value;
    std::cin >> value;
    return value;
}

int getMathOp()
{
    std::cout << "Input one of the following math operators: +, -, * or / : ";
    char op;
    std::cin >> op;
    return op;
}

int printReuslt(double x, char op, double y)
{
    if (op == '+')
        std::cout << x << " + " << y << " is " << x + y << '\n';
    else if (op == '-')
        std::cout << x << " - " << y << " is " << x - y << '\n';
    else if (op == '*')
        std::cout << x << " * " << y << " is " << x * y << '\n';
    else if (op == '/')
        std::cout << x << " / " << y << " is " << x / y << '\n';
    else 
        std::cout << op << " is not a valid operator. An error has occurred";
}

int main()
{
    double x = getUserInput();
    double y = getUserInput();

    char op = getMathOp();

    printReuslt(x, op, y);

    return 0;
}

我正在构建一个非常简单的计算器。

  1. getUserInput() - 从用户获取值(数字) 将其存储为名为“value”的双变量
  2. getMathOp() - 取一个值(一个数学运算符:+, - ,*或 /)来自用户并将其存储为名为'op'
  3. 的char变量
  4. printResult() - 计算结果 对于x op y的每个实例(其为:x + y,x-y,x * y,x / y)。 然后打印出结果。
  5. main() - 以正确的顺序将每个功能拉到一起。
  6. 我想添加另外两项功能,执行以下操作:

    1. 如果用户输入的值不是double,程序将会 输出一条消息,告诉用户“值不是数字”和 将使用户输入一个双倍的值,以便程序 可以继续
    2. 如果用户输入的数学运算符是 不在消息“+, - ,*或/”中,程序将输出一个 消息,告诉用户“操作员不是有效的操作员”和 将使用户输入指定的运算符,以便程序可以 继续。
    3. 对于第一个功能,我通过简单地将功能“重置”到第一行来设想它 - 让用户再次输入一个数字。 我会通过一个'if'语句来实现这一点,该语句将输入值设置为不是double的值,它将执行一条消息“value is not number”。然后该函数将重置为第一行并忽略先前的输入。

      对于第二部分功能,我在'printResult'中包含一个'else'语句,使输入值成为未指定为执行消息的数学运算符的东西,这有点开头。运算符不是有效的运算符。发生错误“。在此之后,程序结束。 我想要输出该消息,然后将'printResult'函数重置为第一行(忽略先前的输入),而不是在消息输出后结束程序。

      我的问题是我如何做我想要的事情(将函数放在第一行,忽略先前的输入,以便用户可以输入新值)以及将此功能实现到我的最佳方式是什么码? 此外,我希望对我想要添加的预期功能进行任何改进。

      由于

1 个答案:

答案 0 :(得分:0)

使用某种形式的循环和一系列操作,如:

  • 读取输入
  • 验证输入
  • 做工作,但只有全部输入都没问题

另外要注意确保在成功完成工作后#34;循环的下一次运行再次读取所有输入,即使它们在作业之前是正常的,否则它将在第一次成功的任务之后继续过时的输入。

即。

之类的东西
double x, y;
char op;

// make sure that first iteration will read everything
bool x_OK=false, y_OK=false, op_OK=false;

while(true)
{
    // only re-read parts that were broken
    // or, if you want to re-read ALL not just those broken, just remove IFs
    if(!x_OK) x = getX();
    if(!y_OK) y = getY();
    if(!op_OK) op = getOP();

    // ensure that we know what's broken
    x_OK = checkNumber(x);
    y_OK = checkNumber(y);
    op_OK= checkOp(op);

    if(!x_Ok) // error message..
    if(!y_Ok) // error message..
    if(!op_Ok) // error message..

    // ready to go?
    if( x_OK&&y_OK&&op_OK )
    {
       printReuslt(x, op, y);
       x_OK = y_OK = op_OK = false; // clear to force next iteration to read all
    }
    // else - nothing! loop will loop and will start again
    // but now with DIFFERENT states of x/y/op/OK variables
    // so next iteration of the loop will probably behave
    // in a different way to previous
}

这当然是绝对天真的草图,没有处理/清除CIN错误等。很少有功能也缺失,但你应该从他们的名字中得到这个想法。此外,添加一些智能退出条件而不是while-true是一个好主意。

相关问题