/*
*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;
}
运行结果:
程序缺陷太多了