2013-BIT程序设计 8.逻辑行计数 -- 模拟

8. 逻辑行计数

背景

有一个软件公司常常用一种不为人知的语言来开发程序,这个语言的特点如下:

  1. 有两种形式的字符串,一种为加单引号,另一种加双引号。单引号的字符串可以包含双引号,双引号的字符串也可以包含单引号。字符串不能分行,其中也不能包含同样的引号字符。
  2. 有两种注释方式:@字符为行注释,而双括号(())内的文本为块注释。
  3. 块注释不能嵌套,所有在块中的文本均被忽略。一个块可以包含几行文本。
  4. 注释不能出现在字符串内。
  5. 在块注释中的行注释字符和引号都是没有意义的。
  6. 注释行中的双括号和引号都是没有意义的。
  7. 程序的任何部分都不能出现“#”字符;即使在字符串或注释内。
  8. 分号用来终止语句和生命。每个不在注释或字符串中的分号都被作为程序逻辑行的结束。

可以用逻辑行数来粗略地评估程序的大小,即计算不在注释或字符串内的分号的个数。写一个程序读进去几组程序代码,对每组代码都输出逻辑行数和物理行数,并对未终止的注释块和字符串发出警告消息。

输入

输入包含一个或多个程序,以#表示每个程序终止,以##表示输入终止。

输出

对于包括未终止字符串的行,输出:“Unterminated string in line n.”,其中n换成行号。

如果该程序包括一个未终止的块注释,以如下格式输出一行:”Unterminated block comment at end of program.“

在错误信息之后,以如下格式输出一行:”Program x contains y logical lines and z physical lines.“。其中x、y和z用相应的数字代替。

 

测试用例 1 以文本方式显示
1.((Block comment:))↵
2."string";('another string;');@line comment↵
3.##↵
 以文本方式显示
1.Program 1 contains 2 logical lines and 2 physical lines.↵
 1秒 64M 0 


自己做这道题的时候写过一个帖子,这里就偷懒直接贴上来吧:

(先来英文原题)ps:原题意实在难以理解。。。

Logical Line Counting
(This program is slightly modified from a program used in a regional ACM Programming Contest competition.)
A software development company develops programs using a little known programming language with the following features:

There are two styles of string. A string may be enclosed in 'single quotes' or in "double quotes". If a string is enclosed in double quotes, it can contain single quote characters, and if a string is enclosed in single quotes it can contain double quote characters.
*However, strings are not broken over a line, and cannot contain the same quote character used to enclose it.

There are two styles of comment. All text from an @ character to the end of line is a line comment, and all text within ((double parentheses)) is a block comment.
*Block comments are not nested.
*All open parentheses inside a block comment are ignored.
*A block comment can extend over several lines.
*Comments cannot occur inside a string.
*Line comment characters or quotes inside a block comment have no significance.
*Double parentheses or quotes inside a line comment have no significance.
A semi-colon is used to terminate statements and declarations. Every semi-colon not in a comment or a string is thus considered to terminate a logical line of the program.
A rough estimate for the size of a program can be made by counting logical lines, which means *counting the semi-colons that do not appear inside a comment or string.
Write a program that reads in a program, counts the number of logical lines and the number of physical lines and issues warning messages for unterminated strings and unterminated block comments.

For each line containing an unterminated string, output a line of the form:

Unterminated string in line #.

where # is the physical line number within the program. Subsequent text must be handled as though the string had been terminated at the end of the line. If the program contains an unterminated block comment, output a line of the form:
Unterminated block comment at end of program.

After error messages, if any, output a line of the form:
Program # contains # logical lines and # physical lines.

Output will be as specified above. It must have exactly the form shown, with no extra spaces around numeric output, and with a full stop at the end of each line and with no blank lines.
值得再提的是原题对于输入的终止方式不是以‘#’和“##”结束。我一直错在第四组用例上的原因也是没有太注意‘#’和“##”的差别(感谢汉神,潘神,学长,小天,当然还有业界良心的红领巾同志,O(∩_∩)O~谢谢)。
注意:
1. ‘#’和“##”不一定单独成行,还可以在输入的一些字符之后,作为输入的结束。
所以就需要在对整个串扫描的时候,时刻判断是否=='#',如果==,就接着判断下一位是否=='#',找到了“##”就可以输出错误信息,然后直接停止了(我的错误在于没有及时停止,导致输出了多行)
附用例:
((;;
;;
##
Unterminated block comment at end of program.
Program 1 contains 0 logical lines and 2 physical lines.
((;;
;;##
Unterminated block comment at end of program.
Program 1 contains 0 logical lines and 1 physical lines.
上面两个用例还可以说明的是*‘#’和“##”所在的那一行不算是physical line。
而且如果只输入单独的一行
#
输出:
Program 1 contains 0 logical lines and 0 physical lines.
2.为了方便调试,每次想出新测试用例的时候,大家都会'#'去结束一次输入,然后接着输入。有时候‘#’和“##”的不同在* 字符串,块注释 里可能会出现不同的结果(也许这就是bug所在)但大家会说如果输入了“##”,有些编译器就会直接停止了,看不到结果。有一个小方法:在头文件里加上一个#include<stdlib.h>,然后在程序结束的return 0之前加上一行system("pause");这样就可以看到输入“##”后的结果了。
3.建议大家在出现了错误的时候,自己多想用例,用例应该是有多种类型,不要只想一种类型的例子。
e.g:
";;
@;
((#
";;
@;;
#
';;
@;;
#
((;;
@;;
;;
#
((;;#
((;;
';;"sdf";;
;@sdg;;
#
((;;
';;"sdf";;
;;));;
;@sdg;;
##
Unterminated string in line 1.
Unterminated block comment at end of program.
Program 1 contains 0 logical lines and 2 physical lines.
Unterminated string in line 1.
Program 2 contains 0 logical lines and 2 physical lines.
Unterminated string in line 1.
Program 3 contains 0 logical lines and 2 physical lines.
Unterminated block comment at end of program.
Program 4 contains 0 logical lines and 3 physical lines.
Unterminated block comment at end of program.
Program 5 contains 0 logical lines and 0 physical lines.
Unterminated block comment at end of program.
Program 6 contains 0 logical lines and 3 physical lines.
Program 7 contains 3 logical lines and 4 physical lines.

 

注意:不要用gets(),会超内存,用getchar()更好。

 

代码:
(暂时隐藏)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值