1

Been working on this program which requires the use of a function that compares a string input by the user and gives the user the opportunity to leave the characters that he/she doesn't know out of the input, replacing them with * . The input represents a license-plate of a car that has 6 characters (for instance ABC123) and the user is allowed to leave any of those characters out (for instance AB** 23 or ** C12* etc.). So the function needs to return all objects that match the characters in the right position, but it cannot return if, say, A is in the right position but any of the other characters are not. The user is, however, allowed to only enter A* * * * *, for instance, and the function should return all objects that have A in the first position. What I did was use a function to remove all the asterisks from the input string, then create sub-strings and send them to the function as a vector.

    string removeAsterisk(string &rStr)// Function to remove asterisks from the string, if any.
    {
        stringstream strStream;
        string delimiters = "*";
        size_t current;
        size_t next = -1;
        do
    {
         current = next + 1;
         next = rStr.find_first_of( delimiters, current );
         strStream << rStr.substr( current, next - current ) << " ";
}
while (next != string::npos);

return strStream.str();

}

     int main()
    {
            string newLicensePlateIn;
            newLicensePlateIn = removeAsterisk(licensePlateIn);
            string buf; // Have a buffer string
            stringstream ss(newLicensePlateIn); // Insert the string into a stream

            vector<string> tokens; // Create vector to hold our words

            while (ss >> buf)
                tokens.push_back(buf);
            myRegister.showAllLicense(tokens); 
    }

The class function that receives the vector currently looks something like this:

    void VehicleRegister::showAllLicense(vector<string>& tokens)//NOT FUNCTIONAL
    {
        cout << "\nShowing all matching vehicles: " << endl;
        for (int i = 0; i < nrOfVehicles; i++)
        {

            if(tokens[i].compare(vehicles[i]->getLicensePlate()) == 0)
            {
                cout << vehicles[i]->toString() << endl;
            }
        }
    }

If anyone understand what I'm trying to do and might have some ideas, please feel free to reply, I would appreciate any advice. Thanks for reading this/ A.

4

1 に答える 1

0

Just iterate through the characters, comparing one at a time. If either character is an asterisk, consider that a match, otherwise compare them for equality. For example:

bool LicensePlateMatch(std::string const & lhs, std::string const & rhs)
{
    assert(lhs.size() == 6);
    assert(rhs.size() == 6);
    for (int i=0; i<6; ++i)
    {
        if (lhs[i] == '*' || rhs[i] == '*')
            continue;
        if (lhs[i] != rhs[i])
            return false;
    }
    return true;
}

Actually, you don't have to restrict it to 6 characters. You may want to allow for vanity plates. In that case, just ensure both strings have the same length, then iterate through all the character positions instead of hardcoding 6 in there.

于 2012-12-02T17:53:04.730 に答える