while (arr[i] != '\0')
{
if (((arr[i] == '/' && arr[i + 1] == '*') ||
(arr[i] == '*' && arr[i + 1] == '/') ||
(arr[i] == '*' && arr[i - 1] == '/') ||
(arr[i] == '/' && arr[i - 1] == '*'))
|| (arr[i] == ' ' || arr[i] == '\t' || arr[i] == '\r' ||
arr[i] == '\n' || arr[i] == '\v' || arr[i] == '\f'))
flag_char = 0;
if (arr[i] == '/' && arr[i + 1] == '*' && flag_comment == 0)
flag_comment = 1;
if (arr[i] == '*' && arr[i + 1] == '/' && flag_comment == 1)
flag_comment = 0;
if (flag_comment == 1 && flag_char == 1)
{
characters++;
flag_char = 1;
}
else
flag_char = 1;
i++;
}
所以我试图计算多行注释中的字符。如果我做/ * asd * /它会有3个字符,但如果我这样做
/*
asd
*/
它不会计算任何字符,因为数组中填充了一些垃圾元素。当它到达'a'arr [i-1]时显示一些字符而不是\ n。 我怎么能摆脱它?我正在用fgets获取角色:
while (fgets(arr, 100, stdin) != NULL)
{
if (strstr(arr, "Terminate")) break;
getCount(arr); // that's my function name
}