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);
}
}
(今天又是星期五了,我得早点回去睡觉,明天一大早还得赶到学校去呢!上面的程序有一点改动,下次我空再讨论吧。)
下面把原代码贴一下,有几个地方作了修改:
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);
}
}
(今天又是星期五了,我得早点回去睡觉,明天一大早还得赶到学校去呢!上面的程序有一点改动,下次我空再讨论吧。)
相关文章推荐
- TxtMonster的设计过程(一)
- 某海量用户网站,用户拥有积分,积分可能会在使用过程中随时更新。现在要为该网站设计一种算法,在每次用户登录时显示其当前积分排名。用户最大规模为2亿;积分为非负整数,且小于100万。
- C#基础-041 设计一个玩家类,一个敌人类, 玩家可以攻击敌人,敌人可以攻击玩家,攻击点合理设计,可以通过随机数产生,模拟战斗过程
- 软工总结(四)——面向过程设计
- [转]用EA 建模设计Blog全过程
- [更新设计]跨平台物联网通讯框架ServerSuperIO 2.0 ,功能、BUG、细节说明,以及升级思考过程!
- 架构设计三之app应用的启动过程分析
- 快速入门过程与方法:设计与思路;如何学习新的知识框架,建立思维模式,熟悉应用场景体系
- MySQL的存储过程设计的例子
- Asp.net开发过程中的几个模式(设计,源,cs or vb)切换
- WEB2.0时代的UE设计过程
- 一个RPC服务客户端代理中间件的设计过程的回顾
- 毕业设计过程记录六,单目单点测距
- 软件工程(四)面向过程的软件设计方法
- c++设计模式之Adapter适配器模式.txt
- c++设计模式之Proxy代理模式.txt
- webWMS开发过程记录(四)- 整体设计
- Java实时多任务调度过程中的安全监控设计
- MySQL数据库的设计和命令行模式下建立详细过程
- 聊天室螺旋式体系结构设计——思考过程