项目 - 处理C++源代码的程序
2016-06-23 21:44
162 查看
问题及代码:
/*
*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;
}
运行结果:
知识点总结:
通过该程序,强化了我对简单程序结构的认识。
学习心得:
期间有很多小错误,要继续写程序争取早日掌握C++。
问题及代码:
运行结果:
知识点总结:
通过该程序,强化了我对简单程序结构的认识。
学习心得:
期间有很多小错误,要继续写程序争取早日掌握C++。
/*
*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;
}
运行结果:
知识点总结:
通过该程序,强化了我对简单程序结构的认识。
学习心得:
期间有很多小错误,要继续写程序争取早日掌握C++。
问题及代码:
运行结果:
知识点总结:
通过该程序,强化了我对简单程序结构的认识。
学习心得:
期间有很多小错误,要继续写程序争取早日掌握C++。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- 使用 Libki 来管理公共用户访问计算机
- c++ primer 第五版 笔记前言
- 微型计算机的始祖:Altair 8800
- share_ptr的几个注意点
- 通过手机、电脑远程开关机,Windows和linux机手机,电脑相互控制
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例