while ((temp = fgetc(fp)) != EOF)
{
if(temp == '\n')
{
chars++;
lines++;
if((temp = fgetc(fp)) != EOF && (temp == '(' || temp == ')' || temp == '{' || temp == '}'))
{
chars++;
brackets++;
}
}
}
基本上我想计算随机c文件中的每个(),{}和行。这个循环计算行很好,但无法统计所有指定的符号。知道为什么会这样吗?
答案 0 :(得分:3)
考虑一个简单的状态机结构,如:
int ch;
while ((ch = getc(fp)) != EOF) {
switch (ch) {
case '\n':
chars++;
lines++;
break;
case '(': case ')': /* ... */
chars++;
brackets++;
break;
}
}
答案 1 :(得分:1)
while ((temp = getc(fp)) != EOF)
{
chars++;
if(temp == '\n')
{
lines++;
continue;
}
if(temp == '(' || temp == ')' || temp == '{' || temp == '}')
{
brackets++;
}
}
答案 2 :(得分:1)
while ((temp = fgetc(fp)) != EOF)
{
if(temp == '\n')
lines++;
else if (temp == '(' || temp == ')') // include other brackets
brakets++;
chars++; // it appears that you want to count them all?
}