Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. struct Node;
  4. typedef struct Node *Ptr;
  5. typedef Ptr Tree;
  6.  
  7. struct Node{
  8. char name[16];
  9. double longi; /*longitude*/
  10. double lati; /*latitude*/
  11. int people; /*population*/
  12. double area;
  13. int level;
  14. Tree LL,LR,RL,RR;
  15. };
  16.  
  17. Tree Input(Tree T,char *xname,double xlongi,double xlati,int xpeople,double xarea,int count);
  18. Tree Quest(Tree T,double xlongi,double xlati);
  19.  
  20. int main(void){
  21. Tree T=NULL;
  22. char input; /*I,Q,E*/
  23. char xname[16]={0};
  24. double xlongi,xlati,xarea;
  25. int xpeople;
  26. int i,count=0;
  27. while(scanf(" %c ",&input)==1){
  28. if(input=='I'){
  29. fgets(xname,16,stdin);
  30. xname[15]='\0';
  31. scanf(" %lf %lf %d %lf",&xlongi,&xlati,&xpeople,&xarea);
  32. T=Input(T,xname,xlongi,xlati,xpeople,xarea,count);
  33. count=0; /*root*/
  34. }
  35. else if(input=='Q'){
  36. scanf("%lf %lf",&xlongi,&xlati);
  37. Quest(T,xlongi,xlati);
  38. }
  39. else if(input=='E'){
  40. printf("BYE");
  41. break;
  42. }
  43. }
  44. return 0;
  45. }
  46.  
  47. Tree Input(Tree T,char *xname,double xlongi,double xlati,int xpeople,double xarea,int count){
  48. int i;
  49. if(T==NULL){
  50. T=malloc(sizeof(struct Node));
  51. if(T==NULL)
  52. printf("Out of space!\n");
  53. else{
  54. count++; /*+root*/
  55. strcpy(T->name,xname);
  56. T->longi=xlongi;
  57. T->lati=xlati;
  58. T->people=xpeople;
  59. T->area=xarea;
  60. T->level=count;
  61. T->LL=T->LR=T->RL=T->RR=NULL;
  62. printf("OK %s %d\n",T->name,T->level);
  63. return T;
  64. }
  65. }
  66. else if(xlongi<=T->longi){
  67. if(xlati<=T->lati){ /*SW*/
  68. count++;
  69. T->LL=Input(T->LL,xname,xlongi,xlati,xpeople,xarea,count);
  70. }
  71. else{ /*NW*/
  72. count++;
  73. T->LR=Input(T->LR,xname,xlongi,xlati,xpeople,xarea,count);
  74. }
  75. }
  76. else if(xlongi>=T->longi){
  77. if(xlati<=T->lati){ /*SE*/
  78. count++;
  79. T->RL=Input(T->RL,xname,xlongi,xlati,xpeople,xarea,count);
  80. }
  81. else{ /*NE*/
  82. count++;
  83. T->RR=Input(T->RR,xname,xlongi,xlati,xpeople,xarea,count);
  84. }
  85. }
  86. return T;
  87. }
  88.  
  89. Tree Quest(Tree P,double xlongi,double xlati){
  90. if(P==NULL)
  91. printf("NO\n");
  92. else if(xlongi==P->longi&&xlati==P->lati)
  93. printf("%s %d %.1f %d\n",P->name,P->people,P->area,P->level);
  94. else{
  95. if(xlongi==P->longi&&xlati==P->lati)
  96. return P;
  97. else if(xlongi<=P->longi){
  98. if(xlati<=P->lati)
  99. return Quest(P->LL,xlongi,xlati);
  100. else if(xlati>P->lati)
  101. return Quest(P->LR,xlongi,xlati);
  102. }
  103. else if(xlongi>P->longi){
  104. if(xlati<=P->lati)
  105. return Quest(P->RL,xlongi,xlati);
  106. else if(xlati>P->lati)
  107. return Quest(P->RR,xlongi,xlati);
  108. }
  109. }
  110. }
Add Comment
Please, Sign In to add comment