请教高手一个关于循环处理字符串的程序问题,多谢

C语言 码拜 9年前 (2015-05-11) 794次浏览 0个评论
 

#include<ctype.h>
#include<stdbool.h>
#define STOP “”|””
int main()
{
char c;
char prev;
long n_chars = 0L;
int n_lines = 0;
int n_words = 0;
int p_lines = 0;
bool inword = false;
printf(“Enter text to be analyzed(|to terminate);\n”);
prev = “”\n””;
while ((c = getchar()) != STOP)
{
n_chars++;
if (c == “”\n””)
n_lines++;
if (!isspace(c) && !inword)
{
inword = true;
n_words++;
}
if (isspace(c) && inword)
inword = false;
prev = c;
}
if (prev != “”\n””)
p_lines = 1;
printf(“characters=%ld,words=%d,lines=%d,”, n_chars, n_words, n_lines);
printf(“partial lines=%d\n”, p_lines);
return 0;
}
该程序只会处理一次输入,如果想要在处理完第一次输入以后不退出调试继续输入应该对程序做那些修改?

20分
#include<ctype.h>
#include<stdbool.h>
#define STOP ""|""
int main() {
    char c;
    char prev;
    long n_chars;
    int n_lines;
    int n_words;
    int p_lines;
    bool inword;

    while (1) {
        n_chars = 0L;
        n_lines = 0;
        n_words = 0;
        p_lines = 0;
        inword = false;
        printf("Enter text to be analyzed(|to terminate,Ctrl+C to exit);\n");
        prev = ""\n"";
        while ((c = getchar()) != STOP) {
            n_chars++;
            if (c == ""\n"")
                n_lines++;
            if (!isspace(c) && !inword) {
                inword = true;
                n_words++;
            }
            if (isspace(c) && inword)
                inword = false;
            prev = c;
        }
        if (prev != ""\n"")
            p_lines = 1;
        printf("characters=%ld,words=%d,lines=%d,", n_chars, n_words, n_lines);
        printf("partial lines=%d\n", p_lines);
    }
    return 0;
}
引用 1 楼 zhao4zhong1 的回复:
#include<ctype.h>
#include<stdbool.h>
#define STOP ""|""
int main() {
    char c;
    char prev;
    long n_chars;
    int n_lines;
    int n_words;
    int p_lines;
    bool inword;

    while (1) {
        n_chars = 0L;
        n_lines = 0;
        n_words = 0;
        p_lines = 0;
        inword = false;
        printf("Enter text to be analyzed(|to terminate,Ctrl+C to exit);\n");
        prev = ""\n"";
        while ((c = getchar()) != STOP) {
            n_chars++;
            if (c == ""\n"")
                n_lines++;
            if (!isspace(c) && !inword) {
                inword = true;
                n_words++;
            }
            if (isspace(c) && inword)
                inword = false;
            prev = c;
        }
        if (prev != ""\n"")
            p_lines = 1;
        printf("characters=%ld,words=%d,lines=%d,", n_chars, n_words, n_lines);
        printf("partial lines=%d\n", p_lines);
    }
    return 0;
}

赵老师,请问实现ctrl+c退出是那一句,我觉得在输入ctrl+c以后第二个while虽然没实现但是第一个while条件仍然为真,应该会继续等待输入,而不是退出,请赵老师解答

引用 1 楼 zhao4zhong1 的回复:
#include<ctype.h>
#include<stdbool.h>
#define STOP ""|""
int main() {
    char c;
    char prev;
    long n_chars;
    int n_lines;
    int n_words;
    int p_lines;
    bool inword;

    while (1) {
        n_chars = 0L;
        n_lines = 0;
        n_words = 0;
        p_lines = 0;
        inword = false;
        printf("Enter text to be analyzed(|to terminate,Ctrl+C to exit);\n");
        prev = ""\n"";
        while ((c = getchar()) != STOP) {
            n_chars++;
            if (c == ""\n"")
                n_lines++;
            if (!isspace(c) && !inword) {
                inword = true;
                n_words++;
            }
            if (isspace(c) && inword)
                inword = false;
            prev = c;
        }
        if (prev != ""\n"")
            p_lines = 1;
        printf("characters=%ld,words=%d,lines=%d,", n_chars, n_words, n_lines);
        printf("partial lines=%d\n", p_lines);
    }
    return 0;
}

赵老师,不好意思,我才查了吓知道ctrl+c是强制退出

ctrl+c有时候不行就用Ctrl+Break,如果还是不行,点cmd窗口右上角的×,如果还是不行,在任务管理器里面结束进程cmd.exe!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明请教高手一个关于循环处理字符串的程序问题,多谢
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!