Stack Kullanarak Dosya Yazma



Arkadaşlar aşağıdaki kod pc de mevcut olan html dosyalarını sırayla açıp okuyarak stack ta kaydediyor. Ardından bütün dosyalara içeren kelime araması yapmamıza imkan sağlıyor . Kelimenin hangi html dosyasında kaç kere geçtiğini ekrana bastırıyor. Kodda gördüğünüz hataları yorum olarak bana atarsanız beraber geliştirme yapabiliriz. Kolay gelsin umarım işinize yarar







struct node{
    char word[1000];
    int freq;
    struct node *left, *right;
};

struct node *rootIgnore = NULL;
struct node *file1 , *file2 , *file3 , *file4 , *file5 , *file6 , *file7 , *file8 , *file9 , *file10 = NULL;
int counter(int count);
void searchFiles(char * temp);
int search(struct node *root, char word[1000]);
struct node *newNode(char word[1000]);
struct node* insert(struct node* node, char word[1000]);
int search1(struct node *root, char word[1000]);

int main() {
    FILE *ptr_file;
    char temp[100];
   
    ptr_file =fopen("ignoreList.txt","r");
    if (!ptr_file){
        printf("IgnorList ");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){
        rootIgnore = insert(rootIgnore, temp);
    }

  int i;
 
  for(i=1;i<=10;i++){
     switch(i){
    case 1 :
  ptr_file =fopen( "cse22501.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){
     
        file1 = insert(file1, temp);
     
    }
  break;
  case 2 :
  ptr_file =fopen( "cse22502.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){

        file2 = insert(file2, temp);
     
    }
   break;
   case 3 :
  ptr_file =fopen( "cse22503.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){

        file3 = insert(file3, temp);
     
    }
   break;
   case 4:
  ptr_file =fopen( "cse22504.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){

        file4 = insert(file4, temp);
     
    }
  break;
   case 5 :
  ptr_file =fopen( "cse22505.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){
        file5 = insert(file5, temp);
     
    }
  break;
   case 6 :
  ptr_file =fopen( "cse22506.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){
        file6= insert(file6, temp);
     
    }
  break;
  case 7 :
  ptr_file =fopen( "cse22507.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){
        file7 = insert(file7, temp);
     
    }
  break;
case 8 :
  ptr_file =fopen( "cse22509.html","r");
  if (!ptr_file){
        printf("hata.");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){

        file8 = insert(file8, temp);   
    }
  break;
   case 9 :
  ptr_file =fopen( "cse22508.html","r");
  if (!ptr_file){
        printf("hata");
        return 1;
    }
   
    while (fscanf(ptr_file,"%s",temp)!=EOF){
        file9 = insert(file9, temp);
      }
  break;
   case 10 :
  ptr_file =fopen( "cse22510.html","r");
  if (!ptr_file){
        printf("hata");
        return 1;
    } 
    while (fscanf(ptr_file,"%s",temp)!=EOF){   
        file10 = insert(file10, temp); 
    }
  break;
}
   
  }
int a=1;
    while (a==1) {
        printf("Enter your query: ");
       char query[100];
    gets(query);
       
        char * pch;
        pch = strtok (query," ,.-");
        while (pch != NULL){
           
            searchFiles(pch);
            pch = strtok (NULL, " ,.-");
 
    printf("push 1 to continue");
    scanf("%d",&a);
        }
    } 
}

void searchFiles(char * temp){
    printf("Word: %s\n",temp);
  int a;
  for(a=1;a<=10;a++)
   switch(a){
case 1:
    if (search(file1, temp) != 0) {
        printf("file1 (%d)\t",search(file1, temp));
    }break;
 case 2: 
    if (search(file2, temp) != 0) {
        printf("file2 (%d)\t",search(file2, temp));
    }break;
 case 3: 
    if (search(file3, temp) != 0) {
        printf("file3 (%d)\t",search(file3, temp));
    }break;
  case 4: 
    if (search(file4, temp) != 0) {
        printf("file4 (%d)\t",search(file4, temp));
    }break;
  case 5: 
    if (search(file5, temp) != 0) {
        printf("file5 (%d)\t",search(file5, temp));
    }break;
  case 6: 
    if (search(file6, temp) != 0) {
        printf("file6 (%d)\t",search(file6, temp));
    }break;
   case 7:
    if (search(file7, temp) != 0) {
        printf("file7 (%d)\t",search(file7, temp));
    }break;
   case 8:
    if (search(file8, temp) != 0) {
        printf("file8 (%d)\t",search(file8, temp));
    }break;
  case 9 : 
    if (search(file9, temp) != 0) {
        printf("file9 (%d)\t",search(file9, temp));
    }break;
  case 10 : 
    if (search(file10, temp) != 0) {
        printf("file10 (%d)\t",search(file10, temp));
    }break;
}
    printf("\n");
}

struct node* insert(struct node* node, char word[1000]){
   
    if (node == NULL) return newNode(word);
   
    if (strcmp(word, node->word) < 0)
        node->left  = insert(node->left, word);
    else if (strcmp(word, node->word) > 0)
        node->right = insert(node->right, word);
   
    return node;
}

struct node *newNode(char word[1000]){
    struct node *temp =  (struct node *)malloc(sizeof(struct node));
    memcpy(temp->word, word, strlen(word)+1);
    temp->left = temp->right = NULL;
    temp->freq = 1;
    return temp;
}

int search(struct node *root, char word[1000]){

    if (root == NULL )
        return 0;
   
    if (strcmp(root->word, word) == 0){
           
  return   root->freq;   
    }
   
    if (strcmp(root->word, word) < 0)
        return search1(root->right, word);
    return search1(root->left, word);
}

int counter(int count){
int counter;
counter++;
return counter;
}
int count=0;
int search1(struct node *root, char word[1000]){

    if (root == NULL )
        return 0;
 
   if (strcmp(root->word, word) == 0){
       root->freq += 1;
    count=counter(count);
   
       return count;
    }
   
    if (strcmp(root->word, word) < 0)
        return search1(root->right, word);

    return search1(root->left, word); 
}

Yorumlar