K& R2 1-23 [英] K&R2 1-23

查看:90
本文介绍了K& R2 1-23的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

练习是从源文件中删除所有注释。到目前为止我写的
是:


/ * decommentizer:从源头删除这种评论* /

#include< stdio.h>


void strconst(char);

void quotestr(char);

void decomment(void);


int main(无效)

{

int c;

while((c = getchar()))!= EOF)

{


我不确定我是否通过案例。例如,我是理所当然地确定处理引用字符串的函数只是简单地使用putchar()直到它击中另一个字符串。 。我已经和自己的角色常数一起讨论了相同的情况。如果是这样,我可以减少上面原型声明的数量

并且通过或者'和'和'

函数做同样的事情。我的另一个问题是其他符合ISO标准,

语法合法的东西可以跟随/除了*。我不相信

/这是这个问题的答案。 MPJ

解决方案

Merrill& Michele写道:

练习是从源文件中删除所有注释。到目前为止我写的是:

/ * decommentizer:从源中删除这种评论风格* /
#include< stdio.h>

void strconst(char);
void quotestr(char);
void decomment(void);

int main(void)
{
int c;
while((c = getchar()))!= EOF)
{
/ *

**执行工作的代码。

* /

}

}


成本不高且可编译:-)


我不确定我是否通过案例正确思考过。例如,我很合理地确定处理引用字符串的函数只是简单地putchar()直到它击中另一个字符串。 。我已经和自己的角色常数一样了。如果是这样,我可以减少上面原型声明的数量,并且可以通过或''并且具有被调用的
功能做同样的事情。我的另一个问题是,其他符合ISO标准,语法合法的东西可以遵循/除了*。我不相信这是/这个问题的答案。 MPJ




如果你限于练习,没有C ++ / C99行

评论(//)。


您的案件还要照顾不要离开的事情。一个字符串

字面上的\"但是在\\",例如(扩展到/检查留给你的其他

案例:-))。


提示:介绍一个表示状态的变量(外部/在注释/中

字符串/在字符常量中)。 (你似乎有计划这样做但是

没有明确提到它。)

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


Michael Mair

Merrill& Michele写道:

练习是从源文件中删除所有注释。到目前为止我写的是:

/ * decommentizer:从源中删除这种评论风格* /
#include< stdio.h>

void strconst(char);
void quotestr(char);
void decomment(void);

int main(void)
{
int c;
while((c = getchar()))!= EOF)
{


/ *
**代码正在完成工作。
* /
}
}

成本不高且可编辑: - )



我是很高兴我在这一点上检查了错误的头脑思维,而不是编码,因为它挽救了尴尬,而你ugghhh就是这个人

将要学习?"

我不确定我是否通过案例正确思考过。例如,
我合理地确定处理引用字符串的函数将是
简单地putchar()直到它击中另一个字符串。 。我已经和
谈了同样的字符常量。如果是这样,我可以减少上面的
原型声明数量并且通过或''并且让
调用函数做同样的事情。我的另一个问题是,除了*之外,其他符合ISO标准的语法合法的东西是什么。我不认为/是这个问题的答案。 MPJ



如果你限于练习,没有C ++ / C99行
评论(//)。

你的案例也有照顾不要离开的事情。 \"字符串
但是在\\",例如(扩展到/检查留给你的其他
案例:-))。

提示:引入一个表明状态的变量(外/ in comment / in
string / in character constant)。 (你似乎有计划这样做,但
没有明确提到它。)



我没有。我改变了这个愚蠢的逻辑大厦。回去工作。 MPJ


2004年12月6日星期一12:05:48 -0600,Merrill& Michele

< be ******** @ comcast.net>写道:

我不确定我是否通过案例正确思考。例如,我很合理地确定处理引用字符串的函数只是简单地putchar()直到它击中另一个字符串。 。


字符串和字符常量中的转义引用?

我自己也谈到了与字符常量相同的情况。如果是这样,我可以减少上面原型声明的数量,并且可以通过或''并且具有被调用的
功能做同样的事情。


是的,我经常这样做,传递起始字符作为结尾

字符,并且区别对待字符串和字符常量。

我的另一个问题是其他符合ISO标准,语法合法的东西可以跟随/除了*。我不相信这是/这个问题的答案。 MPJ




相信它,这就是ISO C规范。说。当然,作为措辞,

答案是几乎任何东西。 (例如,a = b / -c;)。但我认为

你的意思是什么可以跟随/作为一个有效的令牌?,以及

的答案是*或/。并且不要忘记加入行(\然后换行)

以//开头的行注释。


Chris C


The exercise is to remove all comments from a source file. What I''ve
written so far is:

/* decommentizer: removes this style of comment from source */
#include <stdio.h>

void strconst(char);
void quotestr(char);
void decomment(void);

int main(void)
{
int c;
while((c = getchar())) != EOF)
{

I''m not sure if I''ve thought correctly through the cases. For example, I''m
reasonably certain that the function that handles quoted strings will simply
putchar() until it hits another " . I''ve got myself talked into the same
being the case with character constants. If so, I could reduce the number
of prototype declarations above and either pass " or '' and have the called
function do the same thing. My other question is what other ISO-conforming,
syntactically-legal things can follow a / besides an * . I don''t believe
that / is an answer to this question. MPJ


解决方案

Merrill & Michele wrote:

The exercise is to remove all comments from a source file. What I''ve
written so far is:

/* decommentizer: removes this style of comment from source */
#include <stdio.h>

void strconst(char);
void quotestr(char);
void decomment(void);

int main(void)
{
int c;
while((c = getchar())) != EOF)
{ /*
** Code doing the work.
*/
}
}

Does not cost much and is compilable :-)

I''m not sure if I''ve thought correctly through the cases. For example, I''m
reasonably certain that the function that handles quoted strings will simply
putchar() until it hits another " . I''ve got myself talked into the same
being the case with character constants. If so, I could reduce the number
of prototype declarations above and either pass " or '' and have the called
function do the same thing. My other question is what other ISO-conforming,
syntactically-legal things can follow a / besides an * . I don''t believe
that / is an answer to this question. MPJ



If you restrict yourself to the exercise, there are no C++/C99 line
comments (//).

Your cases have also to take care of things like not "leaving" a string
literal on \" but on \\", for example (expansion to/checking for other
cases left to you :-)).

Hint: Introduce a variable indicating the state (outside/in comment/in
string/in character constant). (You seem to have planned doing that but
did not explicitly mention it.)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


"Michael Mair"

Merrill & Michele wrote:

The exercise is to remove all comments from a source file. What I''ve
written so far is:

/* decommentizer: removes this style of comment from source */
#include <stdio.h>

void strconst(char);
void quotestr(char);
void decomment(void);

int main(void)
{
int c;
while((c = getchar())) != EOF)
{


/*
** Code doing the work.
*/
}
}

Does not cost much and is compilable :-)


I''m glad I checked at this point for wrong-headed thinking insttead of
coding away, as it saved the embarrassment and you the "ugghhh is this guy
ever going to learn?"

I''m not sure if I''ve thought correctly through the cases. For example, I''m reasonably certain that the function that handles quoted strings will simply putchar() until it hits another " . I''ve got myself talked into the same being the case with character constants. If so, I could reduce the number of prototype declarations above and either pass " or '' and have the called function do the same thing. My other question is what other ISO-conforming, syntactically-legal things can follow a / besides an * . I don''t believe that / is an answer to this question. MPJ



If you restrict yourself to the exercise, there are no C++/C99 line
comments (//).

Your cases have also to take care of things like not "leaving" a string
literal on \" but on \\", for example (expansion to/checking for other
cases left to you :-)).

Hint: Introduce a variable indicating the state (outside/in comment/in
string/in character constant). (You seem to have planned doing that but
did not explicitly mention it.)


I didn''t. I had this silly logic edifice instead. Back to work. MPJ


On Mon, 6 Dec 2004 12:05:48 -0600, Merrill & Michele
<be********@comcast.net> wrote:

I''m not sure if I''ve thought correctly through the cases. For example, I''m
reasonably certain that the function that handles quoted strings will simply
putchar() until it hits another " .
Escaped quotes in strings and character constants?
I''ve got myself talked into the same
being the case with character constants. If so, I could reduce the number
of prototype declarations above and either pass " or '' and have the called
function do the same thing.
Yes, I often do that, pass the start character to be used as the end
character and treat strings and character constants the same otherwise.
My other question is what other ISO-conforming,
syntactically-legal things can follow a / besides an * . I don''t believe
that / is an answer to this question. MPJ



Believe it, it''s what the ISO C spec. says. As worded, of course, the
answer is "almost anything" (a = b/-c; for example). But what I think
you mean is "What can follow / as a valid token?", and the answer to
that is * or /. And don''t forget line joining (\ followed by newline)
in line comments started with //.

Chris C


这篇关于K&amp; R2 1-23的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆