求教c++高手的问题,在线等! 急~~~!!~!~~

keboye 2009-06-11 07:42:13
#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语言又没学好。。。。。。
...全文
38 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanchangyong 2009-06-12
  • 打赏
  • 举报
回复
lz,你想说的是?
shuashua0 2009-06-11
  • 打赏
  • 举报
回复
你最少应该把问题说清楚啊
xuexi1028 2009-06-11
  • 打赏
  • 举报
回复
..............................怎么都喜欢发大篇的程序呢 ,,我敢保证 这个程序
LZ 估计自己都没看 不知道从哪儿下来的 编译不过 ,,就发过来了 ,,,,,BS 你这种人 ,,,问问题 应该就问题 问问题 自己都不知道怎么回事 就乱搞
oneOO8 2009-06-11
  • 打赏
  • 举报
回复
你要问什么?
是程序实现什么功能吗?
oneOO8 2009-06-11
  • 打赏
  • 举报
回复
你要问什么?
是程序实现什么功能吗?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧