C++第16周项目4 -处理C++源代码 - (2)花括号单独占一行

来源:互联网 发布:淘宝的等级划分 编辑:程序博客网 时间:2024/04/30 04:37

课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/9078413


【项目4】写处理C++源代码的程序

   (2)读入一个C++程序,使程序中的所有左花括号“{”和右花括号“}”都单独占一行,新程序保存到另一个.cpp文件中,并在屏幕上显示处理过的程序,显示时加上行号。

参考解答:

#include <fstream>#include<iostream>//#include<string>#include<cstdlib>using namespace std;void outprogram(char *filename);int main( ){    char ch1,ch2;    //将文件中的数据读入到字符数组中    ifstream sourceFile("source.cpp",ios::in);  //以输入的方式打开文件    if(!sourceFile)       //测试是否成功打开    {        cerr<<"source code read error!"<<endl;        exit(1);    }    ofstream outFile("newsource.cpp",ios::out);  //以输出的方式打开文件    if(!outFile)       //测试是否成功打开    {        cerr<<"new source code write error!"<<endl;        exit(1);    }    ch1='\0';    while(!sourceFile.eof())    {        sourceFile.get(ch2);        //读到了花括号,且前一个符号不是换行,应该加入一个换行        if((ch2=='{'||ch2=='}')&&(ch1!='\n'))            outFile.put('\n');        else            //当前读到的不是换行,但前一个是花括号,此时也该加            if((ch1=='{'||ch1=='}')&&(ch2!='\n'))                outFile.put('\n');        outFile.put(ch2); //输出当前读入的符号        ch1=ch2;    }    outFile.close();    sourceFile.close();    cout<<"经过处理后的源程序是:"<<endl;    outprogram("newsource.cpp");    return 0;}void outprogram(char *filename){    char line[256];    int n = 1;    ifstream inFile(filename, ios::in);  //以输入的方式打开文件    if(!inFile)       //测试是否成功打开    {        cerr<<"file open error!"<<endl;        exit(1);    }    while (!inFile.eof())    {        inFile.getline(line,255,'\n');        cout<<n<<'\t'<<line<<endl;        n++;    }    inFile.close();    return;}



原创粉丝点击