K& R2,练习1-23
没有编译时错误或警告。它运行但它没有按照它应该做的那样去做。
想要:打印输入C程序。删除所有评论。
GOT:不打印任何内容。
--------程序----- ------
/ * K& R2:练习1-23
声明:
写一个程序来删除来自C程序的所有评论
不要忘记正确处理引用的字符串和字符常量。
方法:
1.)该程序一次输入一行。它将所有
行(完整输入)复制到一个字符数组中。
2.)然后我们调用一个函数remove_comments。从输入中删除所有
注释。
3.)然后我们打印数组。现在不包含任何评论。
* /
#include< stdio.h>
#定义MAXLENGTH 30000
int getline(char arr []);
void remove_comments(char arr []);
int main()
{
int i = 0;
char arr [MAXLENGTH];
>
/ *初始化数组* /
for(i = 0; i< MAXLENGTH - 1; ++ i)
arr [i] = 0 ;
而(getline(arr))
;
remove_comments(arr);
printf(" ------------------------- \\\
");
printf("%s \\ \\ n",arr);
返回0;
}
int getline(char arr [])
{
int c = 0;
int i = 0;
for(i = 0; i< MAXLENGTH - 1&&(c = getchar())!= EOF&& c!=''\ n'';
++ i)
arr [i] = c;
if(c ==''\ n'')
arr [i ++] = \ n'';
arr [i] =''\ 0'';
返回i;
}
void remove_comments(char arr [])
{
int i = 0;
int j = 0;
int incomment = 0;
char arr2 [MAXLENGTH -1];
代表(i = 0;我< MAXLENGTH - 1; ++ i)
arr2 [i] = 0;
for(i = 0; arr [i]!=''\ 0''; ++ i )
{
if(arr [i]!=''"'')
{
if(arr [i] ==''/''&& arr [i + 1] ==''*'')
incomment = 1;
}
if(arr [i] ==''*''&& arr [i + 1] ==''/''&& arr [i + 2] !=''"'')
incomment = 0;
if(incomment == 0)
arr2 [ j ++] = arr [i];
}
arr2 [j] =''\''';
for(i = 0; arr2 [i]!=''\ 0''; ++ i)
arr [i] = arr2 [i];
}
-----------输出----------------
[arch @ voodo kr2] $ gcc -ansi -pedantic -Wall -Wextra -O ex_1-23.c
[arch @ voodo kr2] $ ./a.out
喜欢这个
-------------------------
[arch @ voodo kr2] $
there are no compile-time errors or warning. it runs but it does not
do what it is supposed to.
WANTED: to print the "input C programme" with all comments removed.
GOT: does not print anything.
-------- PROGRAMME -----------
/* K&R2: exercise 1-23
STATEMENT:
write aprogramme to remove all comments from a C programme
dont forget to handle quoted strings and character constants properly.
METHOD:
1.) this programme gets its input a line at a time. it copies all the
lines (full input) into a characters array.
2.) we then call a function "remove_comments" which removes all the
comments from the input.
3.) we then print the "array" which now does not contain any comments.
*/
#include<stdio.h>
#define MAXLENGTH 30000
int getline(char arr[]);
void remove_comments(char arr[]);
int main()
{
int i = 0;
char arr[MAXLENGTH];
/* initialising array */
for(i = 0; i < MAXLENGTH - 1; ++i)
arr[i] = 0;
while(getline(arr))
;
remove_comments(arr);
printf("-------------------------\n");
printf("%s\n", arr);
return 0;
}
int getline(char arr[])
{
int c = 0;
int i = 0;
for(i = 0; i < MAXLENGTH - 1 && (c = getchar()) != EOF && c != ''\n'';
++i)
arr[i] = c;
if(c == ''\n'')
arr[i++] = ''\n'';
arr[i] = ''\0'';
return i;
}
void remove_comments(char arr[])
{
int i = 0;
int j = 0;
int incomment = 0;
char arr2[MAXLENGTH -1];
for(i=0; i < MAXLENGTH - 1; ++i)
arr2[i] = 0;
for(i=0; arr[i] != ''\0''; ++i)
{
if(arr[i] != ''"'')
{
if(arr[i] == ''/'' && arr[i+1] == ''*'')
incomment = 1;
}
if(arr[i] == ''*'' && arr[i+1] == ''/'' && arr[i+2] != ''"'')
incomment = 0;
if(incomment == 0)
arr2[j++] = arr[i];
}
arr2[j] = ''\0'';
for(i = 0; arr2[i] != ''\0''; ++i)
arr[i] = arr2[i];
}
----------- OUTPUT ----------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-23.c
[arch@voodo kr2]$ ./a.out
like this
-------------------------
[arch@voodo kr2]$
gcc -ansi -pedantic -Wa ll -Wextra -O ex_1-23.c
[arch @ voodo kr2]
gcc -ansi -pedantic -Wall -Wextra -O ex_1-23.c
[arch@voodo kr2]
./ a.out
赞了这个
-------------------------
[arch @ voodo kr2 ]
./a.out
like this
-------------------------
[arch@voodo kr2]