【问题标题】:Simple calculator with numeric values only C++ [duplicate]仅具有数值的简单计算器C ++ [重复]
【发布时间】:2015-05-08 05:45:52
【问题描述】:

我正在编写一个只计算数值的简单计算器。我需要为我的项目使用命令行参数。参数需要采用以下格式: 程序名 firstNumber 操作 secondNumber。我的项目工作正常,除了非数字值。例如,如果我做 abc - def,项目不会拒绝它,它会显示一个等于 0 的答案。我尝试为我的项目做正则表达式,但它不起作用。到目前为止我得到了什么:

#include <iostream>
#include <fstream>
#include <regex>
using namespace std;
double numValidation();
double result;
int main(int argc, char* argv[])
{
    if (argc != 4)
    {
        cerr << "Usage: " << argv[0] << " <number> <operator> <number>" << endl;
        exit(0);
    }
    else
    {
        double firstNumber = atof(argv[1]); 
        char theOperator = argv[2][0];
        double secondNumber = atof(argv[3]);
        switch (theOperator)
        {
        case'+':
            {
                result = firstNumber + secondNumber;
                cout << "The answer is " << result << endl;
                break;
            }
        case '-':
            {
                result = firstNumber - secondNumber;
                cout << "The answer is " << result << endl;
                break;
            }
        case '*':
            {
                result = firstNumber * secondNumber;
                cout << "The answer is " << result << endl;
                break;
            }
        case '/':
            {
                if (secondNumber == 0)
                {
                    cout << "Can not devide by a ZERO" << endl;
                    break;
                }
                else
                {
                    result = firstNumber / secondNumber;
                    cout << "The answer is " << result << endl;
                    break;
                }
            }
        }
    }
}
double numValidation(string input)
{
    regex value("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?");
    if (regex_match(input, value) && input.length() <= 10)
    {
        return result;
    }
    else
    {
        cout << "Input must be numeric value: ";
    }

}

我是 C++ 新手,我的导师从不向我们展示正则表达式。不确定正则表达式是否是仅使用数值进行计算的好方法。任何人都可以帮我修复我的正则表达式或建议我另一种方法来解决我的问题吗?谢谢!

【问题讨论】:

  • 为什么要在这里使用正则表达式?只需检查输入中的每个字符是否在有效字符范围内。
  • “不起作用”?你收到错误了吗?或者不是预期的输出?更具体。
  • @m.s.如果我使用正则表达式,非数字值的答案总是 0。
  • @shuttle87 你能说得更具体点吗?如果你能在代码上解释它会很好,我是 C++ 新手。对不起
  • 您上次为这个项目提出问题时的答案之一是:stackoverflow.com/a/30090381/3807729

标签: c++ regex command-line calculator


【解决方案1】:

您正在使用 atof 解析您的数字,这更难检查其中的错误。

您可能希望使用 errno + strtod,它允许进行错误处理。这是一个完整的工作示例程序。

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* *argv)
{
    char *err;
    double d = strtod( argv[1], &err );
    if(errno || *err != 0) {
        // errno checks out of range, err checks other errors like non-numeric
        printf("That's no double!\n");
        return 1;
    }
    printf("Value is %f", d );

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多