c - 如何计算 .c 文件中的 ifs?

标签 c

我需要一些帮助来完成我的代码。该程序需要从一个 .c 文件中找到所有的 if 并计算它们。到目前为止,我能够找到所有后跟 '(' 的 if 字符串(因为 ifs 在它们之后有一个 '(' )但现在我担心可能会有一些以 if 结尾并后跟 '( ' 并且它也会被计算在内(不同的功能或类似的东西)。所以我想检查'i'之前是否有东西,如果没有我会计算它但如果有某种我不会。所以我的问题是我的想法好不好?如果好怎么办?如果不好怎么办?

到目前为止,这是我的代码:

#include <stdio.h>
int main(){
    FILE *file = fopen ("poohzad.c", "r");
    char *ch = NULL;
    int i=1;
    if (file != NULL){
        char line [256];
        while (fgets(line, sizeof line, file) != NULL){
            if (strstr(line, "if")){
                ch=strstr(line, "if");
                if(*(ch+2) == '(') printf("%s\n", line);
            }
            i++;
        }

    }
    fclose (file);
    return 0;
}

最佳答案

大多数错误检查都被省略了。需要您自担风险使用它。 (使用 AStyle 格式化,所以请不要评论)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int ifs;

/**
   loads an ascii file, returns NULL on failure
*/
char *loadFile(char*fname){
    char *buff;
    int len;
    FILE *fd;
    fd=fopen( fname,"r");
    if(!fd){
         printf("error opening file '%s'\n");
         exit(1);
    }
    fseek(fd,0,SEEK_END);
    len=ftell(fd);
    fseek(fd,0, SEEK_SET);
    buff=malloc(len+1);
    fread(buff,1,len,fd);
    fclose(fd);
    *(buff+len)=0;
    return buff;
}

/**
  placeholder what to do with the word
*/
void addWord(const char *word){

    if(0)
        printf("'%s'\n",word);
    else
    if(!strcmp(word,"if"))
        ifs++;
}


/**
    is operator simplified
*/
int isop(char c){
    static char list[]=
    "_"
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "0123456789";
    return !strchr(list,c);
}

int main(){
    char *pbuff,*buff,*ptr,c;
    int pos;
    pbuff=loadFile("main.c");
    buff=pbuff;
    ptr=pbuff;


    while(c=*buff){
        switch(c){
            case '?':
                if(0){
                    addWord("if");
                }
                ptr=&buff[1];
                break;
            case '#':
                while(*buff && *buff!='\n'){
                    if(c=='\\') buff++;
                    buff++;
                }
                ptr=&buff[1];
                break;
            case '/': //comment?

                //single line comment
                if(buff[1]=='/'){
                    buff+=2;
                    while(*buff && *buff!='\n'){
                        if(*buff=='\\')buff++;
                        buff++;
                    }

                    //multi line comment
                }else if(buff[1]=='*'){
                    buff+=2;
                    while(*buff && !((buff[-1]=='*') && (*buff=='/'))){
                        buff++;
                    }
                }else{
                    //not a comment, just ignore it;
                }
                ptr=&buff[1];
                break;
            case '\"': //skip strings
            case '\'':
                buff++;
                while(*buff && *buff!=c){
                    if(*buff=='\\') buff++;
                    buff++;
                }
                ptr=&buff[1];
                break;
            default:
                if(isspace(c) || isop(c)){
                    if(ptr-buff){
                        *buff=0;
                        addWord((const char*)ptr);
                        *buff=c;
                    }
                    ptr=&buff[1];
                }
        }
        buff++;

    }

    // take habit to free memory
    free(pbuff);
    printf("there are %d ifs in source file\n",ifs);
    return 0;
}

关于c - 如何计算 .c 文件中的 ifs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33746515/

相关文章:

c - vim 的高效非交互使用

c - 警告 : format '%d' expects argument of type 'int *' , 但参数 2 的类型为 'int'

C 在不知道字符数的情况下分配一个字符串

c - printf 系列中有返回字符串一部分的函数吗?

c - 二进制值的小端计算

c - 如何在 C 上设置 unsigned char 数组的值

c - C 中用位表示一组整数

c - 使用两个标准排序

c - 如何解决这个linux定时器错误

c - mod_auth_token 安装问题