K& R2,练习1-23 [英] K&R2, exercise 1-23

查看:46
本文介绍了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]







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

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