버블 정렬 활용법 질문좀 드립니다! 부탁드려요!
싸리
질문 제목 : 현재 지정된 txt파일을 매개로 받아서 그 내용에 있는 단어세기를 세어라 이런 거에요!단어 세기를 세면 예를들어 a : 1
apple : 3
이런식으로 나오게됩니다!질문 내용 : 지금 짜놓은 코드가있는데 다 짜놓았는데 ㅠㅠ 갑자기 버블정렬로 바꾸라고 하거든요...멘붕이와서;;
조금이나마 잘하시는 분들에게 여쭈어보려고합니다 ㅠ
여기에서 어떤 부분을 어떻게 바꾸어야 하는 건가요???!!!!
#include stdio.h
#include stdlib.h
#include string.h
#include ctype.h
#define maxword 1000 // 파일에 들어있는 단어의 최대 수
void initialize();
void read_file();
void print_words();
void deallocate();
void convert_lower(char *str);
int linear_search(char *key, int *found);
void move_downward(int index);
void insert_data(int index, char *str);
void print_words();
void bubble(char *words[] , int nwords)
struct wordcount{ // 단어를 저장할 구조체 선언
char *str; // 단어를 저장할 문자형 포인터
int count; // 단어가 나타난 횟수
};
struct wordcount *words; //단어를 저장할 배열(포인터로 선언)
int nwords; // 배열에 저장되어 있는 단어 수
void main()
{
initialize();
read_file();
print_words();
deallocate();
}
void initialize() // 기억장치 할당
{
words = (struct wordcount *)malloc(sizeof(struct wordcount)*maxword);
memset(words, 0 , sizeof(struct wordcount)*maxword);
nwords = 0; // 저장된 단어 수 =0
}
void deallocate() // 기억장치 해제
{
int n;
for(n=0; nnwords; n++) // 저장되어 있는 단어에 대하여
if(words[n].str != null) // 기억장소가 할당되었다면
{
free(words[n].str); // 단어를 저장한 기억장소를 해제한다.
free(words); // 구조체 배열을 해제한다.
}
free(words); // 구조체 배열을 해제 한다.
}
void read_file() // 파일 읽기 구현
{
char buffer[256]; // 한 개의 라인을 읽을 버퍼
char *token;
int found, index;
file *fp = fopen(applenews.txt, r); // 읽기 모드로 파일 열기
while(fgets(buffer,255,fp) != null) // 한줄을 읽었으면
{
token = strtok(buffer, ,.!?\t\n); // 첫번째 토큰 분리
while(token != null){
convert_lower(token); // 단어를 소문자로 변환한다.
index = linear_search(token, &found); // 단어가 존재하는지를 검사한다
if(found == 1) // 배열에 단어가 존재한다면
words[index].count += 1; // 단어 수를 증가시킨다.
else // 배열에 단어가 존재하지 않는다면
{
move_downward(index); // 단어를 한 칸씩 아래로 옮기고
insert_data(index, token); // 해당위치에 단어를 추가한다.
}
token = strtok(null, ,.!?\t\n); // 다음 단어를 분리한다.
}
}
fclose(fp); // 파일을 닫는다
}
void convert_lower(char *str)
{
while(*str != null) // 문자열의 끝이 아니면
{
*str = tolower(*str); // 소문자로 변환한다.
str++; // 다음 문자를 처리한다.
}
}
int linear_search(char *key, int *found)
{
int n, compare;
*found = 0; //찾지 못했다고 가정한다.
for(n=0; nnwords; n++) // 배열의 처음부터 배열의 단어 수 까지
{
compare = strcmp(key, words[n].str); // 문자열을 비교한다.
if(compare == 0) // 찾았다면
{
*found = 1; // 단어를 찾았다고 표시하고
break; // 루프를 벗어난다.
}
if(compare 0) // 배열의 단어가 키보다 크면
{
break; // 루프를 벗어난다.
}
}
return n; // 배열의 인덱스를 리턴한다.
}
void move_downward(int index)
{
int n;
if(nwords maxword-1) // 단어 수가 배열의 최대수를 초과하지 않으면
{
for(n = nwords; n=index; n--) // 마지막 단어부터
{
words[n+1].str = words[n].str; // 단어를 옮긴다.
words[n+1].count = words[n].count; // 단어 수를 옮긴다.
}
}
}
void insert_data(int index, char *str)
{
int size; // 단어의 문자 수
if(nwords maxword-1) // 단어 수가 배열의 최대 수를 초과하지 않으면
{
size = strlen(str) + 1; // 단어의 문자수를 구하고
words[index].str = (char *)malloc(size); // 기억장소를 할당하고
strcpy(words[index].str, str); // 단어를 복사한다.
words[index].count = 1; // 단어 횟수를 1로 설정한다.
nwords += 1; // 나타난 단어 수를 증가 시킨다.
}
}
void print_words()
{
int n;
for(n=0; nnwords; n++)
printf(%3d. %-18s: %d\n, n+1, words[n].srds[n].str, words[n].count);
}그리고요
void bubble(char *words[] , int nwords)
{
int i = 0;
int j = 0;
char tempstr[200];
for(i=0; inwords-1; i++)
{
for(j= i+1; jnwords; j++)
{
if(strcmp(words[i], words[j]) 0)
{
strcpy(tempstr, words[i]);
strcpy(words[i], words[j]);
strcpy(words[j] , tempstr);
}
}
}
}제가 짜본 버블정렬은 이건데.....이게 솔직히 맞는건지도 잘모르겠고 저 void bubble옆에 변수도
제대로 쓴건지도 잘 모르겠어요 ㅠㅠ
그리고 더욱 중요한건 이걸 어디에 어떻게 넣어서 활용해야 하는질 전혀 갈피를 못잡겠네요 ㅠㅠ
도움좀 부탁드립니다
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |
2699842 | 질문을 너무 많이 하네여.....죄송.... (2) | 해님꽃 | 2025-06-29 |