upsidedown

conv r.p

Mar 25th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. class polar;
  5.  
  6. class rect
  7. {
  8.     double x,y;
  9. public:
  10.     rect(double a=0,double b=0)
  11.     {
  12.         x=a;
  13.         y=b;
  14.     }
  15.  
  16.     rect(polar &k)
  17.     {
  18.         x=k.r*cos(k.a);
  19.         y=k.r*sin(k.a);
  20.     }
  21.  
  22.     void display()
  23.     {
  24.         cout<<"the rectangular co-ordinates:\n x = "<<x<<" and y = "<<y<<endl;
  25.     }
  26. };
  27.  
  28. class polar
  29. {
  30.     double r,a;
  31. public:
  32.     polar(double c=0,double b=0)
  33.     {
  34.         r=c;
  35.         a=(3.14/180)*b;
  36.     }
  37.  
  38.     polar(rect &k)
  39.     {
  40.         r=sqrt(k.x*k.x+k.y*k.y);
  41.         a=atan(k.y/k.x);
  42.     }
  43.    
  44.     polar operator+(polar &t)
  45.     {
  46.         polar fin;
  47.         double x,y,z,s;
  48.         x=r*cos(a);
  49.         y=r*sin(a);
  50.         z=t.r*cos(t.a);
  51.         s=t.r*sin(t.a);
  52.         x=x+z;
  53.         y=y+s;
  54.         fin.a=atan(y/x);
  55.         fin.r=sqrt(x*x+y*y);
  56.         return fin;
  57.     }
  58.  
  59.     void display()
  60.     {
  61.         cout<<"the polar co-ordinates:\n radius is = "<<r<<" and the angle = "<<a<<endl;
  62.     }
  63. };
  64.  
  65.  
  66.  
  67. int main()
  68. {
  69.     polar p1,p2,p3;
  70.     rect r1;
  71.     double x,y;
  72.     int a,t=1,i;
  73.     do
  74.     {
  75.         cout<<"enter a choice:\n";
  76.         cout<<"1.add two polar co-ordinates\n";
  77.         cout<<"2.convert from polar to rectangular co-ordinates\n";
  78.         cout<<"3.convert from rectangular to polar co-ordinates\n";
  79.         cout<<"4.exit\n";
  80.         cin>>a;
  81.         switch(a)
  82.         {
  83.         case 1:
  84.             cout<<"enter the values of the radius and angle of the two co-ordinates:\n";
  85.             for(i=1;i<=2;i++)
  86.             {
  87.                 cout<<"radius and angle in degrees of "<<i<<" No.:\n";
  88.                 cin>>x>>y;
  89.                 if(i==1)
  90.                     p1=polar(x,y);
  91.                 else
  92.                     p2=polar(x,y);
  93.             }
  94.             p3=p1+p2;
  95.             cout<<"the result is:\n";
  96.             p3.display();
  97.             break;
  98.  
  99.         case 2:
  100.             cout<<"enter the values of the radius and angle of the co-ordinates:\n";
  101.             cout<<"radius and angle in degrees:\n";
  102.             cin>>x>>y;
  103.             p1=polar(x,y);
  104.             r1=p1;
  105.             r1.display();
  106.             break;
  107.  
  108.         case 3:
  109.             cout<<"enter the values of x and y:\n";
  110.             cin>>x>>y;
  111.             r1=rect(x,y);
  112.             p1=r1;
  113.             p1.display();
  114.             break;
  115.  
  116.         case 4:
  117.             t=0;
  118.             break;
  119.  
  120.         default:
  121.             cout<<"invalid\n";
  122.             break;
  123.         }
  124.     }while(t!=0);
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment