通过获取从输入文本计数符号和空行

时间:2014-01-23 20:07:27

标签: c

程序应该从输入文本中打印空行数和某些运算符。我遇到了修复空行的问题,但是我遇到了与操作员有关的问题。我猜这次休息有点不对劲。我很感激修复代码的任何好主意。对不起,如果这样的线程已经存在,但我检查过,无法得到解决方案。提前致谢!

    char c,line[300];
int emptyLine = 0;      
int operators = 0;
printf("Input your text and press ctr+Z on a new line when done: \n");
        while(gets(line)) { 
        int i = 0;emptyLine++;
        for (i = 0; i < strlen(line); i++) {
            if(line[i] == '+'|| line[i] == '-' || line[i] == '/' || line[i] == '*' || line[i] == '%') 
            {
                operators++;    
            }
            if (line[i] != '\n' && line[i] != '\t' && line[i] != ' ') {
                emptyLine--;
                break;
                }
            }
            } 
    printf("The number of empty lines is: %d",emptyLine);
    printf("\nThe number of opperators is: %d",operators);

1 个答案:

答案 0 :(得分:-1)

尝试这样的事情:

char c,line[300];
int emptyLine = 0;      
int operators = 0;
printf("Input your text and press ctr+Z on a new line when done: \n");
while(gets(line)) { 
    bool isEmpty = true;  // Assume empty line
    for (int i = 0; 0 != line[i]; i++) {
        if (!isspace(line[i])) {
            isEmpty = false;   // Line not empty
        }
        if (line[i] == '+'|| line[i] == '-' || line[i] == '/' || line[i] == '*' || line[i] == '%') {
            operators++;    
        }
    }
    if (isEmpty) {
        emptyLine += 1;
    }
}
printf("The number of empty lines is: %d",emptyLine);
printf("\nThe number of operators is: %d",operators);