求教c++高手的问题,在线等! 急~~~!!~!~~
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class CppfileFilter
{
public:
CppfileFilter(istream * i=0, ostream * o1=0, ostream * o2=0);
bool Work();
string::size_type LineNumber() const;
private:
istream *from;
ostream *to1,*to2;
string Line;
string::size_type pos,size,LineNum;
enum Status{Normal,DivStart,DivDiv,Quotation,DoubleQuotation,Finish};
Status S;
void NewLine();
void NormalFunc();
void DivStartFunc();
void DivDivFunc();
void QuotationFunc();
void DoubleQuotationFunc();
typedef void (CppfileFilter::*ptrf)();
ptrf array[5];
};
CppfileFilter::CppfileFilter(istream* i,ostream* o1,ostream* o2)
{
from=i;
to1=o1;
to2=o2;
S=Normal;
array[0]=&CppfileFilter::NormalFunc;
array[1]=&CppfileFilter::DivStartFunc;
array[2]=&CppfileFilter::DivDivFunc;
array[3]=&CppfileFilter::QuotationFunc;
array[4]=&CppfileFilter::DoubleQuotationFunc;
}
string::size_type CppfileFilter::LineNumber() const
{
return LineNum;
}
bool CppfileFilter::Work()
{
LineNum=0;
if(!from || !to1 && !to2) return false;
getline(*from,Line);
pos=0;
size=Line.size();
++LineNum;
while(true){
if(S==Finish) return false;
if(!*from) return true;
(this->*array[S])();
}
}
void CppfileFilter::NewLine()
{
if(getline(*from,Line)){
pos=0;
size=Line.size();
++LineNum;
if(to1) *to1<<'\n';
if(to2) *to2<<'\n';
}
}
void CppfileFilter::NormalFunc() //正常状态
{
if(Line.empty()||pos==size){
NewLine();
}
else{
for(;pos<size;++pos){
if(Line[pos]=='/' && pos+1<size){
if(Line[pos+1]=='*'){
if(to2) *to2<<Line[pos]<<Line[pos+1];
pos+=2;
S=DivStart;
return;
}
if(Line[pos+1]=='/'){
if(to2) *to2<<Line[pos]<<Line[pos+1];
pos+=2;
S=DivDiv;
return;
}
}
else if(Line[pos]=='"'){
if(to1) *to1<<Line[pos];
++pos;
S=DoubleQuotation;
return;
}
else if(Line[pos]=='\''){
if(to1) *to1<<Line[pos];
++pos;
S=Quotation;
return;
}
if(to1) *to1<<Line[pos];
}
}
}
void CppfileFilter::DivStartFunc() //进入 /*的状态
{
for(;pos<size;++pos){
if(to2) *to2<<Line[pos];
if(Line[pos]=='*' && pos+1<size && Line[pos+1]=='/'){
if(to2) *to2<<Line[pos+1];
pos+=2;
S=Normal;
return;
}
}
if(pos==size) NewLine();
}
void CppfileFilter::DivDivFunc() //进入 //的状态
{
for(;pos<size;++pos){
if(to2) *to2<<Line[pos];
}
S=Normal;
}
void CppfileFilter::QuotationFunc() //进入 '的状态
{
if(Line[pos]=='\\' && pos+2<size && Line[pos+2]=='\''){
if(to1) *to1<<Line[pos]<<Line[pos+1]<<Line[pos+2];
pos+=3;
S=Normal;
return;
}
else if(Line[pos]!='\\' && pos+1<size && Line[pos+1]=='\''){
if(to1) *to1<<Line[pos]<<Line[pos+1];
pos+=2;
S=Normal;
return;
}
S=Finish;
}
void CppfileFilter::DoubleQuotationFunc() //进入 "的状态
{
for(;pos<size;++pos){
if(to1) *to1<<Line[pos];
if(Line[pos]=='"' && Line[pos-1]!='\\'){
++pos;
S=Normal;
return;
}
}
S=Finish;
}
vector<string> level0, level1,level2;
int anay_code(char *path) {
ifstream fin(path);
if(!fin.is_open() ){
return -1;
}
string str;
while (getline(fin, str)) {
size_t pos = str.find("class");
if(string::npos != pos) {
string class_name = "";
pos += 4;//跳过class
while(str[++pos] == ' ');
while(isalpha(str[pos])) {
class_name += str[pos];
++pos;
}
if(str.find("public") != string::npos || str.find("private") != string::npos) {
level1.push_back(class_name);
}
else {
level0.push_back(class_name);
}
}
size_t pos1 = str.find("::");
if(string::npos != pos1)
{
string class_name = "";
pos1+=2;
while(isalpha(str[pos1])) {
class_name+= str[pos1];
++pos1;
}
level2.push_back(class_name); }
}
cout << "基类:" << endl;
for (int i = 0; i < level0.size(); ++i) {
cout << level0[i] << " ";
}
cout << endl;
cout << "派生:" << endl;
for ( i = 0; i < level1.size(); ++i) {
cout << level1[i] << " ";
}
cout << endl;
return 0;
}
int main()
{
cout << "Enter file name: ";
string name;
cin >> name;
ifstream inf(name.c_str());
ofstream outf1("the file without annotatiaon.cpp");
ofstream outf2("annotation.txt");
CppfileFilter T(&inf,&outf1,&outf2);
if(T.Work()==false) cout<<"error at line#"<<T.LineNumber(); //输出错误的注释所在
return 0;
anay_code("comment.cpp");
system("end");
}
有点乱,希望大大们帮我看下~~~~ C++ 好难哦~~ C语言又没学好。。。。。。