读入一个C++程序,使程序中的所有左花括号“{”和右花括号“}”都单独占一行,新程序保存到另一个.cpp文件中,并在屏幕上显示处理过的程序,显示时加上行号。
这个程序的核心其实就是:括号前后如何处理。学习两个连续字符处理程序的方法
原文答案的思路更清晰有条理。。
我为什么能这么写是因为只是单纯的考虑了单个‘{’,并没有整体考虑,答案属于整体综合考虑了,所以比较简练。。
#include<iostream>
#include<vector>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<list>
#include<cstring>
#include<string>
#include<deque>
using namespace std;
int main()
{
ifstream fin;
fin.open("source.cpp");
if(!fin){cerr<<"Open error..."<<endl;exit(1);}
char ch2,ch3;
ch2='\0';
ofstream fout;
fout.open("source5.cpp");
while(fin.get(ch3))
{
if(('{'==ch3&&ch2!='\n')||('{'==ch2&&ch3!='\n'))//花括号的前面或者后面不是换行的时候就增加一个换行符
{cout<<endl<<ch3;fout<<endl<<ch3;}
// else if('{'==ch2&&ch3!='\n')
// {cout<<endl<<ch3;fout<<endl<<ch3;}
else if(('}'==ch3&&ch2!='\n')||('}'==ch2&&ch3!='\n'))
{cout<<endl<<ch3;fout<<endl<<ch3;}
// else if('}'==ch2&&ch3!='\n')
// {cout<<endl<<ch3;fout<<endl<<ch3;}
else
{cout<<ch3;fout<<ch3;}
ch2=ch3;
}
fin.close();
fout.close();
return 0;
}