I am reading a file whick looks like following:
Code:
10 = NAND(1, 3)
11 = NAND(3, 6, 5)
15 = NAND(9, 6, 2, 8)
Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?
My code:
Code:
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))
{
std::size_t guard_found = line.find(guard_str);
if (guard_found ==std::string::npos)
{
///NAND
std::size_t found_2 = line.find(nand_str);
if (found_2!=std::string::npos)
{
vector <string> nand_inputs;
found_2 = line.find_first_of('(', found_2 + 1);
//find first input
string first_input = line.substr( found_2 + 1, ( line.find_first_of(',', found_2) - found_2 - 1) );
nand_inputs.push_back(first_input);
//Second input
found_2 = line.find_first_of(',', found_2 + 2);
string second_input = line.substr( found_2 + 2, ( line.find_first_of(')', found_2) - found_2 - 2) ); //CHANGED "found_2 + 1" TO "found_2 + 2" AND "found_2 - 1" TO "found_2 - 2"
nand_inputs.push_back(second_input);
cout<<"first: "<<first_input;
}
}
}