计算C程序中的注释数

时间:2016-11-25 20:22:56

标签: c file file-io comments enumeration

我开始在大学学习C语言。教授给了我们一个任务 - 编写一个计算另一个C程序中评论数量的程序。我们还没有谈到过使用文件。我找到了类似的解决方案 - C Program to count comment lines (// and /* */)。修改了一下它实际上有效,但我无法理解enum的东西。试图重写它没有枚举但没有成功(因为我们必须解释如何编程工作)。我的问题是 - 有没有办法在没有枚举的情况下解决它?

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

int main(int argc, char **argv) {

FILE *fp;
int c, i=0;
char ch;
char path[150];

unsigned int chars  = 0;
unsigned int multi  = 0;
unsigned int single = 0;

enum states { TEXT, 
    SAW_SLASH, 
    SAW_STAR, 
    SINGLE_COMMENT, 
    MULTI_COMMENT } state = TEXT;

printf("Write file's path. Separate the folders with TWO back slashes (\\)\n");
scanf("%s", &path);

fp = fopen(path, "r");
if ( !fp )
{
    fprintf(stderr, "Cannot open file %s\n", argv[1] );
}
else {
    while((c=fgetc(fp)) != EOF){

    switch( state ) {
        case TEXT :
        switch( c )
        {
            case '/'  : state = SAW_SLASH; break;
            default   : break;
        }
        break;
        case SAW_SLASH :
            switch( c )
            {
              case '/'  : 
                  printf("case SLASH case / \n");
                  state = SINGLE_COMMENT; 
                  break;
              case '*'  : 
                  printf("case SLASH case * \n");
                  state = MULTI_COMMENT; 
                  break;
              default   : 
                  state = TEXT; 
                  break;
            }
            break;
        case SAW_STAR :
            switch( c )
            {
                case '/'  : 
                    printf("case STAR case / \n");
                    state = TEXT; 
                    multi++; 
                    break;
                case '*'  : 
                    break;
                case '\n' : 
                    printf("case SLASH case 'NEW LINE' \n");
                    multi++; // fall through
                default   : 
                    state = MULTI_COMMENT; 
                    break;
            }
            break;
        case SINGLE_COMMENT :
            switch( c )
            {
              case '\n' : 
                  printf("case SINGLE case NEW LINE \n");
                  state = TEXT; 
                  single++; // fall through
              default   : 
                  break;
            }
            break;
        case MULTI_COMMENT :
            switch( c )
            {
              case '*'  : 
                  printf("case MULTI case * \n");
                  state = SAW_STAR; 
                  break;
              case '\n' : 
                break;
              default   : 
                  break;
            }
            break;

        default: // NOT REACHABLE
            break;
        }
    }
    fclose(fp);
    printf( "File                 : %s\n", argv[1] );
    printf( "Single-comment: %8u\n", single );
    printf( "Multi-comment: %8u\n", multi );

}
return 0;
}

1 个答案:

答案 0 :(得分:0)

由于枚举相当于一堆#define,你可以替换这部分代码:

import java.util.Scanner;
public class class2 {

    public void Multipleclass(){
       String x,y;
       Scanner sc=new Scanner(System.in);

       System.out.println("Enter your First name");
       x=sc.next();
       System.out.println("Enter your Last name");
       y=sc.next();

       System.out.println(x+  " "  +y );
   }
}

使用:

enum states {
    TEXT, 
    SAW_SLASH, 
    SAW_STAR, 
    SINGLE_COMMENT, 
    MULTI_COMMENT
} state = TEXT;