#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAXN 100
struct p{
int x,y;
char name[20];
p* next;
p* prec;
}head,cend;
int n,x[MAXN+2],y[MAXN+2];
char name[MAXN][20];
void build(p* now,int pos){//建造鏈表
p* next=(p*)malloc(sizeof(p));
now->next=next;
next->prec=now;
next->x=x[pos];
next->y=y[pos];
memcpy(next->name,name[pos], sizeof(name[pos]));
if(pos==n){
next->next=&cend;
(&cend)->prec=next;
return;
}
build(next,pos+1);
return;
}
void init(){//數據輸入
(&head)->prec=NULL;
(&cend)->next=NULL;
for(int i=1;i<=n;++i){
cin>>x[i];
cin>>y[i];
cin>>name[i];
}
build(&head,1);
}
int dis(int x1,int y1,int x2,int y2){//計算2點距離
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
void select(int x1,int y1,int D){
p* now=&head;
p* next;
int countt=0;
while(now->next!=NULL){
next=now->next;
if(dis(next->x,next->y,x1,y1)<=D*D){//判斷距離
if(next->x==0&&next->y==0&&strcmp(next->name,"")==0){//cend鏈尾情況不考慮,因爲cend初始默認爲x=0,y=0
now=next;
continue;
}
cout<<"找到城市:"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
countt++;
}
now=next;
}
if(countt==0)//沒有符合條件的城市
cout<<"沒有這個城市"<<endl;
return;
}
void selectc(char* cn){
p* now=&head;
p* next;
int countt=0;
while(now->next!=NULL){
next=now->next;
if(strcmp(next->name,cn)==0){//找到指定城市
cout<<"找到城市:"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
return;
countt++;
}
now=next;
}
if(countt==0)
cout<<"沒有這個城市"<<endl;
return;
}
void insert(int x1,int y1,char* cn,int len){//插入一個城市信息
p* now=(p*)malloc(sizeof(p));
now->x=x1;
now->y=y1;
memcpy(now->name,cn, len);
now->name[len]='\0';
p* prec=(&cend)->prec;
now->prec=prec;
now->next=(&cend);
prec->next=now;
(&cend)->prec=now;
cout<<"已插入"<<now->x<<' '<<now->y<<' '<<now->name<<endl;
return;
}
void delete1(char* cn){//刪除一個城市信息
p* now=&head;
p* next;
while(now->next!=NULL){
next=now->next;
if(strcmp(next->name,cn)==0){
p* prec=next->prec;
p* n=next->next;
prec->next=n;
n->prec=prec;
cout<<"已刪除"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
free(next);//釋放內存
return;
}
now=next;
}
cout<<"沒有這個城市"<<endl;
return;
}
void updata(int x1,int y1,char* cn){//更新一個城市信息
p* now=&head;
p* next;
while(now->next!=NULL){
next=now->next;
if(strcmp(next->name,cn)==0){
next->x=x1;
next->y=y1;
cout<<"已更新"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
return;
}
now=next;
}
cout<<"沒有這個城市"<<endl;
return;
}
int main(){
cout<<"請輸入你要設置的城市個數:之後(x+y+城市名)\n";
cin>>n;
int m,t,i;
init();
while(true){
cout<<endl;
cout<<"請選擇操作類型 1:查找一個點範圍內的城市;2:查找一個城市;3: 插入;4:刪除;5:更新 6:退出\n";
int type;//操作類型
scanf("%d",&type);
if(type==1){
cout<<"請輸入x+y+D\n";
int x1,y1,D;
cin>>x1>>y1>>D;
select(x1,y1,D);
}
else if(type==2){
char cn1[20];
cout<<"請輸入城市名\n";
cin>>cn1;
selectc(cn1);
}
else if(type==3){
cout<<"請輸入x+y+城市名\n";
int x1,y1;
char cn1[20];
cin>>x1>>y1>>cn1;
insert(x1,y1,cn1,sizeof(cn1));
}
else if(type==4){
char cn1[20];
cout<<"請輸入城市名\n";
cin>>cn1;
delete1(cn1);
}
else if(type==5){
cout<<"請輸入x+y+城市名\n";
int x1,y1;
char cn1[20];
cin>>x1>>y1>>cn1;
updata(x1,y1,cn1);//根據一個城市名字找
}
else if(type==6)
break;
}
return 0;
}
城市鏈表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.