我试图搜索文件计数没有。使用二进制搜索遇到的'C'保留字

问题描述 投票:0回答:1

我正在用c编写一个程序来搜索源代码文件并计算遇到的'C'保留字的数量。但只有输入的保留字是第一个字时才会打印保留字。并且它计算字符串的总数而不是使用的保留字的总数。有人可以帮我这个。我的代码太乱了,请不要介意。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define KEYMAX 32


FILE *fp ;
char data[1024];
struct keyword
{
    char word[10];
    int occur;
};
int i = 0, j = 0, pos;
char str[100], unit[20], ch;
int stored[1024];
char delimiters[] = " \t\n\v\f\r";  /* possible space delimiters */
char *token;
struct keyword key[32] = {"auto", 0, "break", 0, "case", 0,
                          "char", 0, "const", 0, "continue", 0,
                          "default", 0, "do", 0, "double", 0,
                          "else", 0, "enum", 0, "extern", 0,
                          "float", 0, "for", 0, "goto", 0,
                          "if", 0, "int", 0, "long", 0,
                          "register", 0, "return", 0, "short", 0,
                          "signed", 0, "sizeof", 0, "static", 0,
                          "struct", 0, "switch", 0, "typedef", 0,
                          "union", 0, "unsigned", 0, "void", 0,
                          "volatile", 0, "while", 0,};

int main()
{
    takeinput();
    system("CLS");
    theresult();
    // processresult();
    // ctoken();
return (0);

}

int takeinput()    // function to write in the file
{

  printf( "**********Welcome*************" ) ;
    fp = fopen("test.c", "w") ;   // Open file in write mode.
    if ( fp == NULL )
    {
        printf( "Could not open file test.c" ) ;  // Prints the statement if the file is not able to open.
        return 1;
    }
    printf( "\nPlease enter some text from keyboard to write in the file test.c \n\t" ) ;
    // getting input from user
    while ( strlen ( gets( data ) ) > 0 )
    {
        // writing in the file
        fputs(data, fp) ;           // Writes to file
        fputs("\n", fp) ;
    }

    // closing the file
    fclose(fp) ;
    return 0;
}

int theresult()
{

   fp = fopen("test.c", "r"); // read mode

   if (fp == NULL)
   {
      perror("Error while opening the file.\n");   // Prints the statement if the file is not able to open.
      return 1;
   }
   printf("The contents of test.c file are:\n");

 // To covert the ch into str
   int i= 0;
  //  printf("-----this is from ch----\n"); (Just for reference)
   while((ch = fgetc(fp)) != EOF)
   {
    str[i]=ch;
    i++;

   //  printf("%c",ch);   prints character

   }

   printf("%s",str);

   // printf("\n----This is from token-----\n");   (just for reference)
   for (token = strtok(str, delimiters); token != NULL;
         token = strtok(NULL, delimiters)) /* 'for loop' conditional part */
        /* prints token one per line */
       // puts(token);      // prints token

    for (i = 0; i < strlen(str); i++)
    {
        while (i < strlen(str) && str[i] != ' ' && isalpha(str[i]))
        {
            unit[j++] = tolower(str[i++]);
        }
        if (j != 0)
        {
            unit[j] = '\0';
            pos = binarysearch(unit, key);
            j = 0;
            if (pos != -1)
            {
               key[pos].occur++;
            }
        }
    }
    printf("***********************\n   Keyword\tCount\n***********************\n");
    for (i = 0; i < KEYMAX; i++)
    {
        if (key[i].occur)
        {
            printf("  %s\t  %d\n", key[i].word, key[i].occur);       // Prints the reserved keyword and its occurance
        }
    }

    fclose(fp);
   return (0);
}



int binarysearch(char *word, struct keyword key[])
{
    int low, high, mid;

    low = 0;
    high = KEYMAX - 1;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (strcmp(word, key[mid].word) < 0)
        {
            high = mid - 1;
        }
        else if (strcmp(word, key[mid].word) > 0)
        {
            low = mid + 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}

输入的字符串是:如果我中断请重新加入。浮漂


关键字计数


if            1 
break     1
Float     2
c arrays binary-search file-handling
1个回答
1
投票

错误在函数theresult中。在用于标记输入的for循环中,您处理并搜索整个输入str中的单词,而不是从token返回的单词strtok。在标记化之后,您不必检查空间(' '),因为空间是分隔符的一部分。

在将循环更改为:

   for (token = strtok(str, delimiters); token != NULL;
         token = strtok(NULL, delimiters)) /* 'for loop' conditional part */
        /* prints token one per line */
       // puts(token);      // prints token

        for (i = 0; i < strlen(token); i++)
        {
            while (i < strlen(token) && token[i] != ' ' && isalpha(token[i]))
            {
                unit[j++] = tolower(token[i++]);
            }
            if (j != 0)
            {
                unit[j] = '\0';
                pos = binarysearch(unit, key);
                j = 0;
                if (pos != -1)
                {
                   key[pos].occur++;
                }
            }
        }

输出是

The contents of test.c file are:
if i break please re-join it. float float
***********************
   Keyword      Count
***********************
  break   1
  float   2
  if      1

补充说明:

而不是将整个输入文件读入str我建议使用fgets和循环逐行读取和处理输入。

如果您希望用户输入输入文本,您可以直接处理输入行,而不是先将它们写入文件"test.c"然后再读取文件。

标记化后的while循环将切断第一个非alpha字符的所有内容。也许你实现了这个,因为它由于原始错误而无法正常工作。当令牌是"re-join"时,它将搜索"re"。您应该检查这是否是您想要的,并在必要时更改while循环。

© www.soinside.com 2019 - 2024. All rights reserved.