벌써 딜레이 이틀째 ㅠㅠ
왜 이게 segmentation fault가 나는 지 몰겠음 ㅠㅠㅠㅠ
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max_size 255
typedef struct linenode{
char data[400];
int special;
struct linenode *prev;
struct linenode *next;}
LINE;
typedef struct element{
LINE* word[500];
int NUMBER;
struct element * leftchild;
struct element * rightchild;
struct element* parent;
}ELEMENT;
ELEMENT* Queue[max_size];
int front=0,rear=0;
void Put(ELEMENT*);
ELEMENT* Get(void);
int isQueueEmpty(void);
void Put(ELEMENT * ptrNode)
{
Queue[rear]=ptrNode;
rear=(rear++)%max_size;
}
ELEMENT* Get(void)
{
ELEMENT *ptrNode;
if(!isQueueEmpty()){
ptrNode=Queue[front];
front=(front++)%max_size;
return ptrNode;}
else
return NULL;}
int isQueueEmpty(void){
if(rear==front)
return 1;
else
return 0;
}
ELEMENT* Search(ELEMENT* d, char* key){
ELEMENT* temp=d;
while(temp){
if(!strcmp(key,temp->word[0]->data))
return temp;
else if(strcmp(temp->word[0]->data,key))
temp=temp->leftchild;
else
temp=temp->rightchild;
}
return NULL;
}
void lineinsert(LINE*root,LINE*node){
int i;
LINE* indexptr=(LINE*)malloc(sizeof(LINE));
LINE* temp=(LINE*)malloc(sizeof(LINE));
indexptr=root;
if(root->special==-898){
strcpy(root->data,node->data);
root->special=1;}
else
while(indexptr!=NULL &&indexptr->next!=NULL){
indexptr=indexptr->next;}
strcpy(temp->data,node->data);
temp->prev=indexptr;
temp->next=indexptr->next;
indexptr->next=temp;
}
void wordinsert(ELEMENT* a,LINE* word,int i ){
if(a!=NULL)
a->word[i]=word;
}
void bstInsert(ELEMENT* d,LINE* word){
ELEMENT * temp=(ELEMENT*)malloc(sizeof(ELEMENT));
ELEMENT * temp2=(ELEMENT*)malloc(sizeof(ELEMENT));
ELEMENT * temp3=(ELEMENT*)malloc(sizeof(ELEMENT));
temp=d;
int i;
if(temp->NUMBER==-997){
wordinsert(temp,word,0);
temp->leftchild=NULL;
temp->rightchild=NULL;
temp->NUMBER=1;
}
while(temp){
if(strcmp(temp->word[0]->data,word->data)){
temp2=temp;
temp=temp->leftchild;
}
else if(strcmp(word->data,temp->word[0]->data)){
temp2=temp;
temp=temp->rightchild;
}
else if(!strcmp(word->data,temp->word[0]->data)){
temp2=temp;
break;
}
}
if(strcmp(word->data,temp2->word[0]->data)){
wordinsert(temp3,word,0);
temp3->NUMBER=1;
temp3->parent=temp2;
temp3->leftchild=NULL;
temp3->rightchild=NULL;
temp2->rightchild=temp3;
}
else if(strcmp(temp2->word[0]->data,word->data)){
wordinsert(temp3,word,0);
temp3->NUMBER=1;
temp3->parent=temp2;
temp3->leftchild=NULL;
temp3->rightchild=NULL;
temp2->leftchild=temp3;
}
else if(!strcmp(word->data,temp2->word[0]->data)){
if(temp2!=NULL)
wordinsert(temp2,word,temp2->NUMBER);
(temp2->NUMBER)++;
}
printf("%s",temp2->word[0]->data);
}
ELEMENT* bstDelete(ELEMENT* d, char* key){
ELEMENT * temp=(ELEMENT*)malloc(sizeof(ELEMENT));
ELEMENT * temp2=(ELEMENT*)malloc(sizeof(ELEMENT));
ELEMENT* temp3=(ELEMENT*)malloc(sizeof(ELEMENT));
temp=Search(d,key);
int i;
if(!temp->leftchild && !temp->rightchild &&temp->parent){
temp3->NUMBER=temp->NUMBER;
for(i=0;i<temp3->NUMBER;i++)
temp3->word[i]=temp->word[i];
temp->parent->leftchild=NULL;
temp->parent->rightchild=NULL;
free(temp);
}
else if(temp->rightchild!=NULL){
temp3->NUMBER=temp->NUMBER;
for(i=0;i<temp3->NUMBER;i++)
temp3->word[i]=temp->word[i];
temp2=temp->rightchild;
while(temp2->leftchild!=NULL)
temp2=temp2->leftchild;
temp->NUMBER=temp2->NUMBER;
for(i=0;i<temp->NUMBER;i++)
temp->word[i]=temp2->word[i];
if(temp2->rightchild!=NULL&&temp2->parent!=temp){
temp2->parent->leftchild=temp2->rightchild;
temp2->rightchild->parent=temp2->parent;
free(temp2);
}
else if(temp2->rightchild!=NULL && temp2->parent==temp){
temp2->parent->rightchild=temp2->rightchild;
temp2->rightchild->parent=temp2->parent;
free(temp2);
}}
else if(temp->leftchild!=NULL){
temp3->NUMBER=temp->NUMBER;
for(i=0;i<temp3->NUMBER;i++)
temp3->word[i]=temp->word[i];
temp2=temp->leftchild;
while(temp2->rightchild!=NULL)
temp2=temp2->rightchild;
temp->NUMBER==temp2->NUMBER;
for(i=0;i<temp->NUMBER;i++)
temp->word[i]=temp2->word[i];
if(temp2->leftchild!=NULL&&temp2->parent!=NULL){
temp2->parent->rightchild=temp2->leftchild;
temp2->leftchild->parent=temp2->parent;
free(temp2);}
else if(temp2->leftchild!=NULL && temp2->parent==temp){
temp2->parent->leftchild=temp2->leftchild;
temp2->leftchild->parent=temp2->parent;
free(temp2);
}}
return temp3;}
void wordchange(ELEMENT*d, char* wordsbefore,char*wordsafter){
ELEMENT* temp=(ELEMENT*)malloc(sizeof(ELEMENT));
int i;
temp=bstDelete(d,wordsbefore);
for(i=0;i<temp->NUMBER;i++){
strcpy(temp->word[i]->data,wordsafter);
bstInsert(d,temp->word[i]);
printf("%s",temp->word[i]->data);
}
}
void bstPrint(ELEMENT* d){
front=0;
rear=0;
ELEMENT*temp=(ELEMENT*)malloc(sizeof(ELEMENT));
temp=d;
int i=0;
if(temp!=NULL)
Put(temp);
while(!isQueueEmpty()){
temp=Get();
if(temp->leftchild!=NULL)
Put(temp->leftchild);
if(temp->rightchild!=NULL)
Put(temp->rightchild);}
}
int main(int argc,char*argv[]){
int i;
char s[30];
LINE* linesnode=(LINE*)malloc(sizeof(LINE));
LINE* tempword=(LINE*)malloc(sizeof(LINE));
linesnode->prev=NULL;
linesnode->next=NULL;
linesnode->special=-898;
char line[300];
char* temptok1;
char* temptok2;
char* temptok3;
char temp4[5];
char* temp5;
LINE* indexptr;
ELEMENT* wordtree=(ELEMENT*)malloc(sizeof(ELEMENT));
ELEMENT* indexptr2;
wordtree->leftchild=NULL;
wordtree->parent=NULL;
wordtree->rightchild=NULL;
wordtree->NUMBER=-997;
char temp6[30];
char temp7[30];
char temp8[30];
char* temp9;
char * temp10;
int j;
int pos;
FILE* fp=fopen(argv[1],"rt");
while(!feof(fp)){
fgets(line,sizeof(line),fp);
temptok1=line;
temptok1=strtok(temptok1," !@#$%^-&*,.?");
while(temptok1!=NULL){
strcpy(tempword->data,temptok1);
lineinsert(linesnode,tempword);
bstInsert(wordtree,tempword);
temptok1=strtok(NULL," !@#$%^-&*,.?");
}
}
fclose(fp);
while(temp6[0]!='Q'){
scanf("%s",temp6);
if(temp6[0]=='R'){
scanf("%s",temp7);
scanf("%s",temp8);
wordchange(wordtree,temp7,temp8);
}
}
fp=fopen(argv[2],"wt");
while(linesnode!=NULL){
fprintf(fp,linesnode->data);
linesnode=linesnode->next;
}
fclose(fp);
}
왜 저 상태에서 ./Editor abc.txt output.txt로 실행시킨뒤 R and who하면 에러가 나는 거 ㅠㅠ
아무리 답답해도 소스코드 다 올려놓으면 일단 보기 싫어진다.
ㅎㅎ 마지막 한 줄만 보임
하이라이트라도 씌워줘,,,
Search 함수에서 에러가 남 ㅠㅠ
소스코드 한줄한줄 따라가봤음?