第十六周 项目五:编程处理C++源代码(5.2:使花括号独占一行)

来源:互联网 发布:android 视频通话 源码 编辑:程序博客网 时间:2024/05/17 07:40

问题及代码:

/** Copyright (c) 2014, 烟台大学计算机学院* All rights reserved.* 文件名称:Project4.cpp* 作    者:李楠* 完成日期:2014年12月14日* 版 本 号:v1.0** 问题描述:读入一个程序,编程使”{“和”}“独占一行,并保存在新的CPP中* 输入描述:略* 程序输出:略*/#include <iostream>#include <fstream>#include <cstdlib>using namespace std;int main( ){    char ch1,ch2;    ch1='\0';    ifstream sourcefile("source.cpp",ios::in);    if(!sourcefile)    {        cerr<<"open error!"<<endl;        exit(1);    }    ofstream outfile("newsource.cpp",ios::out);    if(!sourcefile)    {        cerr<<"newsource open error!"<<endl;        exit(1);    }    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();    return 0;}



运行结果:

修改之前:

修改之后:

 

 

知识点总结:

一、这个程序不仅要读入,还要输出到新的cpp中;

二、if((ch2=='{'||ch2=='}')&&(ch1!='\n'))
            outfile.put('\n');
        else if((ch1=='{'||ch1=='}')&&(ch2!='\n'))
            outfile.put('\n');

这一段程序的意思是:读到了花括号,且前一个不是换行,要加一个换行。

                                        当前读到的不是换行,但前一个是花括号,此时也应加一个换行。

三、疑问疑问疑问!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        我能大概知道 outfile.put('\n'); 类似语句的意思,但是我度娘不告诉我这是什么啊!!!??

        好心人路过告诉我~~~

学习心得:

老师在写这个程序的时候还让 源程序输出了 ,我比较懒 就直接让他回到 newsource.cpp中去看了,就没有输出...

0 0