Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- struct Node;
- typedef struct Node *Ptr;
- typedef Ptr Tree;
- struct Node{
- char name[16];
- double longi; /*longitude*/
- double lati; /*latitude*/
- int people; /*population*/
- double area;
- int level;
- Tree LL,LR,RL,RR;
- };
- Tree Input(Tree T,char *xname,double xlongi,double xlati,int xpeople,double xarea,int count);
- Tree Quest(Tree T,double xlongi,double xlati);
- int main(void){
- Tree T=NULL;
- char input; /*I,Q,E*/
- char xname[16]={0};
- double xlongi,xlati,xarea;
- int xpeople;
- int i,count=0;
- while(scanf(" %c ",&input)==1){
- if(input=='I'){
- fgets(xname,16,stdin);
- xname[15]='\0';
- scanf(" %lf %lf %d %lf",&xlongi,&xlati,&xpeople,&xarea);
- T=Input(T,xname,xlongi,xlati,xpeople,xarea,count);
- count=0; /*root*/
- }
- else if(input=='Q'){
- scanf("%lf %lf",&xlongi,&xlati);
- Quest(T,xlongi,xlati);
- }
- else if(input=='E'){
- printf("BYE");
- break;
- }
- }
- return 0;
- }
- Tree Input(Tree T,char *xname,double xlongi,double xlati,int xpeople,double xarea,int count){
- int i;
- if(T==NULL){
- T=malloc(sizeof(struct Node));
- if(T==NULL)
- printf("Out of space!\n");
- else{
- count++; /*+root*/
- strcpy(T->name,xname);
- T->longi=xlongi;
- T->lati=xlati;
- T->people=xpeople;
- T->area=xarea;
- T->level=count;
- T->LL=T->LR=T->RL=T->RR=NULL;
- printf("OK %s %d\n",T->name,T->level);
- return T;
- }
- }
- else if(xlongi<=T->longi){
- if(xlati<=T->lati){ /*SW*/
- count++;
- T->LL=Input(T->LL,xname,xlongi,xlati,xpeople,xarea,count);
- }
- else{ /*NW*/
- count++;
- T->LR=Input(T->LR,xname,xlongi,xlati,xpeople,xarea,count);
- }
- }
- else if(xlongi>=T->longi){
- if(xlati<=T->lati){ /*SE*/
- count++;
- T->RL=Input(T->RL,xname,xlongi,xlati,xpeople,xarea,count);
- }
- else{ /*NE*/
- count++;
- T->RR=Input(T->RR,xname,xlongi,xlati,xpeople,xarea,count);
- }
- }
- return T;
- }
- Tree Quest(Tree P,double xlongi,double xlati){
- if(P==NULL)
- printf("NO\n");
- else if(xlongi==P->longi&&xlati==P->lati)
- printf("%s %d %.1f %d\n",P->name,P->people,P->area,P->level);
- else{
- if(xlongi==P->longi&&xlati==P->lati)
- return P;
- else if(xlongi<=P->longi){
- if(xlati<=P->lati)
- return Quest(P->LL,xlongi,xlati);
- else if(xlati>P->lati)
- return Quest(P->LR,xlongi,xlati);
- }
- else if(xlongi>P->longi){
- if(xlati<=P->lati)
- return Quest(P->RL,xlongi,xlati);
- else if(xlati>P->lati)
- return Quest(P->RR,xlongi,xlati);
- }
- }
- }
Add Comment
Please, Sign In to add comment