게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
(C 언어 허프만) ffulsh(stdin) 에러 질문
게시물ID : programmer_16490짧은주소 복사하기
작성자 : 등등등을
추천 : 0
조회수 : 371회
댓글수 : 1개
등록시간 : 2016/04/03 15:56:43
옵션
  • 베스트금지
  • 본인삭제금지
 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct huffTable{
char ch;
int count;
char code[20];
double frequency;
}HT[128];
typedef struct Tree{
double val;
char ch;
char dad_val;
struct Tree *left, *right, *dad;
}Tree;
typedef struct min{
int index;
float val;
}min;
int exception[100], exception_count = 0;
int HT_SIZE = 0;
void init(){
for(int i = 0; i < 128; i ++){
HT[i].ch ='\0';
HT[i].count = 0;
for(int a = 0; a < 20; a ++)
HT[i].code[a] = '\0';
}
}
int chk_HT(char c){
for(int i = 0; i < HT_SIZE; i ++){
if(HT[i].ch == c)
return i;
}
return -1;
}
void sort_(){
struct huffTable tmp ;
for(int i = 0; i < HT_SIZE-1; i++){
for(int j = i+1; j < HT_SIZE; j++)
if(HT[i].count > HT[j].count){
tmp = HT[j];
HT[j] = HT[i];
HT[i] = tmp;
}
}
}
int chk_exception(int index){
for(int z = 0; z < exception_count; z++){
if(index == exception[z])
return -1;
}
return 0;
}
void mk(){
int chk_rt = -1;
int rt = HT_SIZE;
min min[2];
int size = ((HT_SIZE - 1) * 2);
int break_chk = -1;
char code_tmp[20];
Tree node[size];
for(int i = 0; i < size; i++){
node[i].val = HT[i].frequency;
node[i].ch = HT[i].ch;
node[i].left = NULL;
node[i].right = NULL;
node[i].dad = NULL;
node[i].dad_val = '\0';
}
fflush(stdin);

if(node[rt].left == NULL && node[rt].right == NULL){
node[rt].left = &node[0];
node[rt].right = &node[1];
node[rt].val = node[0].val + node[1].val;
node[0].dad = &node[rt];
node[1].dad = &node[rt];
node[0].dad_val = '0';
node[1].dad_val = '1';
printf("\nFIRST RT ! \nch = %c val = %f left char : %c %f code : %c right char : %c %f code : %c\n",node[rt].ch,node[rt].val,node[rt].left->ch,node[rt].left->val, node[rt].left->dad_val,node[rt].right->ch,node[rt].right->val,node[rt].right->dad_val);
exception[exception_count++] = 0;
exception[exception_count++] = 1;
rt++;
}
for(int i = 1; i < (size- HT_SIZE)+1 ; i++){
int min_count = 0;
fflush(stdin);
for(int i = 0; i <2; i++){
min[i].val = 1;
min[i].index = -1;
}
for(int x = 0; x < 2; x++){
for(int z = 0; z < rt; z++){
if(chk_exception(z) == -1)
continue;
if(min[0].index == z)
continue;
if(min[min_count].val > node[z].val){
min[min_count].index = z;
min[min_count].val = node[z].val;
}
}min_count = 1;
}
exception[exception_count++] = min[0].index;
exception[exception_count++] = min[1].index;
node[rt].val = node[min[0].index].val + node[min[1].index].val;
node[rt].left = &node[min[0].index];
node[rt].right = &node[min[1].index];
node[min[0].index].dad = &node[rt];
node[min[1].index].dad = &node[rt];
node[min[0].index].dad_val = '0';
node[min[1].index].dad_val = '1';
printf("\n%d RT ! \nch = %c val = %f left char : %c %f code : %c right char : %c %f code : %c\n",i+1,node[rt].ch,node[rt].val,node[rt].left->ch,node[rt].left->val, node[rt].left->dad_val,node[rt].right->ch,node[rt].right->val,node[rt].right->dad_val);
rt++;
fflush(stdin);
}
fflush(stdin);
fflush(stdin);
fflush(stdin);
for(int g = 0; g < HT_SIZE; g++){
for(int z = 0; z < 20; z ++){
code_tmp[z] = '\0';
}
for(int index = 0; node[g].dad_val != '\0'; index++){
code_tmp[index] = node[g].dad_val;
node[g] = *node[g].dad;
}
for(int z = 0; z <= strlen(code_tmp); z++){
HT[g].code[z] = code_tmp[strlen(code_tmp) -1 - z];
}
HT[g].code[strlen(HT[g].code)-1] = '\0';
fflush(stdin);
}
}
int main(int ar, char *argv[]){
int len, totla_len = 0;
float fre = 0;
char text[100];
Tree *Tre = NULL;
init();
if(ar != 2){
fprintf(stderr,"how to using huffman....\n(huffman_exeFile txtFile)");
return -1;
}
FILE *f = fopen(argv[1],"r");
while(fgets(text,100,f) != NULL){
len = strlen(text);
totla_len += len;
if(HT[0].ch == '\0'){
HT[0].ch = text[0];
HT[0].count++;
HT_SIZE++;
}
for(int i = 1; i < len; i ++){
int chk = 0;
chk = chk_HT(text[i]);
if(chk != -1){
HT[chk].count++;
}else{
HT[HT_SIZE].ch = text[i];
HT[HT_SIZE].count++;
HT_SIZE++;
}
}
}
printf("TOTAL LEN : %d\n",totla_len);
for(int i = 0; i <HT_SIZE; i++){
fre += HT[i].frequency = ((float)HT[i].count / (float)(totla_len-1));
}
sort_();
mk();
printf("\n\n");
for(int i = 0; i < HT_SIZE; i++){
printf("%c %d %f code :%s\n",HT[i].ch,HT[i].count,HT[i].frequency,HT[i].code);
}
printf("frequence : %f \n",fre);
printf("\nHT_SIZE : %d\n",HT_SIZE);
fclose(f);
return 0;
}
위에 fflush(stdin)함수를 하나라도 빼버리면 세그먼테이션 폴트 11이 나오고 다 넣고 컴파일하면 정상적인 값이 나옵니다...
fflush자체가 버퍼를 비워주는 함수인데 왜 하나라도 빼면 에러가 나는거죠..?
제목 없음.png
제목 없음1.png



전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호