#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);
}
注释规范判断
最新推荐文章于 2021-06-07 12:43:27 发布