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];}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |