第十六周上机实践——项目6-处理C++源代码的程序
来源:互联网 发布:淘宝卖家怎么联系淘客 编辑:程序博客网 时间:2024/05/21 20:17
/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作 者:郭永恒 *完成日期:2016年6月27日 *版 本 号:v1.0 * *问题描述:写一个处理c++源代码的程序,可以判断main函数个数,让花括号独占一行,可以去除注释或者注释一部分 */
Process.h:
#ifndef PROCESS_H_INCLUDED#define PROCESS_H_INCLUDED#include <vector>#include <string>using std::string;using std::vector;class Process{public: Process(); ~Process(); void deleteComment(); void insertALineBreak(); void commentSection(); void display();private: void read(); void write(); bool notSpace(string&); vector<string> codes; string filename;};#endif // PROCESS_H_INCLUDED
#include "Process.h"#include <cctype>#include <fstream>#include <string>#include <iostream>#include <cstdlib>using namespace std;Process::Process(){ cout << " 请输入文件名:"; cin >> filename; ifstream infile(filename); int main_num = 0; if(infile) { while(!infile.eof()) { string str; getline(infile,str); if(str.find("main()") != -1 && str.find("main()") != str.rfind("main()")) { cout << " 含有多个main函数" << endl; exit(1); } else if(str.find("main()") != -1) ++main_num; if(main_num > 1) { cout << " 含有多个main函数" << endl; exit(1); } codes.push_back(str); } } else { cout << " 打开错误" << endl; exit(1); } infile.close();}Process::~Process(){ ofstream outfile("Result.cpp"); for(string& str : codes) if(notSpace(str)) outfile << str << endl; outfile.close();}void Process::insertALineBreak(){ int time = 0; for(string& str : codes) { int flag; if((flag = str.find("{")) != -1) { if(flag > 0 && str[flag-1] != '\n') str.insert(flag,"\n"); } if((flag = str.find("}")) != -1) { if(flag < str.size() && flag > 0 && str[flag-1] != '\n') str.insert(flag,"\n"); } ++time; } write(); read();}void Process::commentSection(){ int n,m; cout << " 请输入n和m,注释n行和m行之间的部分(包括n行和m行):" << endl; cin >> n >> m; codes.insert(codes.begin()+n,string("/*")); codes.insert(codes.begin()+m+1,string("*/")); write(); read();}void Process::deleteComment(){ int flag,time = 0,sstart,send; for(string& str : codes) { if((flag = str.find("//")) != -1) str.erase(flag); else if((flag = str.find("/*")) != -1) { str.erase(flag); sstart = time; } else if((flag = str.find("*/")) != -1) { str.replace(0,flag+2,""); send = time; for(vector<string>::iterator s = codes.begin()+sstart+1; s != codes.begin() + send; ++s) (*s).clear(); } ++time; } write(); read();}bool Process::notSpace(string& str){ int judge = 0; for(char& c : str) if(isalpha(c) || isdigit(c) || ispunct(c)) return true; return false;}void Process::display(){ int time = 1; for(string& str : codes) if(notSpace(str)) { cout << time << "\t"; cout << str << endl; ++time; }}void Process::read(){ codes.clear(); ifstream infile("Result.cpp"); if(infile) { while(!infile.eof()) { string str; getline(infile,str); codes.push_back(str); } } infile.close();}void Process::write(){ ofstream outfile("Result.cpp"); for(string& str : codes) if(notSpace(str)) outfile << str << endl; outfile.close();}
main函数:
#include <iostream>#include "Process.h"using namespace std;int main(){ Process pro; cout << " 处理前:" << endl; pro.display(); pro.insertALineBreak(); pro.deleteComment(); cout << " 处理后:" << endl; pro.display(); return 0;}
运行结果:
程序缺陷太多了
0 0
- 第十六周上机实践——项目6-处理C++源代码的程序
- 第十六周上机实践——项目4-用二进制文件处理学生成绩
- 第十六周上机实践项目——指来指去(1)
- 第十六周上机实践项目——指来指去(2)
- 第十六周上机实践项目——填空学指针
- 第十六周上机实践项目——有相同数字?
- 第十六周上机实践项目——奇数因子
- 第十六周上机实践——项目3-电子词典
- 第十六周上机实践——项目5-二进制文件浏览器
- 第十六周上机实践项目1(1):阅读程序
- 第十六周上机实践项目1(2):阅读程序
- 第十六周上机实践项目1(3):阅读程序
- 第十六周上机实践项目2(1):阅读程序
- 第十六周上机实践项目2(2):阅读程序
- 第十六周上机实践项目3(1):阅读程序
- 第十六周上机实践项目3(2):阅读程序
- 第十六周上机实践项目3(3):阅读程序
- 第十六周上机实践—项目3—归并排序算法的改进
- MySql学习记录
- PHPUnit安装
- CentOS 6.x系统 ipmitool工具
- csv大文件分割以及添加表头
- 前端界面优化
- 第十六周上机实践——项目6-处理C++源代码的程序
- 淘宝网站前端分析
- 移动端web页面知识小结之CSS部分
- xpages获得当前页面url及其参数
- 办公室: 如何把“敌意”同事变成朋友?
- 解决腾讯云重启postfix服务失败的问题
- 【JAVA高并发秒杀API之DAO层】课程笔记
- Java基础for循环回顾
- 003-生成排列-归纳法-《算法设计技巧与分析》M.H.A学习笔记