数据结构Word文档格式.docx
《数据结构Word文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构Word文档格式.docx(30页珍藏版)》请在冰豆网上搜索。
Car(){}
Car(CStrings,CTimet){
Licence=s;
ctime=t;
}
booloperator==(Cara){
if(a.Licence==Licence)
returntrue;
elsereturnfalse;
CStringLicence;
CTimectime;
};
voidCParkingManagementDlg:
:
OnClickedButton2()
//入停车场
UpdateData();
CTimetime=CTime:
GetCurrentTime();
Carc(m_licence,time);
CStringlicence=c.Licence;
if(m_licence=="
"
)
MessageBox(_T("
请输入车牌号!
));
elseif(parking.IsIn(c))
车辆已在停车场!
elseif(road.Search(c))
车辆已在便道!
elseif(!
parking.IsFull())//进入停车场
{
parking.Add(c);
//车辆加入栈
Showparking();
//ctrllist显示车辆信息
m_licence="
;
UpdateData(false);
CStringstr;
str="
进入停车场"
str=c.Licence+str;
SetDlgItemText(IDC_EDIT3,str);
SetDlgItemText(IDC_EDIT5,_T("
}
else//进入便道
road.Add(c);
Showroad();
OnClickedButton3()
//出停车场
//TODO:
在此添加控件通知处理程序代码
CarCout(m_licence,time),Cin;
CStringlicence=Cout.Licence;
elseif(parking.IsIn(Cout))
Stack<
Car>
temproad;
Cartemp;
while(!
(temp==Cout))
{
//车进入临时通道
parking.Delete(temp);
temproad.Add(temp);
}
temproad.Delete(Cin);
temproad.IsEmpty())
//将临时通道的车开进parking
temproad.Delete(temp);
parking.Add(temp);
if(!
road.IsEmpty())
road.Delete(temp);
temp.ctime=time;
//更改便道上的车进入停车场的时间为当前时间
//计算车辆停车时间
CTimeSpanparkingtime;
parkingtime=Cout.ctime-Cin.ctime;
CStringptime;
ptime.Format(_T("
%02d:
%02d"
),parkingtime.GetHours(),parkingtime.GetMinutes(),parkingtime.GetSeconds());
Income(parkingtime);
停车时间:
str=str+ptime;
SetDlgItemText(IDC_EDIT5,str);
出停车场"
str=m_licence+str;
parking.IsIn(Cout))
车不在停车场内!
二、个人电话号码查询
根据用户输入的信息(例如姓名等)进行快速查询,在外存上,用文件保存电话号码信息;
在内存中,设计数据结构存储电话号码信息;
提供查询功能:
根据姓名实现快速查询;
提供其他维护功能:
例如插入、删除、修改等。
由于需要管理的电话号码信息较多,而且要在程序运行结束后仍然保存电话号码信息,所以电话号码信息采用文件的形式存放到外存中。
在系统运行时,需要将电话号码信息从文件调入内存来进行查找等操作,为了接收文件中的内容,要有一个数据结构与之对应,可以设计如下结构类型的数组来接收数据:
constintmax=10;
structTeleNumber
stringname;
//姓名
stringphoneNumber;
//固定电话号码
stringmobileNumber;
//移动电话号码
stringemail;
//电子邮箱
}Tele[max];
TeleNumber
CStringname;
//姓名
CStringphoneNumber;
CStringmobileNumber;
CStringemail;
//电子邮箱
Operator==//姓名相等
Operator>
Operator<
主要问题是文件的读取,要从文件中读取姓名,固话,移动电话,邮箱并初始化TeleNumber对象。
通过查找资料,了解了fstream流的基本应用。
此题中用到的是二叉搜素树,但在某些情况下,二叉搜素树会不平衡,搜索话费的时间肯能比用一般的方法还要长,因此要考虑二叉搜素树是否平衡,为了解决这个问题,可以用AVL树。
#pragmaonce
classTeleNumber
TeleNumber(){}
TeleNumber(CStringn,CStringp,CStringm,CStringe)
name=n;
phoneNumber=p;
mobileNumber=m;
email=e;
booloperator==(TeleNumbert)
if(name==t.name)
else
returnfalse;
booloperator!
=(TeleNumbert)
return!
(*this==t);
booloperator<
(TeleNumbert)const
if(name<
t.name)
booloperator>
(TeleNumbert)const
if(name>
CStringname;
CStringphoneNumber;
CStringmobileNumber;
CStringemail;
//读取文件
voidCPhoneSearchDlg:
OnBnClickedButton7()
//打开
CFileDialogdlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("
ALLFile(*.txt)|*.*"
),NULL,0,TRUE);
if(dlg.DoModal()!
=IDOK)
return;
CStringstr=dlg.GetPathName();
wifstreamReadFile;
inti=0;
wchar_tline[50];
CStringtemp;
TeleNumbertele;
ReadFile.open(str);
if(ReadFile.fail())return;
ReadFile.imbue(locale("
chs"
//ReadFile.getline(line,50,'
\n'
);
while(!
ReadFile.eof())
ReadFile.getline(line,50,'
//逐行读入
//提取姓名
i=0;
temp.Empty();
while(line[i]!
='
'
temp=temp+line[i];
i++;
tele.name=temp;
while(line[i]=='
//提取固定电话
tele.phoneNumber=temp;
//提取移动电话
tele.mobileNumber=temp;
//提取邮箱
tele.email=temp;
try
Tele.Insert(tele);
catch(BadInput)
MessageBox(_T("
重复!
三、排序应用
编写程序,读出文件中的内容,再按挑房的先后次序排队后将排序号和姓名以文本方式存放到文件A2.txt中。
排队原则:
先按职称排,同职称按分房工龄排,同工龄按年龄排。
用一个对象数组存储职工信息,然后用快速排序算法排序。
对于“先按职称排,同职称按分房工龄排,同工龄按年龄排”的排序规则,用重载>
和<
操作符的方法。
Node
intnumber;
intwork_age;
intage;
operator>
operator<
对于运算符重载做得比较复杂。
排序前:
排序后:
写入文件是,用的是把数据先存入一个CString的字符串中,在数据量大时不合理。
写入文件格式不是很好。
核心代码:
#include"
stdafx.h"
classNode
intnumber;
intwork_age;
intage;
(Noden)//重载>
操作符按先按职称排,同职称按分房工龄排,同工龄按年龄排
if(number>
n.number)
elseif(number==n.number)
if(work_age>
n.work_age)
returntrue;
elseif(work_age==n.work_age)
{
if(age>
n.age)
returntrue;
elsereturnfalse;
}
elsereturnfalse;
(Noden)
if(number<
if(work_age<
if(age<
//读取文件;
voidCOrderDlg:
OnBnClickedButtonOpen()
row=0;
intn=0;
ReadFile.getline(line,50,'
node[n].name=temp;
//提取编号
node[n].number=_ttoi(temp);
//提取工龄
node[n].work_age=_ttoi(temp);
//提取年龄
node[n].age=_ttoi(temp);
n++;
row++;
Show();
//快速排序算法
QuickSort(Node*a,intn)
{//对a[0:
n-1]进行快速排序.
//要求a[n]必须有最大关键值.
quickSort(a,0,n-1);
quickSort(Nodea[],intl,intr)
{//Sorta[l:
r],a[r+1]haslargevalue.
if(l>
=r)return;
inti=l,//从左到右的游标
j=r+1;
//从右到左的游标
Nodepivot=a[l];
//支点
//swapelements>
=pivotonleftside
//withelements<
=pivotonrightside
while(true)
do{//find>
=elementonleftside
i=i+1;
}while(a[i]<
pivot);
do{//find<
=elementonrightside
j=j-1;
}while(a[j]>
if(i>
=j)break;
//swappairnotfound
//Swap(a[i],a[j]);
Nodetemp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
//placepivot
a[l]=a[j];
a[j]=pivot;
quickSort(a,l,j-1);
//sortleftsegment
quickSort(a,j+1,r)
四、火烧连营问题
“火烧连营”是三国演义中的著名典故之一广为流传,假定文本文件c1.txt是火烧连营中的军营分布图,每个字符A代表一个营帐,营帐是可燃物,其他字符代表不可燃的空白地段,文件共有40行70列,请你编写程序,读入该文件的内容,再从键盘输入任意点的x和y值(x<
70,y<
40)作为着火点,“火烧连营”后,被燃烧的营帐标上字符’X’,并把整个结果输出到文件c2.txt中。
用一个char型二维数组存储文件中的40*70的数据,然后根据这个二维数组确定要点火的点,并将这个点放入到一个栈,并判断其周围的点是否是A,是A的话则换成X并将这个点放入栈中,直到栈空为止。
对于边界的处理类比书上的迷宫问题,加一圈围墙,而对于每一行的最后在加上一个换行符“\n”。
另外,在读取文件的时候由于没有考虑到文本本来就有换行符,用的Get函数一个个读取,每一行都多读了一个换行符,导致出错。
在程序设计时考虑不是很严密,导致出现很多问题。
//fire.h
stack.h"
#include<
fstream>
usingnamespacestd;
classPosition
friendclassFire;
private:
intx;
inty;
classFire
Fire()
for(inti=0;
i<
72;
i++)//设置围墙
tent[0][i]='
X'
tent[41][i]='
for(intj=0;
j<
42;
j++)
tent[j][0]='
tent[j][71]='
voidOpenfile();
voidFireposition(intxx,intyy);
voidSave();
voidFireUp(Position&
p)
if(tent[p.x][p.y+1]=='
A'
ten