使用命令行和C ++的简单计算器 [英] Simple calculator using command line with C++

查看:76
本文介绍了使用命令行和C ++的简单计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个项目,我们从命令行开始做简单的计算器。用户以这种格式输入程序名firstNumber运算符secondNumber 。这是到目前为止我得到的:

I'm writing a project that we do simple calculator from command line. The users input in this format programname firstNumber operator secondNumber. Here what I got so far:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main(int argc, char* argv[])
{
    cout << fixed << setprecision(2);
    if (argc != 4)
    {
        cerr << "Usage: " << argv[0] << " <number> <operator> <number>" << endl;
        exit(0);
    }
    else
    {
        double firstNumber = atoi(argv[1]);
        char theOperator = argv[2][0];
        double secondNumber = atoi(argv[3]);
        switch (theOperator)
        {
        case'+':
        {
                   cout << "The answer is " << firstNumber + secondNumber << endl;
                   break;
        }
        case '-':
        {
                    cout << "The answer is " << firstNumber - secondNumber << endl;
                    break;
        }
        case '*':
        {
                    cout << "The answer is " << firstNumber * secondNumber << endl;
                    break;
        }
        case '/':
        {
                    if (secondNumber == 0)
                    {
                        cout << "Can not devide by a ZERO" << endl;
                        break;
                    }
                    else
                    {
                        cout << "The answer is " << firstNumber / secondNumber << endl;
                        break;
                    }
        }
        }
    }
}

我想到一个问题,我的项目对十进制数字的计算错误。例如,如果我做2.5 + 1.25,结果为3。有人可以帮助我吗?我是C ++的新手

I came up with a problem that my project does the calculation wrong for decimal numbers. For example, if I do 2.5 + 1.25, the result is 3. Can anyone please help me? I'm new to C++

推荐答案

其他答案是正确的,但我将使用 strtod 而不是以Eris的名义使用atof,并且因为检查输入的正确性更加容易。

The other answers are correct, but I'm going to pitch using strtod instead of atof in the name of Eris and because it's easier to check the input for correctness.

char * endp;
double firstNumber;
if (argv[1][0] != '\0')
{ // input string is not empty 
    firstNumber = strtod (argv[1], &endp);
    if (*endp != '\0')
    { // number didn't end at the end of the string. String not a number.
        cout << "First number was not a number, dude." << endl;
        return 0;
    }
}
else
{
    cout << "Can't do much without a number, dude." << endl;
    return 0;
}

使用atof,可以使用我是模特并产生结果。不好的结果,但结果却完全一样。

With atof, the program can be called with "I'm the very model of a modern major-general" and produce results. Bad results, but results all the same.

哦,并警告在C ++中调用 exit 。该程序在那里停止死,并且没有调用析构函数。这里没有害处,但不是一个养成的好习惯。

Oh, and warning on calling exit in C++. The program stops dead right there, and no destructors are called. No harm here, but not a good habit to get into.

这篇关于使用命令行和C ++的简单计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆