Separating input on forward slash in C++

2019-09-15 14:46发布

Let me preface this by saying I am really new to C++.

I want to grab the numerator and denominator from a fraction that gets entered in the form of A/B (in the command line); what is the easiest way to get A and B into their respective variables?

标签: c++ input
4条回答
闹够了就滚
2楼-- · 2019-09-15 14:50

The easiest approach using C++ is

double numerator, denominator;
char   dummy;
if (in >> numerator >> dummy >> denominator) {
    // ...
}

The above reads the rational value from a stream. To get a stream from an argument on the command line, you'd use an std::istringstream:

int main(int ac, char* av[]) {
    for (int i(1); i != ac; ++i) {
        std::istringstream in(av[i]);
        // ...
    }
}

The main drawback is that the separating character can be anything with the extraction code mentioned above. To fix this up I'd use a slash manipulator:

std::istream& slash(std::istream& in) {
    if (in.get() != '/') {
        in.setstate(std::ios_base::failbit);
    }
    return in;
}
// ...
if (in >> numerator >> slash >> denominator) {
    // ...
}

Since this solution seems to be appreciated, I want to point out that depending on needs you might want to slightly tweak the implementation of slash(): The posted version expects the slash right after the numerator. It may be reasonable to skip leading whitespace:

if ((in >> std::ws).get() != '/') {
    ...
}

Also, this is a special implementation for just one character and you might want to have similar manipulators for other characters. To avoid replicating code the manipulator can become a template:

template <char Separator>
std::istream& separator(std::istream& in) {
    if ((in >> std::ws).get() != std::char_traits<char>::to_int_type(Separator) {
        // ...
}
typedef std::istream& (*separator_manipulator)(std::istream&);
separator_manipulator const slash = &separator<'/'>;
separator_manipulator const comma = &separator<','>;
// ...

The need to use std::char_traits<char>::to_int_type() is there to avoid problems when using chars with a negative value.

查看更多
仙女界的扛把子
3楼-- · 2019-09-15 14:52

Command line arguments are passed as C strings (i.e, character pointers). If you have a basic framework of a program, you already have...

void main(int argc, char * argv[])
{ ... }

... or something like it. The first member of the argv array (i.e., argv[0]) is a character pointer to the name of the running program. The remaining arguments (argv[1] thru argv[argc-1]) are the command line arguments.

If you are just learning c/c++, then you should run a basic simple program under the debugger passing different arguments, quoting any arguments with spaces, using environment variables as arguments, and just experimenting to see what really gets passed in.

When you have this down, use your A/B as an argument. It will be a string and not a number, so you will need to divide at the "/".

int a, b;
scanf("%d/%d", &a, &b);

will work as long as your numbers and slash are all run together to make one argument. The shell will usually take spaces to delimit different arguments.

You can use other methods to find the slash and the value. The answer to your question does not have to be specific to c++. Scanf works for both c/c++. Another simple way is to...

char *copyOfString = strdup(argv[1]);
char *B = strchar(copyOfString, '/');
*B = NULL;
B++;
char *A = copyOfString;

Now you have the string form of the values that would need to be converted to integer (or float or double, depends on your requirement). Of course, there is no error checking, so if you don't call the program exactly right, programmatic chaos will ensue.

Others have posted other answers for other ways to do it, but I hope this helps and welcome to c/c++ programming. And please get a good book, trial and error only goes so far and C/C++ are very unforgiving.

查看更多
成全新的幸福
4楼-- · 2019-09-15 14:54

scanf. Even in C++, it still works. Note that variable names should start with lowercase.

int a, b;
scanf("%d/%d", &a, &b);
查看更多
等我变得足够好
5楼-- · 2019-09-15 15:04

-Removed-By Poster for not completing the answer with command line.

Split the string look up strtok once you read it in from std in like others have demonstrated.

查看更多
登录 后发表回答