zoj 2851 Code Formatter
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.
Author: YANG, Chao
Source: Zhejiang Provincial Programming Contest 2007
//1860042 2009-05-08 15:46:09 Wrong Answer 2851 C++ 0 184 Wpl
//60341 2009-05-08 19:17:24 Segmentation Fault 2851 C++ 0 0 Wpl
//1860479 2009-05-08 20:20:49 Accepted 2851 C++ 0 184 Wpl
#include <iostream>
#include <string>
using namespace std;
string str;
int t,Tnum,Bnum;
void Process()
{
int temp,j;
while(getline(cin,str,'\n'))
{
if(str=="##")
return ;
temp=0;
for(j=0;j!=str.size();j++)
{
if(str[j]=='\t')
{
Tnum++;
temp+=4;
continue;
}
if(str[j]==' ')
{
temp++;
continue;
}
temp=0;
}
Bnum+=temp;
}
}
int main()
{
cin>>t;
getchar();
while(t--)
{
Tnum=0;
Bnum=0;
getchar(); //因为这里吃回车,所以错误了好多次.5555
Process();
printf("%d tab(s) replaced\n",Tnum);
printf("%d trailing space(s) removed\n",Bnum);
}
return 0;
}