2012-12-02 5 views
1

사용자가 입력 한 문자열을 비교하고 사용자가 알지 못하는 문자를 남길 수있는 기능을 사용해야하는이 프로그램을 작업하고 있습니다. 입력을 *로 바꿉니다. 입력은 6 자 (예 : ABC123) 인 자동차의 면허 번호판을 나타내며 사용자는 그 문자 중 임의의 문자를 빼낼 수 있습니다 (예 : AB ** 23 또는 ** C12 * 등). 따라서 함수는 올바른 위치에있는 문자와 일치하는 모든 객체를 반환해야하지만, 예를 들어 A가 올바른 위치에 있지만 다른 문자는 그렇지 않으면 반환 할 수 없습니다. 그러나 사용자는 예를 들어 A * * * * * 만 입력 할 수 있으며 함수는 첫 번째 위치에 A가있는 모든 객체를 반환해야합니다. 내가 한 것은 입력 문자열에서 모든 별표를 제거한 다음 하위 문자열을 만들어 함수로 벡터로 보냅니다.문자열의 특정 요소를 비교해야합니다.

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); 
    } 

벡터를받는 클래스의 기능은 현재 다음과 같은 :

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; 
      } 
     } 
    } 

사람이 내가 뭘하려는거야 이해하고 몇 가지 아이디어가있을 경우 , 언제든지 회신 해 주시면 감사하겠습니다. 이/A를 읽어 주셔서 감사합니다.

답변

0

한 번에 하나씩 비교하면서 문자를 반복합니다. 두 문자 중 하나가 별표 인 경우 해당 문자를 별표로 간주하고 그렇지 않으면 평등을 비교하십시오. 예 :

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; 
} 

실제로 6 자로 제한 할 필요가 없습니다. 세면대를 허용 할 수 있습니다. 이 경우 두 문자열의 길이가 같고 6 문자를 하드 코딩하지 말고 모든 문자 위치를 반복합니다.

+0

그게 바로 제가 뭘 찾고 있었는지, 문제가 해결되었습니다. 고마워. –

관련 문제