0

In my code I'm trying to find "numbers", "identifiers", and "words". Numbers are defined as sequences of numbers that are separated by a letter , non-letter, or a non digit character( such as a space or /n).

Such as: 898A111 (This has two numbers) 898 111 (This also has two numbers)

Words are defined as a letter leading a sequence of numbers, letters, or both.

Such as: AJKALJ8923 or ALSJOIA or B93082092 (These are all considered words)

And Identifiers are the letters used to lead a word, or separate two numbers

Such as:

898A111 (The Identifier is A)
AJLKAKA (The Identifier is A)

I've been trying to scribble out possible solutions, and as far as checking words I believe I have a solution, but as far as counting and identifying both "numbers" and "identifiers" (in a string), I'm at a complete loss. Anyone have any ideas? Any help at all would be appreciated. I'd say my knowledge of C++ is at a beginner's level.

Main function:http://pastebin.com/MrXKLXYv
Header file: http://pastebin.com/Xn23zn7X
Assignment for reference if I was unclear: http://pastebin.com/2bgEPqbG

4

2 に答える 2

0

std::bitsetたぶん、この状況を処理するために使用できます。

#include <bitset>

std::string strInput = "898A111";
std::string strDigit = "0123456789";
std::bitset<255> bsDigit;
std::vector<std::string> vctDigit;
for (int i = 0; i < strDigit.length(); i++)
{
    bsDigit[strDigit.at(i)] = true;
}

int nTemp = 0;
int nLength = strInput.length();
for (int i = 0; i < nLength; i++)
{
    if (!bsDigit[strInput.at(i)])
    {
        vctDigit.push_back(strInput.substr(nTemp, i - nTemp));
        nTemp = i + 1;
    }
    else if (i == nLength - 1)
    {
        vctDigit.push_back(strInput.substr(nTemp, (i + 1) - nTemp));
    }
}

std::vector<std::string>::iterator it = vctDigit.begin();
for (; it != vctDigit.end(); it++)
{
    std::cout << (*it).c_str() << std::endl;
}
于 2013-07-18T02:56:07.550 に答える