0

For a project I need to implement a solution to the graph coloring problem. However, the input is required to have specific syntax with which I have no idea on how to parse through in order to access the data needed to store in variables.

The input constraints are to first enter the number of colors, then the number of vertices, followed by a sequence of edges. The edges should be entered in the format (v1 v2). The sequence is terminated with v1 = -1. So, (-1 0), (-1 -1), etc.

So the input will end up looking something along the lines of:

2 4 (0 1)(1 2)(2 3)(3 0)(-1 -1)

Any help would be greatly appreciated, as I have no idea where to even begin! I know there are similar questions here, however I can't figure out how to apply their solutions for this specific implementation.

5
  • 1
    If this is homework, you should add the homework tag or at least say so in your post. Commented May 6, 2012 at 0:07
  • And why did you tag this syntactic-sugar? Commented May 6, 2012 at 0:08
  • 1
    @jedwards I edited the tag out. As far as I can tell, the tag makes no sense on this question. Commented May 6, 2012 at 0:12
  • @jedwards syntactic-sugar because its only purpose is to make the input more readable. Commented May 6, 2012 at 0:15
  • 1
    @jmd Parsing input is not syntactic sugar. Syntactic sugar is making the code more readable, not the input. (Well, the term could be applied to certain input formats, but considering you're stuck with a specific input format, it does not apply here.) Commented May 6, 2012 at 0:18

2 Answers 2

2

Try something like this:

#include <iostream>

static inline int error(int n) { std::cerr << "Input error!\n"; return n; }

int main()
{
    int nc, nv;  // number of colours and vertices

    if (!(std::cin >> nc >> nv)) { return error(1); }

    for (int i = 0; i != nv; ++i)
    {
        char lb, rb;
        int v1, v2;
        if (!(std::cin >> lb >> v1 >> v2 >> rb) || lb != '(' || rb != ')') { return error(1); }

        std::cout << "We have a pair [" << v1 << ", " << v2 << "]\n";
    }
}

Note the key principle of input processing: All input operations appear inside a conditional context. As @jedwards says, the input can be any std::istream, such as a string stream or a file stream, or as in my example std::cin.

Sign up to request clarification or add additional context in comments.

1 Comment

Just pointing out that you could swap std::cin with a std::stringstream or std::ifstream in case you don't want to have to type the same input every time.
0

You might want to look into the standard input/output. Basically, ask for the user to enter the input, and get it like this: std::string mystr; std::getline(con, mystr); and then parse it using the >> operator and std::stringstream. You can skip spaces or parenthesis by just storing it in a char. So, to get the first two numbers, do: int colors, verts; char c; stringstream(mystr) >> colors >> c >> verts; You can then expand this for the rest of your input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.