[英]Why is this program yielding wrong output
This program is supposed to remove all comments from a C source code (in this case comments are considered double slashes '//' and a newline character '\\n' and anything in between them, and also anything between '/* ' and '*/'. 该程序应该从C源代码中删除所有注释(在这种情况下,注释被视为双斜杠'//'和换行符'\\ n'以及它们之间的任何内容,以及介于'/ *'和'之间的任何内容) * /”。
The program: 该程序:
#include <stdio.h>
/* This is a multi line comment
testing */
int main() {
int c;
while ((c = getchar()) != EOF)
{
if (c == '/') //Possible comment
{
c = getchar();
if (c == '/') // Single line comment
while (c = getchar()) //While there is a character and is not EOF
if (c == '\n') //If a space character is found, end of comment reached, end loop
break;
else if (c == '*') //Multi line comment
{
while (c = getchar()) //While there is a character and it is not EOF
{
if (c == '*' && getchar() == '/') //If c equals '*' and the next character equals '/', end of comment reached, end loop
break;
}
}
else putchar('/'); putchar(c); //If not comment, print '/' and the character next to it
}
else putchar(c); //if not comment, print character
}
}
After I use this source code as its own input, this is the output I get: 在将此源代码用作其自己的输入之后,这是我得到的输出:
#include <stdio.h>
* This is a multi line comment
testing *
int main() {
int c;
while ((c = getchar()) != EOF)
{
if (c == '') ////////////////
{
c = getchar();
if (c == '') ////////////////////
while (c = getchar()) /////////////////////////////////////////
if (c == '\n') ///////////////////////////////////////////////////////////////
break;
else if (c == '*') ///////////////////
{
while (c = getchar()) ////////////////////////////////////////////
{
No more beyond this point. 没有更多的东西超出了这一点。 I'm compiling it using g++ on the ubuntu terminal. 我正在ubuntu终端上使用g ++对其进行编译。
As you can see, multi lines comments had only their '/' characters removed, while single line ones, had all their characters replaced by '/'. 如您所见,多行注释仅删除了'/'字符,而单行注释将其所有字符替换为'/'。 Apart from that, any '/' characters that were NOT the beginning of a new comment were also removed, as in the line if (c == ''), which was supposed to be if (c == '/'). 除此之外,还删除了不是新注释开头的所有'/'字符,例如if(c =='')行,而if(c =='/')应该是。
Does anybody know why? 有人知道为什么吗? thanks. 谢谢。
C does not take notice of the way you indent your code. C不会注意您缩进代码的方式。 It only cares about its own grammar. 它只关心自己的语法。
Look carefully at your else
s and think about which if
they attach to (hint: the closest open one). 仔细查看您的else
并考虑它们if
附在其中(提示:最接近的敞开的)。
There are other bugs, as well. 还有其他错误。 EOF
is not 0, so only the first while
is correct. EOF
不为0,因此只有第一个while
是正确的。 And what happens if the comment looks like this: /* something **/
? 如果注释看起来像这样,会发生什么: /* something **/
?
while (c = getchar()) //While there is a character and is not EOF
You're assuming that EOF == 0
. 您假设EOF == 0
。 Why not be explicit and change the preceding line to: 为什么不明确,将上一行更改为:
while((c = getchar()) != EOF)
else putchar('/'); putchar(c);
Are both of the putchar
s supposed to be part of the else
clause? 这两个putchar
是否都应该是else
子句的一部分? If so, you need braces {}
around the two putchar
statements. 如果是这样,则需要在两个putchar
语句两边加上大括号{}
。 Also, give each putchar
its own line; 另外,给每个putchar
自己的行; it not only looks nicer but it's more readable. 它不仅看起来更好,而且更具可读性。
Other than what I've mentioned, your logic looks sound. 除了我刚才提到的,您的逻辑看起来还不错。
As already mentioned, the if/else matching is incorrect. 如前所述,if / else匹配不正确。 One aditional missing functionality is that you must make it more stateful to keep track of whether you are inside a string or not, eg 缺少的一项附加功能是,您必须使其状态更强,以跟踪是否在字符串中,例如
printf("This is not // a comment\n");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.