注释规范判断

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define MAX_CHAR    1024
#define MAX_ROWS    1024
#define MAX_NOTES   20

int main(int argc, char *argv[])
{
    FILE    *in;
    void    *result;
    int     row = 1;
    int     row_space[MAX_ROWS];
    char    row_first_c[MAX_ROWS];
    char    star_after_c[MAX_ROWS];
    char    c;
    char    line[MAX_CHAR];
    int     i = 0;
    int     j = 0;
    int     colon_mark;
    int     note1_mark = 0;
    int     note2_mark = 0;
//  int     colon[20];
//  int     colon_num = 0;
    int     note1[MAX_NOTES];
    int     note2[MAX_NOTES];

    printf("file: %s\n", argv[1]);
    result = (in = fopen("test.txt", "r"));
    if (result == NULL) {
        printf("open %s fail\n", argv[1]);
        return -1;
    }

    /* feof()遇到eof返回非零 */
    while (!feof(in)) {

        int     colon[20];
        int     colon_num = 0;

        fgets(line, MAX_CHAR, in);
        /* 记录冒号的位置 要把冒号的位置清零!!!!!!!!!!*/
        for (i = 0; i < strlen(line); i++) {
            if (line[i] == '"') {
                colon[colon_num] = i;
                colon_num++;
            }
        }

        /*记录每一行开头的空格数目,以及第一个字母*/
        for (i = 0; i < strlen(line); i++) {
            if (line[i] != ' ') {
                row_space[row] = i;
                row_first_c[row] = line[i];
                star_after_c[row] = line[i+1];
                break;
            }
        }

        /*对 // 注释进行判断*/
        for (i = 0; i < strlen(line); i++) {
            if (line[i] == '/' && line [i+1] == '/') {
                for (j = 0; j < colon_num; j++) {
                    if (i < colon[j]) {
                        colon_mark = j;
                        break;
                    }
                }

                /* 如果 // 在冒号之间则忽略 */
                if ((colon_mark % 2) == 0) {
                    printf("error:%d row has error notes\n", row);
                }
            }
        }

        /*对 /*后是否有空格 */
        for (i = 0; i < strlen(line); i++) {
            if (line[i] == '/' && line [i+1] == '*') {
                for (j = 0; j < colon_num; j++) {
                    if (i < colon[j]) {
                        colon_mark = j;
  //                      printf("colon_mark = %d", colon_mark);
                        break;
                    }
                }

                if (line[i+2] != ' ' && ((colon_mark % 2) == 0)) {
                    printf("error:%d row has lost space after /*\n", row);
                }

                if ((colon_mark % 2) == 0) {
                    note1[note1_mark] = row;
                    note1_mark++;
                }
            }
        }

        /*对 * /之前是否有空格 */
        for (i = 0; i < strlen(line); i++) {
            if (line[i] == '*' && line [i+1] == '/') {
                for (j = 0; j < colon_num; j++) {
                    if (i < colon[j]) {
                        colon_mark = j;
                        break;
                    }
                }

                if (line[i-1] != ' ' && ((colon_mark % 2) == 0)) {
                    printf("error:%d row has lost space before */ \n", row);
                }

                if ((colon_mark % 2) == 0) {
                    note2[note2_mark] = row;
                    note2_mark++;
                }
            }
        }

        printf("%d: %s", row, line);
        row++;
    }

    printf("\n");
    int tmp;
    int tmp2;
    // int x; 我定义了一个这个怎么就不可以了 判断都错了?
    for (i = 0; i < note1_mark; i++) {
        tmp = note1[i];

        /* 多行注释判断 */
        if (note1[i] != note2[i]) {
            printf("%d row to %d row is Multi line notes\n", tmp, note2[i]);
            tmp2 = note2[i];
            if (row_space[tmp] != row_space[tmp2 + 1] ) {
                printf("error:%d row don't aline as %d row\n", tmp, tmp2+1);
            }

            for(j = tmp + 1; j <= tmp2; j++) {
                if (row_first_c[j] != '*') {
                    printf("error:%d row don't has star\n", j);
                } else if ((star_after_c[j] != ' ') && (j != tmp2)) {
                    printf("error:%d row after * don't has space\n", j);
                }

                if (row_space[j] != (row_space[tmp] + 1)) {
                    printf("error:%d row don't aline as %d row\n", j, tmp);
                }
            }
        } else {
            if (row_first_c[tmp] == '/') {
                if (row_space[tmp] != row_space[tmp+1]) {
                printf("error:%d rows don't has the same space as next row\n", tmp);
                }
            }
        }

    }

/*    printf("\n\nrows space:\n ");
    for(i = 0; i < row; i++) {
        printf("%d rows space = %d\n", i, row_space[i]);
    }
 */
    printf("\nthe file %s has %d row\n", "test.txt", row);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值