c++ 질문드립니다...
가랑비
1번코드대신 2번코드를 사용하면 왜 더 간단한지 자세히 설명좀 부탁드리겠습니다ㅜㅜ=====================================================1번 코드=====================================================#include iostreamusing namespace std;
struct word{ char* str; // 각 단어의 내용 int count; // 각 단어의 사용 빈도};
int max_of_words = 10; // 전체 words 배열의 크기int num_of_words =0; //입력된 words의 개수word *words = null; //동적 메모리 배열의 주소
int findwords (const char *s);void increase () ;void countwords (char *s);voidshowword();void removeall();
int main (){words = new word[max_of_words];memset(words, 0 ,sizeof (word)*max_of_words);
cout 문자열을 입력하세요 . 종료하려면 ctrl +z을 입력하세요. \n;while(true){ char buffer[128];cin. getline(buffer, 128);countwords (buffer);if(cin.eof)())break;}showwords();removeall();
return 0;}
void countwords(char *s){const char * delimiter = ,.?!\t\n;char *token = strtok(s, delimiter);while(token != null){int index = findwords (token);if (index == -1){if (num_of_words = max _of_words)increase ();else{words[num_of_words].str = new char [strlen (token)+1];strcpy(words[num_of_words].str, token);words[num_of_words]. count=1;num_of_words++;}}else{words [index].count++;}token = strtok (null, delimiter);}}
int findwords(const char *s){for (int i = 0 ; i num_of_words ; i++)if (strcmp(words[i].str,s)==0)return i ;return -1; //단어 검색 실패}
void increase (){max_of_words += 10;
word *tmp = words;words = new word [max_of_words]; //증가된 크기로 배열 생성memcpy(words, tmp, sizeof (word)* num_of_words);delete [] tmp;}
void showwords(){for (int i=0; i num_of_words; i++)cout words [i].str : words[i]. count 번 사용 \n;}
void removeall(){for(int i = 0;i num_of_words ; i++)delete [] words [i].str;delete [] words;}
=====================================================2번 코드=====================================================#include iostream#include string#include vectorusing namespace std;
struct WORD{string str; // 각 단어의 내용int count; // 각 단어의 사용빈도};
vector WORD* words; // WORD 구조체 포인터를 저장하는 벡터 객체
int FindWords(const string& s); // 동일 문자열 검색void CountWords(const string& s); // 문자열 카운트void ShowWords(); // 해당 문자열을 보여줌void RemoveAll(); // 할당된 메모리 해제
int main(){cout 문자열을 입력하세요. 종료하려면 Ctrl+Z를 입력하세요.\n; string buffer;while( cin buffer ) CountWords(buffer);ShowWords () ;RemoveAll () ;
return 0;}
void CountWords(const string& s){int index = FindWords(s);if ( index == -1){ WORD *pWord = new WORD; // 구조체 포인터(동적 메모리 할당) pWord-str = s; pWord-count = 1; words.push_back(pWord); // 배열 원소 추가}else{ words[index]-count++;}}
int FindWords(const string& s) //같은 문자열이 있는지 검색{for(int i = 0 ; i words.size() ; i++) if ( words[i]-str == s) return i;return -1; // 동일한 단어 검색 실패}
void ShowWords() //해당 문자열을 보여줌{for(int i = 0 ; i words.size() ; i++) cout words[i]-str : words[i]-count 번\n;}
void RemoveAll () // 할당된 메모리 해제(반납){for(int i = 0 ; i words.size() ; i++) delete words[i];}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2694854 | 절대값을 구할때 (2) | 그녀는귀여웠다 | 2025-05-15 |
2694827 | 이제 어떻게 공부해야할지 모르겠네요 | 새얀 | 2025-05-14 |
2694778 | 순열 계산요. | 맛조이 | 2025-05-14 |
2694754 | ShowWindow 함수를 이용하려 하는데 질문있습니다. (2) | 파도 | 2025-05-14 |
2694731 | 리눅스 커널의 시작점 질문 | 미르 | 2025-05-13 |
2694702 | 이거 뭐가문제인가요 코드수정좀 (3) | 맑은 | 2025-05-13 |
2694675 | C언어 후위표기를 중위표기로 | 앨런 | 2025-05-13 |
2694646 | 안녕하세요 파일 합치기 함수! (1) | 연블루 | 2025-05-13 |
2694618 | 잘몰라서 설명부탁드립니다. scanf 관련 (3) | 파라 | 2025-05-12 |
2694590 | 이 코드가 뭐하는 코드일까요? #2 | 빵순 | 2025-05-12 |
2694559 | 동적할당으로 배열(2차원열)을 만드는데 있어 그걸 함수화시키는데... (1) | 늘솔길 | 2025-05-12 |
2694532 | 네트워크에 관하여... (4) | 황소자리 | 2025-05-12 |
2694503 | 프로그램 연산 후 바로 종료되는 현상 (6) | Judicious | 2025-05-11 |
2694450 | while문질문입니다. (1) | 허리품 | 2025-05-11 |
2694420 | C언어 질문할게요(유니코드,자료형,버퍼,캐스트연산자) | 은새 | 2025-05-11 |
2694370 | 내일까진데 함수호출 제발 도와주세요!!!!!!!!!11 | 들찬 | 2025-05-10 |
2694339 | putchar()의 괄호 안에 int c=10;로 전에 선언된 c를 넣으면 안되는 이유에서 제가 생각한 것이 그 이유가 되는지 확인하고 싶습니다. (3) | 미르 | 2025-05-10 |
2694316 | 이 코드 어디가 잘못되었는지 고수분들 ㅠㅠ (2) | 나빛 | 2025-05-10 |
2694285 | 언어 공부하는 과정 좀 추천해주세요! (1) | 아빠몬 | 2025-05-09 |
2694258 | 카운터.. 질문입니다. (4) | 하늘빛눈망울 | 2025-05-09 |