C K&;R答案1-12(使用函数减少代码行数)
我已经写了以下程序来回答克尼汉和里奇的ch1问题12 问题是我从来没有真正理解过如何正确使用函数,我想知道为什么我写在这个程序中的那一个,getcharc(),不起作用 什么是解释正确函数用法的好资源。哪里怎么做 我从Richard Heathfield的站点(它使用C K&;R答案1-12(使用函数减少代码行数),c,kernighan-and-ritchie,C,Kernighan And Ritchie,我已经写了以下程序来回答克尼汉和里奇的ch1问题12 问题是我从来没有真正理解过如何正确使用函数,我想知道为什么我写在这个程序中的那一个,getcharc(),不起作用 什么是解释正确函数用法的好资源。哪里怎么做 我从Richard Heathfield的站点(它使用|或,而不是我使用的嵌套while语句)知道这个问题的最佳解决方案,但是我想知道如何使我的程序正常工作: #include <stdio.h> int getcharc (); // Exercise 1-12 // C
|
或,而不是我使用的嵌套while语句)知道这个问题的最佳解决方案,但是我想知道如何使我的程序正常工作:
#include <stdio.h>
int getcharc ();
// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space
int main()
{
int c;
while ((c = getchar()) != EOF) {
while ( c == '\t') {
getcharc(c);
}
while ( c == '\b') {
getcharc(c);
}
while ( c == '\\') {
getcharc(c);
}
while ( c == ' ') {
getcharc(c);
}
putchar(c);
}
}
int getcharc ()
{
int c;
c = getchar();
printf("\n");
return 0;
}
每次都会重复。一个可能的解决方案是,将函数的原型更改为
int-getcharc(int-c,int-flag)
现在你的代码经过一些修改
#include <stdio.h>
int getcharc (int c, int flag);
// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space
int main()
{
int c;
int flag = 0; //to keep track of repeated newline chars.
while ((c = getchar()) != '\n') {
flag = getcharc(c, flag); // call getcharc() for each char in the input string. Testing for newline and printing of chars be done in the getcharc() function
}
return 0;
}
int getcharc (int c, int flag)
{
if( (c == ' ' || c == '\t' || c == '\b' || c== '\\') && flag == 0)
{
printf("\n");
flag = 1;
}
else
{
if(c != ' ' && c != '\t' && c != '\b' && c!= '\\')
{
putchar(c);
flag = 0;
}
}
return flag;
}
一种可能的解决方案是,将函数的原型更改为
intgetcharc(intc,intflag)
现在你的代码经过一些修改
#include <stdio.h>
int getcharc (int c, int flag);
// Exercise 1-12
// Copy input to output, one word per line
// words deleniated by tab, backspace, \ and space
int main()
{
int c;
int flag = 0; //to keep track of repeated newline chars.
while ((c = getchar()) != '\n') {
flag = getcharc(c, flag); // call getcharc() for each char in the input string. Testing for newline and printing of chars be done in the getcharc() function
}
return 0;
}
int getcharc (int c, int flag)
{
if( (c == ' ' || c == '\t' || c == '\b' || c== '\\') && flag == 0)
{
printf("\n");
flag = 1;
}
else
{
if(c != ' ' && c != '\t' && c != '\b' && c!= '\\')
{
putchar(c);
flag = 0;
}
}
return flag;
}
这个
getcharc()
函数到底应该做什么?它所做的是从输入中读取一个字符,打印一个换行符,然后返回零。刚刚从输入中读取的字符将被丢弃,因为您没有对其执行任何操作。调用时,返回值也将被忽略。在调用它的每个地方,您都在无限循环中调用它,因为没有为更改循环控制变量做任何准备
也许您打算做一些类似于c=getcharc()
的事情,但这并没有真正的帮助,因为您无论如何都不会从函数返回c
。(无论如何,这对“无限循环”部分会有帮助。)
这个函数有什么意义?如果您只是正确地使用了
getchar()
,看起来您已经有了解决方案,除了一些其他错误。这个getcharc()
函数到底应该做什么?它所做的是从输入中读取一个字符,打印一个换行符,然后返回零。刚刚从输入中读取的字符将被丢弃,因为您没有对其执行任何操作。调用时,返回值也将被忽略。在调用它的每个地方,您都在无限循环中调用它,因为没有为更改循环控制变量做任何准备
// compiled by my brain muhahaha
#include <stdio.h>
int getcharc(); // we prototype getcharc without an argument
int main()
{
int c; // we declare c
// read character from stdio, if end of file quit, store read character in c
while ((c = getchar()) != EOF) {
// if c is tab \t call function getcharc() until forever since c never changes
while ( c == '\t') {
getcharc(c); // we call function getcharc with an argument
// however getcharc doesn't take an argument according to the prototype
}
// if c is \b call function getcharc() until forever since c never changes
while ( c == '\b') {
getcharc(c);
}
// if c is \\ call function getcharc() until forever since c never changes
while ( c == '\\') {
getcharc(c);
}
// if c is ' ' call function getcharc() until forever since c never changes
while ( c == ' ') {
getcharc(c);
}
// since we never will get here but if we happened to get here by some
// strange influence of some rare cosmic phenomena print out c
putchar(c);
}
}
// getcharc doesn't take an argument
int getcharc ()
{
int c; // we declare another c
c = getchar(); // we read from the keyboard a character
printf("\n"); // we print a newline
return 0; // we return 0 which anyway will never be read by anyone
}
也许您打算做一些类似于c=getcharc()
的事情,但这并没有真正的帮助,因为您无论如何都不会从函数返回c
。(无论如何,这对“无限循环”部分会有帮助。)
这个函数有什么意义?如果您只是正确地使用
getchar()
,看起来您就有了解决方案,除了一些其他错误。您能给我们提供更多信息吗。。。什么是K&R 1-12?您希望函数做什么?你观察到它目前在做什么?Kernighan和Ritchie=K&R 1-12是那本书中的练习1-12。这是C语言编程的第一本书。在本例中,我只希望函数替换程序中重复的两行代码:c=getchar();printf(“\n”);。就这样,没别的了。我知道很多。然而,我的记忆力不足以记住书中的每一个例子。我希望你能描述一下这个练习的目的。将输入复制到输出,将制表符、退格和多个空格替换为单个空格你的代码根本不应该编译,因为你说getcharc()
不接受任何参数,但你将int c
传递到它(例如,getcharc(c)
)。这就像一个没有输入端口的DVD播放机,你想插上插头。你能给我们更多的信息吗。。。什么是K&R 1-12?您希望函数做什么?你观察到它目前在做什么?Kernighan和Ritchie=K&R 1-12是那本书中的练习1-12。这是C语言编程的第一本书。在本例中,我只希望函数替换程序中重复的两行代码:c=getchar();printf(“\n”);。就这样,没别的了。我知道很多。然而,我的记忆力不足以记住书中的每一个例子。我希望你能描述一下这个练习的目的。将输入复制到输出,将制表符、退格和多个空格替换为单个空格你的代码根本不应该编译,因为你说getcharc()
不接受任何参数,但你将int c
传递到它(例如,getcharc(c)
)。这就像一个没有输入端口的DVD播放机,你试图插上插头。谢谢你的回答。我试图用这个函数实现的只是停止重复这两行'c=getchar();printf(“\n”);”在原始代码中。我现在把它附在我的原始问题上。对不起,你说的是不要使用函数,而是在每个嵌套的while中重复使用getchar()?我对您答案的理解是否正确?printf(“\n”)的目的是什么?看起来你在每个重复的空格/制表符/退格处打印一个换行符?不,我不是说不使用这个函数,我只是不理解它的用途。如果要将循环移动到函数中,也可以将其完全移动到函数中。printf(“\n”)
的意思是打印新行,而不是空格、制表符等。因此,如果输入是一行上的多个单词,并用空格、制表符等分隔单词,则程序将输出,请把单词分别写在各行上。谢谢你的回答。我试图用这个函数实现的只是停止重复这两行'c=getchar();printf(“\n”);”在原始代码中。我现在把它附在我的原始问题上。对不起,你说的是不要使用函数,而是在每个嵌套的while中重复使用getchar()?我对您答案的理解是否正确?printf(“\n”)的目的是什么?看起来像你
// compiled by my brain muhahaha
#include <stdio.h>
int getcharc(); // we prototype getcharc without an argument
int main()
{
int c; // we declare c
// read character from stdio, if end of file quit, store read character in c
while ((c = getchar()) != EOF) {
// if c is tab \t call function getcharc() until forever since c never changes
while ( c == '\t') {
getcharc(c); // we call function getcharc with an argument
// however getcharc doesn't take an argument according to the prototype
}
// if c is \b call function getcharc() until forever since c never changes
while ( c == '\b') {
getcharc(c);
}
// if c is \\ call function getcharc() until forever since c never changes
while ( c == '\\') {
getcharc(c);
}
// if c is ' ' call function getcharc() until forever since c never changes
while ( c == ' ') {
getcharc(c);
}
// since we never will get here but if we happened to get here by some
// strange influence of some rare cosmic phenomena print out c
putchar(c);
}
}
// getcharc doesn't take an argument
int getcharc ()
{
int c; // we declare another c
c = getchar(); // we read from the keyboard a character
printf("\n"); // we print a newline
return 0; // we return 0 which anyway will never be read by anyone
}
// compiled by my brain muhahaha
#include <stdio.h>
int getcharc(); // we prototype getcharc without an argument
int main()
{
int c; // we declare c
// read character from stdio, if end of file quit, store read character in c
while ((c = getchar()) != EOF) {
// if c is tab \t call function getcharc() until forever since c never changes
while ( c == '\t') {
getcharc(c); // we call function getcharc with an argument
// however getcharc doesn't take an argument according to the prototype
}
// if c is \b call function getcharc() until forever since c never changes
while ( c == '\b') {
getcharc(c);
}
// if c is \\ call function getcharc() until forever since c never changes
while ( c == '\\') {
getcharc(c);
}
// if c is ' ' call function getcharc() until forever since c never changes
while ( c == ' ') {
getcharc(c);
}
// since we never will get here but if we happened to get here by some
// strange influence of some rare cosmic phenomena print out c
putchar(c);
}
}
// getcharc doesn't take an argument
int getcharc ()
{
int c; // we declare another c
c = getchar(); // we read from the keyboard a character
printf("\n"); // we print a newline
return 0; // we return 0 which anyway will never be read by anyone
}
int getcharch(int c)
{
...
}