如何从标准输入流解析用户输入?

发布于 2025-01-01 15:31:42 字数 305 浏览 5 评论 0原文

我正在编写一个非常简单的程序,我想从标准输入流(键盘)获取用户输入,然后根据我遇到的输入执行某些操作。然而,问题是有时输入是数字(双精度),而有时输入是字符串。我不确定我需要调用什么方法才能正确解析它(可能类似于java中的 Integer.parseInt )。

这是我想做的一些伪代码:

cin >> input
if(input is equal to "p") call methodA;
else if(input is a number) call methodB;
else call methodC;

I'm writing a very simple program where I want to get user input from the standard input stream (keyboard) and then do something based on what input I encountered. However, the problem is that sometimes the input will be a number (double) while othertimes it'll be a string. I'm not sure exactly what methods calls I need in order to parse it properly (perhaps something similar to Integer.parseInt in java).

Here is some pseduocode of what I would like to do:

cin >> input
if(input is equal to "p") call methodA;
else if(input is a number) call methodB;
else call methodC;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幻想少年梦 2025-01-08 15:31:42

我认为这就是您所需要的:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void a(string& s){ cout << "A " << s << endl; }
void b(double d){ cout << "B " << d << endl; }
void c(string& s){ cout << "C " << s << endl; }

int main()
{
    std::string input;
    cin >> input;
    if (input == "p")
        a(input);
    else
    {
        istringstream is;
        is.str(input);
        double d = 0;
        is >> d;
        if (d != 0)
            b(d);
        else
            c(input);
    }
    return 0;
}

希望这会有所帮助;)

I think this is what you need:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void a(string& s){ cout << "A " << s << endl; }
void b(double d){ cout << "B " << d << endl; }
void c(string& s){ cout << "C " << s << endl; }

int main()
{
    std::string input;
    cin >> input;
    if (input == "p")
        a(input);
    else
    {
        istringstream is;
        is.str(input);
        double d = 0;
        is >> d;
        if (d != 0)
            b(d);
        else
            c(input);
    }
    return 0;
}

Hope this helps ;)

下雨或天晴 2025-01-08 15:31:42
std::string input;
std::cin >> input;
if(input =="p") f();
else if(is_number(input)) g();
else h();

现在实现 is_number() 函数:

bool is_number(std::string const & s)
{
  //if all the characters in s, are digits, then return true;
  //else if all the characters, except one, in s are digits, and there is exactly one dot, then return true;
  //else return false
}

自己实现这个函数,因为它看起来像是家庭作业。您还可以考虑数字可能以符号 +- 开头的情况。

std::string input;
std::cin >> input;
if(input =="p") f();
else if(is_number(input)) g();
else h();

Now implement is_number() function:

bool is_number(std::string const & s)
{
  //if all the characters in s, are digits, then return true;
  //else if all the characters, except one, in s are digits, and there is exactly one dot, then return true;
  //else return false
}

Implement this function yourself, as it seems to be homework. You can also consider case like the number may begin with sign + or -.

小巷里的女流氓 2025-01-08 15:31:42

我通常使用的解决方案是将输入读取为一行(使用
std::getline 而不是 >>),并像我在任何情况下一样解析它
语言——boost::regex在这里非常有用;如果你确定
你可以依靠 C++11,它是 std::regex (我认为这几乎是
与 Boost 相同)。所以你最终会得到类似的结果:

std::string line;
if ( ! std::getline( std::cin, line ) ) {
    //   Error reading line (maybe EOF).
} else {
    if ( regex_match( line, firstFormat) ) {
        processFirstFormat( line );
    } else if ( regex_match( line, secondFormat) ) {
        processSecondFormat( line ) ;
    } ...
}

The usual solution I use is to read the input as a line (using
std::getline rather than >>), and parse it as I would in any
language—boost::regex is very useful here; if you are sure that
you can count on C++11, it's std::regex (which I think is almost
identical to Boost). So you end up with something like:

std::string line;
if ( ! std::getline( std::cin, line ) ) {
    //   Error reading line (maybe EOF).
} else {
    if ( regex_match( line, firstFormat) ) {
        processFirstFormat( line );
    } else if ( regex_match( line, secondFormat) ) {
        processSecondFormat( line ) ;
    } ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文