1.定义Point类:
定义设置函数Set,设置坐标x,y
定义分别取x,y轴坐标分量的函数xOffset与yOffset
定义点的极坐标函数angle与极坐标半径函数radius,分别实现极坐标和极坐标半径转换
2.主函数实现:
定义Point类对象p
键盘输入并设置对象p的坐标x=1,y=2
输出对象p的极坐标和极坐标半径
输出对象p的的直角坐标分量
#include<bits/stdc++.h>
using namespace std;
class point{
int x,y;
public:
void set(double a,double b){
x=a;
y=b;
}
void xoffset(double a){
cout<<"x offset="<<a<<",";}
void yoffset(double b){
cout<<"y offset="<<b;
}
void angle(double a,double b){
double c=atan2(b,a);
cout<<"angle="<<c<<",";
}
void radius(double a,double b){
double c=sqrt(a*a+b*b);
cout<<"radius="<<c<<",";
}
};
int main(){
double c,d;
cin>>c>>d;
point p;
p.set(c,d);
p.angle(c,d);
p.radius(c,d);
p.xoffset(c);
p.yoffset(d);
}
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
double _x = 0.0;
double _y = 0.0;
public:
void Set(double x, double y)
{
_x = x;
_y = y;
}
double xOffset() const
{
return _x;
}
double yOffset() const
{
return _y;
}
double angle() const
{
double theta;
if (abs(_x) > abs(_y))
theta = atan(_y / _x);
else
theta = M_PI / 2.0 - abs(atan(_x / _y));
if (_x < 0.0 && _y > 0.0)
theta = M_PI - theta;
else if (_x < 0.0 && _y < 0.0)
theta += M_PI;
else if (_x > 0.0 && _y < 0.0)
theta = 2.0 * M_PI - theta;
return theta;
}
double radius() const
{
return sqrt(_x * _x + _y * _y);
}
};
int main()
{
double x, y;
cin >> x >> y;
Point p;
p.Set(x, y);
cout << "angle=" << p.angle() << ", radius=" << p.radius() << endl;
cout << "xOffset=" << p.xOffset() << ", yOffset=" << p.yOffset() << endl;
return 0;
}