ADT

 

原来的ADT:

struct  Point
{
int Line;
int Queue;
Point 
*next;
}
;

struct  KeyWord
{
char Key[MAX_KEYSIAZE];
Point 
*m_KeyWordLine;
KeyWord 
*next;
}
;

后来自以为改进的ADT:
struct  Row
{
int row;
Row 
*next;
}
;

struct  Position
{
int column;
Row 
*row;
Position 
*next;
}
;

struct  Key
{
char key[MAX_KEYSIAZE];
Position 
*position;
Key 
*next;
}
;

后来实现的时候两个编译程序一对比,前面的ADT要远优于后面的ADT。运算结果一致。

首先体现在时间效率方面。对前面的ADT进行操作花费的时间是对后一ADT操作花费时间的将近1/4(在处理大型文本的时候这个比会更小),虽然前一数据结构空间开销比后一数据结构空间开销要大。由此我们在进行ADT设计的时候,更要综合考虑存储空间与时间效率的平衡.

 

源代码:

1.//Core.h

 


const   int  MAX_KEY  =   10 ;
const   int  MAX_KEYSIAZE  =   50 ;

struct  Point
{
int Line;
int Queue;
Point 
*next;
}
;

struct  KeyWord
{
char Key[MAX_KEYSIAZE];
Point 
*m_KeyWordLine;
KeyWord 
*next;
}
;

enum  State
{
Welcome,
InputK,
InputF,
Search
}
;


class  Core
{
public:
    Core(
void);
public:
    
    
~Core(void);
public:
    
void InputKey();

    
bool ReadFile();
    
    
void AnalysisCenter(ifstream &);
    
    
void IsMatch(ifstream & ,int ,int &); 

    
void KeyManege(stringint []);

    
void ShowResult();

    
void Falg(State);
   
private:
    KeyWord 
*Head;
  
    
int CurrentLine;

    
int CurrentNum;

    
int KeyCount;

    KeyWord 
*lp[MAX_KEY];

}
;

 

2.//Core.cpp

#include  " StdAfx.h "
#include 
" Core.h "

Core::Core(
void )
{
    Head 
= NULL;
    KeyCount 
= 0;
    Falg(Welcome);
}


Core::
~ Core( void )
{  
    Falg(Welcome);
    
if (NULL != Head)
    
{
        KeyWord 
*Current = Head;
        
        
while (Current != NULL)
        
{
            
if (Current->m_KeyWordLine != NULL)
            
{
                Point 
*= Current->m_KeyWordLine;
                
int count = 0;
                
int Position = 0;
                
while(p != NULL)
                
{
                    Point 
*delp = p;
                    p 
= p->next;
                    delete delp;
                }

            }

            
            KeyWord 
*delk = Current;
            Current 
= Current->next;
            delete delk;
        }

    }

    system(
"pause");
}




void  Core::InputKey()
{
    Falg(InputK);
    
    
char _key[MAX_KEYSIAZE];
    
    strcpy (_key, 
"Start");
    
    
int Word = 0;
    
while(1)
    
{  
        Word
++;
        cin.clear();
        cout 
<< "" << Word <<"个关键词->:";
        cin 
>> _key;

        
if (strcmp (_key, "love2008"!= 0)
        
{
            KeyWord 
*temp = new KeyWord;
            strcpy (temp
->Key, _key); 

            
if (!Head)
            
{
                Head 
= temp;
                Head
->m_KeyWordLine = NULL;
                Head
->next = NULL;
                KeyCount 
++;
            }

            
            
else
            
{
                KeyWord 
*Search = Head;
                
                
bool IsContain = false;
                
                
while(1)
                
{
                    
if (strcmp (Search->Key, _key) == 0)
                    
{
                        IsContain 
= true;
                        Word
--;
                        cout 
<< "亲爱的用户,关键词( "<< _key << " )已经存在了,请输入其它关键词." << endl;
                        cout 
<< endl;
                        delete temp;
                        
break;
                    }

                    
                    
if (Search->next == NULL)
                    
{
                    
break;
                    }


                    
else
                    
{
                        Search 
= Search->next;    
                    }

                }

                

                
if (!IsContain)
                
{
                    Search
->next = temp;
                    temp
->m_KeyWordLine = NULL;
                    temp
->next = NULL;
                    KeyCount 
++;
                }

            }

        }

        
        
else
            
break;
    }

    
}



bool  Core::ReadFile()
{
    Falg(InputF);
    
char FileName[100];
    cin 
>> FileName;
    ifstream File(FileName,ios::
in);
    
    
if (!File)
    
{
        
return false;
    }

    AnalysisCenter(File);
    File.close();
    
return true;
}



void  Core::AnalysisCenter(ifstream  & File)
{
    KeyWord 
*Current = Head;
    
for (int i = 0; i < KeyCount; i++)
    
{
        
if (Current != NULL)
        
{
            lp[i] 
= Current;
            Current 
= Current->next;
        }

    }

    
    
int Line = 0;
    
int Pos = 0;
    
do
    
{    Pos = 0;
    Line
++;
    IsMatch(File, Line, Pos);
    }
while(!File.eof());
    
}



void  Core::KeyManege( string  m, int  next[])  //  count the longest prefex string ; 
{    
    next[
0]=0
    
    
int temp; 
    
    
for(int i=1;i<m.size();i++
    

        temp
=next[i-1]; 
        
        
while(m[i]!=m[temp]&&temp>0
        

            temp
=next[temp-1]; 
        }
 
        
        
if(m[i]==m[temp]) 
        
{
            next[i]
=temp+1
        }

        
else 
        
{
            next[i]
=0
        }

    }
 
}
 


void  Core::IsMatch(ifstream  & Read,  int  Line,  int   & pos) 

    
int Tp[MAX_KEY]; 
    
int Mp[MAX_KEY];
    
int next[MAX_KEYSIAZE];
    
    
for (int i = 0; i < MAX_KEY; i++)
    
{
        Tp[i] 
= 0;
        Mp[i] 
= 0;
    }

    
    
char text; 
    
    
do
    
{
        
if (Read.eof())
        
{
            
break;
        }

        
        Read.
get(text);
        
        
for (int i = 0; i < KeyCount; i++)
        
{
            KeyManege(lp[i]
->Key, next);
            
string m = lp[i]->Key;
            
            
int tp = Tp[i];
            
int mp = Mp[i];
            
            
while(text!=m[mp]&&mp) 
            
{
                mp 
= next[mp-1]; 
            }

            Mp[i] 
= mp;
            
            
if(text==m[mp]) 
            
{
                mp
++
            }

            Mp[i] 
= mp;
            Tp[i] 
= tp;
            
            
if(mp==m.size()) 
            

                pos
=tp-mp+1;
                
                Point 
*m_Point = new Point;
                m_Point
->Line = Line;
                m_Point
->Queue = pos+1;
                m_Point
->next = NULL;
                
                
if (lp[i]->m_KeyWordLine == NULL)
                
{
                    lp[i]
->m_KeyWordLine = m_Point;
                    lp[i]
->m_KeyWordLine->next = NULL;
                }

                
else
                
{
                    Point 
*_temp = lp[i]->m_KeyWordLine;
                    
while (_temp->next != NULL)
                    
{
                        _temp 
= _temp->next;    
                    }

                    _temp
->next = m_Point;
                }

            }

            
            tp
++;
            Tp[i] 
= tp;
        }

    }
while ( (text != ' ')&&(!Read.eof()));
}


void  Core::ShowResult()
{
    Falg (Search);
    
if (NULL != Head)
    
{
        cout 
<< "_______________((~_~))________________________"<< endl;
        KeyWord 
*Current = Head;
        
        
while (Current != NULL)
        
{
            
if (Current->m_KeyWordLine != NULL)
            
{
                cout 
<< "关键字为: " << Current->Key << ""<< endl;            
            }

            
            
else
            
{
                cout 
<< "没有搜索到关键词: " << Current->Key << "的存在" << endl;
            }

            
            
if (Current->m_KeyWordLine != NULL)
            
{
                Point 
*= Current->m_KeyWordLine;
                
int count = 0;
                
int Position = 0;
                
while(p != NULL)
                
{
                    Point 
*delp = p;
                    
if ((p->Line )&&(p->Queue))
                    
{
                        
if (Position != p->Line)
                        
{
                            cout 
<< endl;
                            cout 
<< ""<< p->Line << "行第";
                            Position 
= p->Line;
                        }

                        cout 
<< p->Queue << "个, ";
                        count
++;
                    }

                    p 
= p->next;
                    
                }

                cout 
<< endl;
                cout 
<< "一共" << count << "" << endl;
            }

            
            cout 
<< endl;
            cout 
<< "_______________((~_~))________________________"<< endl;
            
            KeyWord 
*delk = Current;
            Current 
= Current->next;
        }

    }

}


void  Core::Falg(State lp)
{
    system(
"cls");
    
switch(lp)
    
{
    
case Welcome:
        
{
            system(
"color 5");
            cout 
<<"……………………………………………………………………………………" << endl;
            Sleep(
4100);
            
break;
        }

        
    
case InputF:
        
{
            system(
"color 9");
            cout 
<<"……………………………………………………………………………………" << endl;
            cout 
<<"…………………请输入您需要搜索的文章,包括后缀明哦……………………" << endl;
            cout 
<<"……………………………………………………………………………………" << endl;
            cout 
<<"………>:";
            
break;
        }

        
    
case InputK:
        
{
            system(
"color 2");
            cout 
<<"………………………………………………………………………………………………" << endl;
            cout 
<< "…请输入您需要搜索的关键词,每个关键词以回车键分隔,结束输入请输love2008…" <<endl;
            cout 
<<"………………………………………………………………………………………………" << endl;
            
            
break;
        }

        
    
case Search:
        
{
            system(
"color 6");
            cout 
<<"………………………………………………………………………………………………" << endl;
            cout 
<<"……………………搜索结果出来了,o(∩_∩)o...呵呵………………………………" << endl;
            cout 
<<"………………………………………………………………………………………………" << endl;
            cout 
<< endl;
            
break;
        }


    }

    
}

3.//main.cpp

 

int  main()
{
    Core core;
    
    core.InputKey();

    
if (core.ReadFile())
    
{
    cout 
<< endl;
    core.ShowResult();
    }


    
else
    
{
    cout 
<< "您所输入的文件名有误,请查证" << endl;
    }


    system(
"pause");
    
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值