ZOJ  2851 Code Formatter

本文介绍了一款简单的代码格式化工具的设计与实现过程。该工具主要用于替换源代码中的制表符为四个空格,并移除行尾空白字符,旨在帮助程序员保持一致的代码风格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Code Formatter p 
Time Limit: 1000MS   Memory Limit: 32768K   64bit IO Format: %lld & %llu  

[Submit]   [Go Back]   [Status] 

Description

Some companies have special requirements for source code format, and it is also good for programmers to keep consistent code style. You are asked to write a simple code formatter for the company to help the poor programmers. 

The first thing you need to do is to check whether the source code contains tabs (represented as the escape character '\t'), since different terminals have different ways to display tabs, it's better not to use them, but replace them with spaces. The code formatter should replace each tab of the source code with 4(four) blank spaces. 

Then you need to remove trailing spaces of the source file. Trailing spaces are one or more consecutive whitespaces right before the EOL (end of line, represented as the escape character '\n'), and they usually have no meaning in most programming language, so they can be safely removed. 


Input


The input contains multiple test cases! 

The first line is an integer N indicating the number of test cases. Each test case is the source which contains not more than 100 lines given to you to format. A single line containing only "##" marks the end of a test case. 


Output


For each test case, output a log of the formatter in two lines of the following format: 

#A tab(s) replaced #B trailing space(s) removed Where #A is the number of tabs replaced and #B is the number of trailing spaces removed. 


Sample Input


2
include <stdio.h>

int main()
{
int a,b;
while(scanf("%d %d",&a, &b) != EOF) 
printf("%d\n",a+b);             
        
}
##

##

Sample Output


4 tab(s) replaced
22 trailing space(s) removed
0 tab(s) replaced
0 trailing space(s) removed


Note

In order to show the whitespaces precisely, all the characters in sample input are underlined. They are not the underscore character.

这题题意就是求tab space的数量和每行最后空格的数量。但是要注意,求空格数量的时候可是要把tab space转换成四个空格,然后再判断空格的数量的,切记啊
代码:
#include<stdio.h>
#include<string.h>
char map[202][10002];
int main()
{
int num,t,num1,num2,i,j,k;
scanf("%d",&t);getchar();
while(t--)
{
num1=num2=0;
i=0;
while(gets(map[i]),strcmp(map[i],"##")!=0)
i++;
num=i;
for(i=0;i<num;i++)
{
k=0;
for(j=0;map[i][j];j++)
if(map[i][j]==9)
{
num1++;
k++;
}
else k=0;
num2+=k*4;
}
for(i=0;i<num;i++)
{
k=0;
for(j=0;map[i][j];j++)
if(map[i][j]==32)
k++;
else k=0;
num2+=k;
}
printf("%d tab(s) replaced\n",num1);
printf("%d trailing space(s) removed\n",num2);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值