K&R第1章 – 练习22解决方案,你觉得怎么样?

K&R第1章 – 练习22解决方案,你觉得怎么样?,第1张

概述我正在从K& r学习C作为第一语言,我只是想问一下,如果你认为这个练习是正确的解决方法,我知道它可能没有你想要的那么完整,但我想要看法,所以我知道我正在学习C. 谢谢 /* Exercise 1-22. Write a program to "fold" long input lines into two or * more shorter lines, after the last non-b 我正在从K& r学习C作为第一语言,我只是想问一下,如果你认为这个练习是正确的解决方法,我知道它可能没有你想要的那么完整,但我想要看法,所以我知道我正在学习C.

谢谢

/* Exercise 1-22. Write a program to "fold" long input lines into two or * more shorter lines,after the last non-blank character that occurs * before then n-th column of input. Make sure your program does something * intelligent with very long lines,and if there are no blanks or tabs * before the specifIEd column. *  * ~svr * * [NOTE: Unfinished,but functional in a generic capacity] * Todo: * Handling of spaceless lines * Handling of lines consisting entirely of whitespace */#include <stdio.h>#define FolD 25#define MAX 200#define NEWliNE '\n'#define BLANK ' '#define DEliM 5#define TAB '\t'intmain(voID){    int line  = 0,space = 0,newls = 0,i = 0,c = 0,j = 0;    char array[MAX] = {0};    while((c = getchar()) != EOF) {        ++line;        if(c == NEWliNE)            ++newls;        if((FolD - line) < DEliM) {            if(c == BLANK) {                if(newls > 0) {                    c = BLANK;                    newls = 0;                }                else                    c = NEWliNE;                line = 0;            }        }        array[i++] = c;    }    for(line = 0; line < i; line++) {        if(array[0] == NEWliNE)            ;        else            printf("%c",array[line]);    }    return 0;}
解决方法 我相信你在这个艰难的轨道上,但有一些可读性的指针:

>评论你的东西
>正确地命名变量,至少给出描述,如果你拒绝
因此,如果你使用某些单行,有些则不行. (imho,总是使用{},所以它更可读)
>在最后一个for循环中的if语句可以更好,像

if(array[0] != NEWliNE)      {           printf("%c",array[line]);     }
总结

以上是内存溢出为你收集整理的K&R第1章 – 练习22解决方案,你觉得怎么样?全部内容,希望文章能够帮你解决K&R第1章 – 练习22解决方案,你觉得怎么样?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1248105.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存