본문 바로가기

Programming/C & C++

텍스트에서 입력 받은 단어의 개수 찾기

Header

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

class SearchWord
{
public:
    SearchWord();
    SearchWord( wstring, wstring );
    ~SearchWord();

private:
    vector<bool> vBool;
    wstring txt = L"";
    wstring word = L"";
    size_t szWord = 0;
    int nWordIndex = 0;
    int nTxtIndex = 0;
    int nSearchCount = 0;
    
private:
    bool check();

public:
    int isEqaul();            

public:
    int getCount(){        return this->nSearchCount;    }
};

CPP

#include "Test.h"
using namespace std;

SearchWord::SearchWord( wstring rWord, wstring rTxt ) 
: word( rWord ), txt( rTxt ), szWord( rWord.size() )
{
    for (int i = 0; i < szWord; i++)
    {
        vBool.push_back( false );
    }
}
SearchWord::~SearchWord(){}

bool SearchWord::check()
{
    for( int i = 0; i < szWord; i++ )
    {
        if (vBool.at( i ) == false)        return false;
    }
    return true;
}

int SearchWord::isEqaul()
{
    if ( nTxtIndex > txt.size() -1 ) return -1;
    
    if (this->txt.at( nTxtIndex ) == this->word.at( nWordIndex ))
    {
        vBool.at( nWordIndex ) = true;
        nTxtIndex++;
        
        if( nWordIndex >= szWord - 1)
        {
            if (check() == true) nSearchCount++;
            for (int i = 0; i < szWord; i++)
            {
                vBool.at( i ) = false;
            }
            nWordIndex = 0;
        }
        else nWordIndex++;
        isEqaul();
    }
    else
    {
        vBool.at( nWordIndex ) = false;
        nTxtIndex++;
        nWordIndex = 0;
        isEqaul();
    }
}

 

string::find 쓰면 편하긴 할텐데..