c语言求助

王朝知道·作者佚名  2009-06-15
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
問題描述:

请大家帮忙,谢谢。

A html file is a text file with embedded tags in it.

The tags describe sections and formating information.

Tags often appear in pairs, with the text they format in between.

For example, <font color=red> Red Heading </font>.

The <br> tag appears on its own and indicates a new line.

We want a way to convert from html files to text files that replaces <br>

with a new line character and removes all the tags.

You can assume the characters < and > are only used in tags.

You can also assume that tags always contain lower case characters.

Write a solution to the problem using C programs that read from

standard input and write to standard output.

The first program (debr) reads the input data and pipes it to the next

program (detag) unchanged except that all sequences '<', 'b', 'r', '>'

generate an extra new line character. The detag program outputs the

text except for the tags, that is '<' and everything including the

next '>' are removed.

Here is an example of how it works:

turing % cat test.html

html>

<title> This Page </title>

<body>

Some text on the page

<br> <br>

<font size=9> Big text </font>

</body>

</html>

turing % gcc debr.c -o debr

turing % gcc detag.c -o detag

turing % cat test.html |debr |detag

This Page

Some text on the page

Big text

Please submit your two C source code programs and record a script of the

programs being compiled and then running on some test file.

參考答案:

可以这样写:

/* debr.c */

#include <stdio.h>

void main( ) {

char buf[ 5 ] = { 0 };

while ( ( buf[ 0 ] = getchar( ) ) != EOF )

if ( buf[ 0 ] == '<' ) {

scanf( "%3c", buf + 1 );

printf( "%s", buf );

if ( buf[ 1 ] == 'b' && buf[ 2 ] == 'r' && buf[ 3 ] == '>' )

puts( "" );

}

else

putchar( buf[ 0 ] );

}

################################################################################

################################################################################

/* detag.c */

#include <stdio.h>

void main( ) {

char c;

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

if ( c == '<' )

while ( ( c = getchar( ) ) != '>' )

;

else

putchar( c );

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航