Code Formatter
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
#
#
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.
统计’\t’ 和 尾随’ ‘的个数~(一次换行也算四次‘ ’,值得注意的是“##”代表此次输出结束~而不是此题输出结束)~此题必须以gets()输入~而不能用scanf()~不理解的同学~看完下面你就·懂了~
普及下~gets()输入和scanf()输入的区别~
都是从输入流中读取数据,但功能有很大差别:
1 操作类型不同。
gets函数仅用于读入字符串。
scanf为格式化输出函数,可以读入任意C语言基础类型的变量值,而不是仅限于字符串(char*)类型。
2 截止字符不同。
gets函数固定的以换行符作为结尾,遇到换行符时结束输入。
scanf函数默认以空白函数结尾,同时可以对截止函数进行修改。
3 对截止字符处理不同。
gets函数会读入截止字符\n, 同时将\n自动替换为\0.
scanf遇到截止字符时不会继续读取,截止字符将存储于输入缓冲中。
4 返回值类型不同。
gets的返回值为char*型,当读入成功时会返回输入的字符串指针地址,出错时返回NULL。
scanf返回值为int型,返回实际成功赋值的变量个数,当遇到文件结尾标识时返回EOF。
这也是为什么gets() 常于 getchar() 连用的原因~~
输入格式弄懂了~其他就好办了~
AC代码:
#include<cstdio>
#include<cstring>
using namespace std;
char st[200010];
int main()
{
int T;
scanf("%d",&T);
getchar();
while(T--){
int cut = 0,sum = 0,ans = 0;
while(gets(st),strcmp(st,"##") != 0){
int nl = strlen(st);
for(int i = sum = 0 ; i < nl ; i++){
if(st[i] == '\t'){
cut++;
sum += 4;
continue;
}
if(st[i] == ' '){
sum++;
continue;
}
sum = 0;
}
ans += sum;
}
printf("%d tab(s) replaced\n%d trailing space(s) removed\n",cut,ans);
}
return 0;
}