14. 逻辑行计数

高老师发明了一种新的语言来开发程序,这个语言的特点如下:

  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用相应的数字代替。


测试输入关于“测试输入”的帮助期待的输出关于“期待的输出”的帮助时间限制关于“时间限制”的帮助内存限制关于“内存限制”的帮助额外进程关于“{$a} 个额外进程”的帮助
测试用例 1以文本方式显示
  1. ((Block comment:))↵
  2. "string";('another string;');@line comment↵
  3. ##↵
以文本方式显示
  1. Program 1 contains 2 logical lines and 2 physical lines.↵
1秒1024KB0


#include<stdio.h>
#include<string.h>

int main()
{
	char ss = NULL, s1 = NULL, s2 = NULL, s3 = NULL, s4 = NULL, s5 = NULL, s6 = NULL, s7 = NULL, s8 = NULL, s9 = NULL, ss10 = NULL, ss11 = NULL, ss12 = NULL;
	int m;
	int ctx = 0, ctw = 0, ctj = 0;
	while (1)
	{
		m = 0;
		ss = getchar();
		k:;		
		if (ss == '\'')
		{
			while (1)
			{
				s1 = getchar();
				if (s1 == '\n')
				{
					ctw++;
					printf("Unterminated string in line %d.\n", ctw);
					break;
				}
				else if (s1 == '\'')
					break;
				else if (s1 == '#')
				{
					ctx++;
					printf("Unterminated string in line %d.\n", ctw);
					printf("Program %d contains %d logical lines and %d physical lines.\n", ctx, ctj, ctw);
					ctj = 0;
					ctw = 0;
					s2 = getchar();
					if (s2 == '\n')
					{
						s2 = getchar();
						ss = s2;
						goto k;
					}
					else if (s2 == '#')
					{
						m = 1;
						break;
					}
					else
					{
						ss = s2;
						goto k;
					}
				}
			}
		}
		else if (ss == '\"')
		{
			while (1)
			{
				s1 = getchar();
				if (s1 == '\n')
				{
					ctw++;
					printf("Unterminated string in line %d.\n", ctw);
					break;
				}
				else if (s1 == '\"')
					break;
				else if (s1 == '#')
				{
					ctx++;
					printf("Unterminated string in line %d.\n", ctw);
					printf("Program %d contains %d logical lines and %d physical lines.\n", ctx, ctj, ctw);
					ctw = 0;
					ctj = 0;
					ss12 = getchar();
					if (ss12 == '\n')
					{
						ss12 = getchar();
						ss = ss12;
						goto k;
					}
					else if (ss12 == '#')
					{
						m = 1;
						break;
					}
					else
					{
						ss = ss12;
						goto k;
					}
				}
				else
					;
			}
		}
		else if (ss == '@')
		{
			while (1)
			{
				s3 = getchar();
				if (s3 == '\n')
				{
					ctw++;
					break;
				}
				else if (s3 == '#')
				{
					ctx++;
					printf("Program %d contains %d logical lines and %d physical lines.\n", ctx, ctj, ctw);
					ctj = 0;
					ctw = 0;
					s4 = getchar();
					{
						if (s4 == '\n')
						{
							s4 = getchar();
							ss = s4;
							goto k;
						}
						else if (s4 == '#')
						{
							m = 1;
							break;
						}
						else
						{
							ss = s4;
							goto k;
						}
					}
				}
				else
					;
			}
		}
		else if (ss == '(')
		{
			s5 = getchar();
			if (s5 == '(')
			{
				while (1)
				{
					s6 = getchar();
					if (s6 == '\n')
						ctw++;
					else if (s6 == '#')
					{
						ctx++;
						printf("Unterminated block comment at end of program.\n");
						printf("Program %d contains %d logical lines and %d physical lines.\n", ctx, ctj, ctw);
						ctj = 0;
						ctw = 0;
						s7 = getchar();
						if (s7 == '\n')
						{
							s8 = getchar();
							ss = s8;
							goto k;
						}
						else if (s7 == '#')
						{
							m = 1;
							break;
						}
						else
						{
							ss = s7;
							goto k;
						}
					}
					else if (s6 == ')')
					{
						s9 = getchar();
						if (s9 == ')')
							break;
						else
						{
							ss = s9;
							goto k;
						}
					}
				}
			}
			else
			{
				ss = s5;
				goto k;
			}
		}
		else if (ss == ';')
			ctj++;
		else if (ss == '\n')
			ctw++;
		else if (ss == '#')
		{
			ctx++;
			printf("Program %d contains %d logical lines and %d physical lines.\n", ctx, ctj, ctw);
			ctj = 0;
			ctw = 0;
			ss10 = getchar();
			if (ss10 == '\n')
			{
				ss11 = getchar();
				ss = ss11;
				goto k;
			}
			else if (ss10 == '#')
			{
				m = 1;
				break;
			}
			else
			{
				ss = ss10;
				goto k;
			}
		}
		else
			;
		if (m == 1)
			break;
	}

	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值