求大神帮忙把这个程序改成c的,蟹蟹
程序代码:
#include<iostream>
using namespace std;
template<class T>
classBinNode;
template<class T>
classBiTree;
template<class T>
class information //此类包含城市的所有信息?包含?城市名?两个坐标?
{
public:
T city; //城市名
struct location//定义一个结构体?用来放城市的坐标
{
intx,y;//城市坐标
}city_cityname;//结构体 location的对象
};
template<class T>
class BiNode// 二叉排序树的结点结构
{
public:
friend class BiTree<T>;//将BiTree这个类声明为其友元类?使其可以调
用这个类中的一切 www. 10 information<T>data;
BiNode<T>*lchild,*rchild;
};
template<class T>
classBiTree
{
public:
BiTree(T a[],int n);//带参构造函数
BiNode<T>*Getroot(){return root;}//获取指向根结点的指针
BiNode<T>*InsertBST(BiNode<T>*root,BiNode<T>*s); //在二叉排序树
中插入一个结点s?此处用于起始值插入
BiNode<T>*InsertBST1(BiNode<T>*root,BiNode<T>*s);
//2在二叉排序树中插入一个结点s?用于后期功能的多个或一个插入
//
值?会显示插入成功
BiNode<T>*SearchBST(BiNode<T>*root,T k);//查找值为k的结点
void Preorder(BiNode<T>*rt);//前序遍历二叉树
void Inorder(BiNode<T>*rt); //中序遍历二叉树
void Choose();//选择界面
BiNode<T>*Begain(BiNode<T>*root,BiNode<T>*s);//对城市X,Y坐标
进行初始化
BiNode<T>*Find(BiNode<T>*root,T k);//用于查找需要被比较的城市的
坐标
BiNode<T>*Find1(BiNode<T>*root,T k);//查找作为参照的城市坐标
private:
BiNode<T>*root;//二叉排序树的根指针
};
#include"1.h"
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<string>
template<class T>
classBiTree;
static intx,y,z,w;//后面需要用到的算距离范围
template<class T>
BiTree<T>::BiTree(T a[],int n)//构造函数?初始化所有内容
{
root=NULL;
for(inti=0;i<n;i++)
{
BiNode<T>*s=new BiNode<T>;
s->data.city=a[i];
s
->lchild=NULL;
s->rchild=NULL; www. 11 root=InsertBST(root,s);//结点依次插入
Begain(root,s);//输入起始数据的各个坐标
}
}
template<class T>
BiNode<T>*BiTree<T>::InsertBST(BiNode<T>*root,BiNode<T>*s)
{//在二叉排序树中插入一个结点s?此处用于起始插入值
if(root==NULL) //如果原结点不存在?则插入结点变成根结点
return s;
else//如果有结点存在?则进行判断?二叉排序树特点?根结点大于其左子树?
小于其右子树
{
if(s->data.city<root->data.city)//插入结点小于根结点?按关键字城市名
进行判断?
root->lchild=InsertBST(root->lchild,s);//插入到左子树中
else //插入结点大于根结点?按关键字城市名进行判断?
root->rchild=InsertBST(root->rchild,s); //插入到右子树中
return root;
}
}
template<class T>
BiNode<T>*BiTree<T>::InsertBST1(BiNode<T>*root,BiNode<T>*s)
{//1在二叉排序树中插入一个结点s?用于后期功能的多个或一个插入值?会显
示插入成功
if(root==NULL) //如果原结点不存在?则?插入结点变成根结点
return s;
else
{
if(s->data.city<root->data.city)
{
root->lchild=InsertBST(root->lchild,s);
}
else
{
root->rchild=InsertBST(root->rchild,s);
cout<<" "<<endl;
Begain(root,s);
}
}
return root;
}
template<class T>
BiNode<T>*BiTree<T>::Begain(BiNode<T>*root,BiNode<T>*s) www. 12 {//输入起始数据的各个坐标
cout<<"下面进行城市坐标的初始化?请输入城市:"<<s->data.city<<"的X,Y
坐标"<<endl;
cout<<"请输入x坐标:";
cin>>s->data.cityname.x;
cout<<"请输入y坐标:";
cin>>s->data.cityname.y;
return root;
}
template<class T>
void BiTree<T>::Choose()//显示的总界面
{
cout<<endl<<endl<<endl;
cout<<"**********************************************************
********"<<endl;
cout<<"===================================================
==============="<<endl;
cout<<"* $欢迎使用城市信息管理系统
$ *"<<endl;
cout<<"===================================================
==============="<<endl;
cout<<endl;
cout<<"*------------请根据如下城市代码了解数字代表的城市信息
------------*"<<endl;
cout<<"##########################################################
#########"<<endl;
cout<<"*------------------------------功能列表--------------------------*"<<endl<<endl;
cout<<"(1)***********************前序遍历城市信息表
**********************"<<endl;
cout<<"(2)***********************中序遍历城市信息表
**********************"<<endl;
cout<<"(3)**************************查询城市信息
*************************"<<endl;
cout<<"(4))************************查找指定范围内的城市
*******************"<<endl;
cout<<"(5))***************************插入一个城市
************************"<<endl;
cout<<"(6)******************************退出
******************************"<<endl;
cout<<endl;
}
template<class T>
void BiTree<T>::Preorder(BiNode<T> *rt) //前序
{ www. 13 if(rt)
{
cout<<rt->data.city<<"("<<rt->data.cityname.x<<","<<rt->data.cityname.y
<<")"<<ends;
Preorder(rt->lchild);
Preorder(rt->rchild);
}
}
template<class T>
void BiTree<T>::Inorder(BiNode<T>*rt) //中序
{
if(rt)
{
Inorder(rt->lchild);
cout<<rt->data.city<<"("<<rt->data.cityname.x<<","<<rt->data.cityname.y
<<")"<<ends;
Inorder(rt->rchild);
}
}
template<class T>
BiNode<T>*BiTree<T>::Find(BiNode<T>*root,T k)
{ //得到被比较结点的x,y坐标?根据关键字查找
if(root==NULL)
return root;
else
{
if(root->data.city==k)
{ //查找成功?返回
x=root->data.city_cityname.x;//将找到的结点的坐标分别赋值给两
个变量x?y
y=root->data.city_cityname.y;
return root;
}
else//如果之前没找到?则按二叉排序树左小右大的规律继续查找
{
if(k<root->data.city)
{
return Find(root->lchild,k);
}
else
{
return Find(root->rchild,k); www. 14 }
}
}
}
template<class T>
BiNode<T>*BiTree<T>::Find1(BiNode<T>*root,T k)
{//得到结点的x,y坐标?作为参照的结点的坐标值
if(root==NULL)
return root;//同上解释
else
{
if(root->data.city==k)
{//查找成功?返回
z=root->data.city_cityname.x;
w=root->data.city_cityname.y;
return root;
}
else
{
if(k<root->data.city)
{
return Find(root->lchild,k);
}
else
{
return Find(root->rchild,k);
}
}
}
}
template<class T>
BiNode<T>*BiTree<T>::SearchBST(BiNode<T>*ROOT,T k)
{//查找某个城市的所有信息
if(root==NULL)
{//没有找到时的显示
cout<<"此城市不存在!"<<endl;
return NULL;
}
else
{
if(root->data.city==k)
{//查找成功?返回
cout<<"查找"<<root->data.city<<"成功!"<<endl;
cout<<"城市为"<<root->data.city<<"的坐标为www. 15 "<<"("<<root->data.cityname.x<<"<"<<root->data.cityname.y<<")"<<endl;
return root;
}
else
{
if(k<root->data.city)
{
returnSearchBST(root->lchild,k);
}//如果之前没找到?则按二叉排序树左小右大的规律继续查找
//
查找左子树
else
{
returnSearchBST(root->rchild,k);
}//查找右子树
}
}
}
#include"1.cpp"
void main()
{
int n;
cout<<"您想输入起始数据的次数:";
cin>>n;
string a[100];
for(inti=0;i<n;i++)
{
cout<<"第"<<i+1<<"个城市 :";
cin>>a[i];
}
BiTree<string>citylist(a,n);
for(int d=0;d<d+1;d++)
{
citylist.Choose();
cout<<endl;
int k;
cout<<"请输入功能序号:";
cin>>k;
switch(k)
{
case 1:
{
for(inti=0;i<n;i++)
cout<<"第
"<<i+1<<"个城市:"<<a[i]<<endl;
cout<<endl; www. 16 cout<<"前序遍历为【城市?坐标1?坐标2?】:"<<endl;
citylist.Preorder(citylist.Getroot());
cout<<endl;
}break;
case 2:
{
for(inti=0;i<n;i++)
cout<<"第"<<i+1<<"个城市:"<<a[i]<<endl;
cout<<endl;
cout<<"中序遍历为【城市?坐标1?坐标2
?】:"<<endl;
citylist.Inorder(citylist.Getroot());
cout<<endl;
}break;
case 3:
{
for(inti=0;i<n;i++)
cout<<"第"<<i+1<<"个城市:"<<a[i]<<endl;
cout<<endl;
string j;
int b;
cout<<"你想查询的次数:";
cin>>b;
for(int e=0;e<b;e++)
{
cout<<"请输入要查询的城市:";
cin>>j;
citylist.SearchBST(citylist.Getroot(),j);
}
}break;
case 4:
{
for(int j=0;j<n;j++)
cout<<"第"<<j+1<<"个城市:"<<a[j]<<endl;
cout<<endl;
BiNode<string>*fcity=new BiNode<string>;
int dis;
cout<<"请输入指定的城市:";
cin>>fcity->data.city;
cout<<endl<<"请指定距离:";
cin>>dis;
cout<<endl;
int v;
for(inti=0;i<n;i++)
{ www. 17 if(a[i]==fcity->data.city)
continue;
else
{
citylist.Find(citylist.Getroot(),a[i]);
citylist.Find1(citylist.Getroot(),fcity->data.city);
v=sqrt((x-z)*(x-z)+(y-w)*(y-w));
cout<<"城市为"<<a[i]<<"的城市与选定城市范围的距离
为:"<<v<<endl;
if(v<dis)
{
cout<<"城市为"<<a[i]<<"的城市?在指定的范围
内!"<<endl;
cout<<endl;
}
else
{
cout<<"不在指定范围内."<<endl;
cout<<endl;
}
}
}
}break;
case 5:
{
for(inti=0;i<n;i++)
{
cout<<"第"<<i+1<<"个城市:"<<a[i]<<endl;
}
cout<<endl;
int c;
cout<<"你想插入的次数?";
cin>>c;
for(int j=0;j<w;j++)
{
cout<<"请输入要插入的城市:";
BiNode<string>* ecity = new BiNode<string>;
cin>>ecity->data.city;
n++;
a[n-1]=ecity->data.city;
ecity->lchild=NULL;
ecity->rchild=NULL;
citylist.InsertBST1(citylist.Getroot(),ecity);
} www. 18 }break;
case 6:
{
cout<<"-----------------------------感谢使用---------------------------"<<endl;
cout<<"=============================真诚设计
==========================="<<endl;
exit(0);
}break;
}
}
}






