【问题标题】:I am trying to search through the file counts the no. of ‘C’ reserved words encountered using binary search我正在尝试搜索文件计数。使用二进制搜索遇到的“C”保留字
【发布时间】:2019-04-29 10:43:50
【问题描述】:

我正在用 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

【问题讨论】:

  • 提供的示例对我来说看起来不错。我错过了什么?
  • 发布的代码无法编译!。除其他外,它缺少子功能所需的原型。建议在编译器上启用警告,然后修复这些警告。 (对于gcc,至少使用:-Wall -Wextra -Wconversion -pedantic -std=gnu11)注意:其他编译器使用不同的选项来产生相同的结果。使用列出的选项会导致编译器输出 15 条警告,其中许多非常严重
  • OT:为了便于阅读和理解:1) 一致缩进代码(没有随机的 2 个字符缩进)建议每个缩进级别为 4 个空格。
  • 遇到(例如)if时,后面可能有也可能没有空格,可能是left paren这样的细节需要考虑

标签: c arrays binary-search file-handling


【解决方案1】:

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

将循环更改为:

   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 循环将切断从第一个非字母字符开始的所有内容。也许您实现了这个,因为它由于原始错误而不起作用。当令牌为"re-join" 时,它将搜索"re"。您应该检查这是否是您想要的,并在必要时更改 while 循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2014-08-10
    • 2014-11-02
    • 1970-01-01
    • 2021-04-20
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多