处理C++源代码的程序

  1. /* 
  2. *Copyright (c) 2016,烟台大学计算机学院 
  3. *All rights reserved. 
  4. *文件名称:main.cpp 
  5. *作    者:李德彪 
  6. *完成时间:2016年6月23日 
  7. *版 本 号:v1.0 
  8. * 
  9. *问题描述:项目 - 处理C++源代码的程序。 
  10. *输入描述:无。 
  11. *程序输出:显示程序。 
  12. */  
  13.   
  14. #include <fstream>    
  15. #include<iostream>    
  16. //#include<string>    
  17. #include<cstdlib>    
  18. using namespace std;    
  19. void outprogram(char *filename);    
  20. int main( )    
  21. {    
  22.     char ch1,ch2;    
  23.     //将文件中的数据读入到字符数组中    
  24.     ifstream sourceFile("source.cpp",ios::in);  //以输入的方式打开文件    
  25.     if(!sourceFile)       //测试是否成功打开    
  26.     {    
  27.         cerr<<"source code read error!"<<endl;    
  28.         exit(1);    
  29.     }    
  30.     ofstream outFile("newsource.cpp",ios::out);  //以输出的方式打开文件    
  31.     if(!outFile)       //测试是否成功打开    
  32.     {    
  33.         cerr<<"new source code write error!"<<endl;    
  34.         exit(1);    
  35.     }    
  36.     
  37.     ch1='\0';    
  38.     while(!sourceFile.eof())    
  39.     {    
  40.         sourceFile.get(ch2);    
  41.         //读到了花括号,且前一个符号不是换行,应该加入一个换行    
  42.         if((ch2=='{'||ch2=='}')&&(ch1!='\n'))    
  43.             outFile.put('\n');    
  44.         else    
  45.             //当前读到的不是换行,但前一个是花括号,此时也该加    
  46.             if((ch1=='{'||ch1=='}')&&(ch2!='\n'))    
  47.                 outFile.put('\n');    
  48.         outFile.put(ch2); //输出当前读入的符号    
  49.         ch1=ch2;    
  50.     }    
  51.     outFile.close();    
  52.     sourceFile.close();    
  53.     cout<<"经过处理后的源程序是:"<<endl;    
  54.     outprogram("newsource.cpp");    
  55.     return 0;    
  56. }    
  57.     
  58. void outprogram(char *filename)    
  59. {    
  60.     char line[256];    
  61.     int n = 1;    
  62.     ifstream inFile(filename, ios::in);  //以输入的方式打开文件    
  63.     if(!inFile)       //测试是否成功打开    
  64.     {    
  65.         cerr<<"file open error!"<<endl;    
  66.         exit(1);    
  67.     }    
  68.     while (!inFile.eof())    
  69.     {    
  70.         inFile.getline(line,255,'\n');    
  71.         cout<<n<<'\t'<<line<<endl;    
  72.         n++;    
  73.     }    
  74.     inFile.close();    
  75.     return;    
  76. }    
  77. /*
    *Copyright (c) 2016,烟台大学计算机学院
    *All rights reserved.
    *文件名称:main.cpp
    *作    者:李磊涛
    *完成时间:2016年6月23日
    *版 本 号:v1.0
    *
    *问题描述:项目 - 处理C++源代码的程序。
    *输入描述:无。
    *程序输出:显示程序。
    */
    
    #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;  
    }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值