您的位置:首页 > 其它

TxtMonster的设计过程(二)

2005-04-22 14:50 1221 查看
继续:程序基本写完了,当然还有一些要补充的。
下面把原代码贴一下,有几个地方作了修改:
TxtMonster.h:
struct point                                     //存储一个字符的点
{
         char c;
         point* next;
};
 
struct line                                        //存储一个串的线,带头结点
{
         point* head;
         line* next;
};
 
struct surface                                  //存储整个树的面,带头结点
{
         line* head;
};
 
class CTxtMonster
{
private:
         surface txt_tree;
         int PrintLine(point*);                  
         int ProlongLine(char);                   //向line结构压入一个字符
         int NewLine();
         int DelLinePoint(line*);         //清除某一个line下的所有的point
         int PrintTree(line*);          //输出整个结构
         int Killme(line*);
public:
         CTxtMonster();                       
         ~CTxtMonster();                    
         int EatChar(char);              //向对象输入字符,这里任何字符都可以
         int ShowLine(int);             //参数表示要求输出的行号,以1开始
         voi
13871
d ShowMe();
};
 
字符树结构没有改变,但成员函数发生了一点改动。
―――――――――――――――――――――――――――――――――
TxtMonster.cpp:
#include<iostream>
#include"TxtMonster.h"
 
using namespace std;
 
CTxtMonster::CTxtMonster()
{
         txt_tree.head=NULL;
         line* first_line=NULL;
         first_line=new line;
         first_line->head=NULL;
         first_line->next=NULL;
         txt_tree.head=first_line;
        
}
 
CTxtMonster::~CTxtMonster()
{
         /**********************
          释放所有单元
         **********************/
 
         Killme(txt_tree.head);
}
 
int CTxtMonster::PrintLine(point* pMe)
{
         if (pMe==NULL) return 1;
         if(pMe->next!=NULL)
                  PrintLine(pMe->next);
         cout<<pMe->c;
         return 0;
}
 
int CTxtMonster::ProlongLine(char nosh)
{
    point* pNosh;
         point* t;
         pNosh=new point;
         pNosh->c=nosh;
         pNosh->next=NULL;
         t=txt_tree.head->head;
         txt_tree.head->head=pNosh;
         pNosh->next=t;
         return 0;
}
 
int CTxtMonster::NewLine()
{
         if (txt_tree.head->head==NULL)                            //别担心txt_tree.head是一个NULL,因为在构造时就添了一个line
                   return 1;
 
         //先将当前line的末尾的空格去掉
         while(txt_tree.head->head->c==32)               //注意:这个删除空格的过程并没有检查是否会全部删除,
                                                                                                       //在调用这个函数前要自行判断
         {
                   point* t=NULL;
                  t=txt_tree.head->head->next;
                   delete txt_tree.head->head;
                  txt_tree.head->head=t;
         }
         ProlongLine('/n');
         line* newline=NULL;
         newline=new line;
         newline->head=NULL;
         newline->next=NULL;
         newline->next=txt_tree.head;
         txt_tree.head=newline;
         return 0;
}
 
int CTxtMonster::DelLinePoint(line* pkill)
//这个函数只是删除pkill所指的line下的所有的point
{
         while(pkill->head!=NULL)
         {
                   point* t=NULL;
                  t=pkill->head;
                  pkill->head=t->next;
                   delete t;
         }
         return 0;
}
 
int CTxtMonster::Killme(line* pkill)
{//删除pkill指针所指的开始的树
         if(pkill->next!=NULL)
                  Killme(pkill->next);
         DelLinePoint(pkill);
         delete pkill;
         return 0;
}
 
int CTxtMonster::PrintTree(line* pl)              //向屏幕输出pl所指的后面的字符树
{
        if(pl==NULL) return 1;
         if(pl->next!=NULL)
         {
                  PrintTree(pl->next);
         }
         PrintLine(pl->head);
         return 0;
}
 
int CTxtMonster::EatChar(char food)
{
         if(food==10)
         {
                  NewLine();
                   return 1;
         }
         if(food==32)
         {
                  if(txt_tree.head->head==NULL) return 2;
         }
         ProlongLine(food);
         return 0;
}
 
int CTxtMonster::ShowLine(int index)           //给出行号,向屏幕输出相应行
{
         int i=0;
         line* p=txt_tree.head;
         while (p!=NULL)                            //这里得到总的line数,最后一个末完的line计也在内
         {
                   i=i+1;
                  p=p->next;
         }
         cout<<"共"<<i<<"行。"<<endl;
         if (index==i)
         {
                   cout<<"本行接收数据尚末结束,不能输出..."<<endl;
                   return 1;
         }
         if (index>i||index<=0)
         {
                   cout<<"提供的行数超出所记录的总行数!"<<endl;
                   return 2;
         }
         i=-index;
         p=txt_tree.head;
         for(int j=0;j<i;j++)
                  p=p->next;
         PrintLine(p->head);
         return 0;
}
 
void CTxtMonster::ShowMe()
{
         PrintTree(txt_tree.head->next);
}

 
main.cpp://main.cpp
#include<iostream>
#include<stdlib.h>
#include"TxtMonster.h"
 
using namespace std;
 
void main()
{
         system("cls");
         cout<<"本程序接收键盘输入的字符,分行存储在树形结构中,"<<endl;
         cout<<"注意:字符'.'表示一次输出"<<endl;
         cout<<"Abeni@sina.com"<<endl;
         //test
         CTxtMonster* pMe;
         pMe=new CTxtMonster;
         while(1)
         {
                   char t=getchar();
                   if (t=='.')
                   {
                            cout<<endl<<"↓↓↓↓↓↓↓↓↓↓输出开始↓↓↓↓↓↓↓↓↓↓"<<endl;
                            pMe->ShowMe();
                            cout<<"← ← ← ← ← ← ← 输出结束"<<endl;
                   }
                   else
                            pMe->EatChar(t);
                  pMe->ShowLine(3);
       
         }
}
 
(今天又是星期五了,我得早点回去睡觉,明天一大早还得赶到学校去呢!上面的程序有一点改动,下次我空再讨论吧。)
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息