daily pastebin goal
32%
SHARE
TWEET

Untitled

mikeper030 Jun 14th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. struct Circle {
  5.    double _x, _y, _r ;
  6. } ;
  7. struct Rectangle {
  8.    double _top_left_x, _top_left_y,
  9.                _bot_right_x, _bot_right_y ;
  10. } ;
  11. struct Node {
  12.    void *_data ;
  13.    struct Node *_left, *_right ;
  14. } ;
  15. struct Gen_arr {
  16.    void * arr[5] ;
  17.    int used ;
  18. };
  19. void insert_reset(Gen_arr&,int&,void*);
  20. typedef int(p2_sort_func)(void*val,void*num);
  21. typedef double(p2_area_func)(void*obj);
  22. typedef void (p2_print_func)(Gen_arr,int);
  23. #define _USE_MATH_DEFINES ;
  24. void read_data_c(Node*&);
  25. void insert_to_tree(void*,Node*&,int(*)(void*,void*));
  26. int cmp_circle_x(void*,void*);
  27. void read_data_r(Node*&root);
  28. double get_area_c(void*circle);
  29. double get_area_r(void*rectangle);
  30. void insert(Gen_arr&,int&,void*);
  31. void find_max(Node*root,Gen_arr&p2_max_arr,p2_area_func p2_a,double&prev,int&used);
  32. void print(p2_print_func ,int,Gen_arr);
  33. void print_c(Gen_arr,int size);
  34. void print_r(Gen_arr,int size);
  35. int main() {
  36.     Node*c_root=NULL,*r_root=NULL;
  37.     int used1=0,used2=0;
  38.     double prev1,prev2;
  39.     Gen_arr a,b;
  40.     read_data_c(c_root);
  41.     read_data_r(r_root);
  42.     find_max(c_root,a,get_area_c,prev1,used1);
  43.     find_max(r_root,b,get_area_r,prev2,used2);
  44.     print(print_c,used1,a);
  45.     print(print_r,used2,b);
  46.     return 0;
  47. }
  48. int cmp_circle_x(void* p2_c,void* p2_n){
  49.     Circle* cr=(Circle*)p2_c;
  50.     Circle* tc=(Circle*)p2_n;
  51.     double x=tc->_x;
  52.     if(cr->_x<x)
  53.         return-1;
  54.  
  55.     return 1;
  56. }
  57. void print(p2_print_func pr,int size,Gen_arr arr){
  58.      pr(arr,size);
  59. }
  60. int cmp_rec_x(void* p2_r,void *p2_n){
  61.     Rectangle* r=(Rectangle*)p2_r;
  62.     Rectangle* s=(Rectangle*)p2_n;
  63.     if(r->_top_left_x<s->_top_left_x)
  64.         return -1;
  65.  
  66.     return 1;
  67. }
  68. void read_data_c(Node*&root){
  69.         while(true){
  70.         Circle* circle =new Circle;
  71.         cin>>circle->_x>>circle->_y;
  72.         if(circle->_x==0&&circle->_y==0)
  73.             break;
  74.         else cin>>circle->_r;
  75.  
  76.         insert_to_tree(circle,root,cmp_circle_x);
  77.        }
  78. }
  79. void read_data_r(Node*&root){
  80.       while(true){
  81.           Rectangle* r=new Rectangle;
  82.           cin>>r->_top_left_x>>r->_top_left_y>>r->_bot_right_x>>r->_bot_right_y;
  83.           if(r->_bot_right_x==0&&r->_bot_right_y==0&&r->_top_left_x==0&&r->_top_left_y==0)
  84.               break;
  85.           insert_to_tree(r,root,cmp_rec_x);
  86.       }
  87. }
  88. void print_c(Gen_arr o,int size){
  89.     for(int i=0;i<size;i++){
  90.         Circle* c=(Circle*)o.arr[i];
  91.         cout<<c->_x<<" "<<c->_y<<" "<<c->_r<<endl;
  92.     }
  93. }
  94.  
  95. void print_r(Gen_arr o,int size){
  96.     for(int i=0;i<size;i++){
  97.         Rectangle* r=(Rectangle*)o.arr[i];
  98.         cout<<r->_top_left_x<<" "<<r->_top_left_y<<" "<<r->_bot_right_x<<" "<<r->_bot_right_y<<endl;
  99.     }
  100. }
  101. double get_area_c(void*circle){
  102.     Circle* c=(Circle*)circle;
  103.     return M_PI*c->_r*c->_r;
  104. }
  105. double get_area_r(void*rectangle){
  106.     Rectangle* r=(Rectangle*)rectangle;
  107.     return ((abs(r->_top_left_x-r->_bot_right_x))*abs((r->_top_left_y-r->_bot_right_y)));
  108.  
  109. }
  110. void insert_reset(Gen_arr&arr,int&used,void*data){
  111.     if(used>4)
  112.         return;
  113.     used=1;
  114.     arr.used=used;
  115.     arr.arr[0]=data;
  116.  
  117. }
  118. void insert(Gen_arr&arr,int&used,void*data){
  119.     if(used>4)
  120.         return;
  121.  
  122.   arr.arr[used]=data;
  123.   used+=1;
  124.   arr.used=used;
  125. }
  126.  
  127. void find_max(Node*root,Gen_arr&p2_max_arr,p2_area_func p2_a,double&prev,int&used){
  128.     if(!root)
  129.        return;
  130.         if(p2_a(root->_data)>prev){
  131.         insert_reset(p2_max_arr,used,root->_data);
  132.         prev=p2_a(root->_data);
  133.         }else
  134.         if(p2_a(root->_data)==prev)
  135.         insert(p2_max_arr,used,root->_data);
  136.  
  137.            find_max(root->_right,p2_max_arr,p2_a,prev,used);
  138.            find_max(root->_left,p2_max_arr,p2_a,prev,used);
  139.  
  140. }
  141.  
  142. void insert_to_tree(void* p2_obj,Node*&root,p2_sort_func cmp_sort_x){
  143.     //reached location to populate node
  144.     if(!root)
  145.      {
  146.      root=new Node;
  147.          if(!root)
  148.          exit(-1);
  149.  
  150.      root->_data=p2_obj;
  151.      root->_left=root->_right=NULL;
  152.      }
  153.     //keep looking.. with recursion..
  154.     else
  155.         if(cmp_sort_x(p2_obj,root->_data)<0)
  156.          insert_to_tree(p2_obj,root->_left,cmp_sort_x);
  157.      else
  158.          insert_to_tree(p2_obj,root->_right,cmp_sort_x);
  159. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top