You are on page 1of 10

HW1:平面座標系

學號:CA365115
輸出結果:
直角座標系:

極座標系:

C++ code(截圖版):
Plane.h:

Plane.cpp:
Cartesian.h:

Cartesian.cpp:
Polar.h:

Polar.cpp:
主程式:
C++ code 原始版:
Plane.h:
#pragma once
#include<string>
#include<iostream>
enum Coordinate
{
Cartesian,
polar
};
class Plane
{
public:
Plane();
virtual ~Plane();
int positionY;
int positionX;
int distance;
float angle;
virtual void coordinate(Coordinate position);

};

Plane.cpp:
#include"Plane.h"
#include<iostream>
using namespace std;
Plane::Plane()
{
}
Plane::~Plane()
{
}
void Plane::coordinate(Coordinate position)
{
}

Cartesian.h:
#pragma once
#include "Plane.h"
class cartesian :
public Plane
{
public:
cartesian();
~cartesian();
void coordinate(Coordinate position);
};

Cartesian.cpp:
#include "cartesian.h"
#include <iostream>
using namespace std;
cartesian::cartesian()
{
}
cartesian::~cartesian()
{
}
void cartesian::coordinate(Coordinate position)
{
int a;
double r1, k;
double x1, y1;
double s;
Plane::coordinate(position);
cout << "直角座標系(x,y)" << endl;
cout << "輸入的直角座標為:("<< positionX <<","<<positionY<<")" << endl;
cout << "對y軸對稱後的直角座標為:(" << -positionX << "," << positionY <<")"<<
endl;
cout << "對x軸對稱後的直角座標為:(" << positionX << "," << -positionY <<")"<<
endl;
cout << "是否轉換成極座標?(按1執行)";
cin >> a;
x1 = positionX + 0.0;
y1 = positionY + 0.0;
if (a == 1)
r1 = sqrt(x1 * x1 + y1 * y1);
if (positionX >= 0 && positionY >= 0)
{
s = atan((y1 / x1));
k = (s / acos(-1)) * 180;
cout << "極座標為:(" << r1 << "," << k << ")" << endl;
}
if (positionX <= 0 && positionY >= 0)
{
y1 = abs(y1);
x1 = abs(x1);
s = atan(y1 / x1);
k = s / acos(-1) * 180 + 90;
cout << "極座標為:(" << r1 << "," << k << ")" << endl;
}
if (positionX <= 0 && positionY <= 0)
{
s = atan((y1 / x1));
k = s / acos(-1) * 180 + 180;
cout << "極座標為:(" << r1 << "," << k << ")" << endl;
}
if (positionX >= 0 && positionY <= 0)
{
y1 = abs(y1);
x1 = abs(x1);
s = atan(y1 / x1);
k = (s / acos(-1))* 180 + 270;
cout << "極座標為:(" << r1 << "," << k << ")" << endl;
}

Polar.h:
#pragma once
#include "Plane.h"
class Polar :
public Plane
{
public:
Polar();
~Polar();
void coordinate(Coordinate position);
};

Polar.cpp:
#include "Polar.h"
#include<iostream>
#include<math.h>
using namespace std;
Polar::Polar()
{
}
Polar::~Polar()
{
}
void Polar::coordinate(Coordinate position)
{
int x;
int z;
double degree;
double number;
int a,x1,y1;
Plane::coordinate(position);
x = angle;
x = x % 360;
if (x < 0)
x += 360;
z = 180 - x;
if (z < 0)
z += 360;
cout << "極座標系(r,θ)" << endl;
cout << "輸入的極座標為:("<<distance<<","<<x<<")" << endl;
cout << "對y軸對稱後的極座標為:(" << distance << "," << z << ")" << endl;
cout << "對x軸對稱後的直角座標為:(" << distance << "," << 360-x << ")" << endl;
cout << "是否轉換成直角座標?(按1執行)";
cin >> a;
if (a == 1)
number = 180.0;
degree = x + 0.00;
degree = (degree/180);
degree=degree*acos(-1);
x1 = distance * cos(degree);
y1 = distance * sin(degree);
cout << "直角座標為:(" << x1 << "," << y1 << ")" << endl;
}
主程式:
#include <iostream>
#include"Polar.h"
#include"cartesian.h"
#include "math.h"
using namespace std;

int main()
{
Plane* c = NULL;
int option;
cout << "請選擇座標系:(1.直角座標系 2.極座標系)";
cin >> option;
switch (option)
{
case 1:
c = new cartesian;
cout << "請輸入X座標:";
cin >> c->positionX;
cout << "請輸入Y座標:";
cin >> c->positionY;
c->coordinate(Cartesian);
break;
case 2:
c = new Polar;
cout << "請輸入R(distance):";
cin >> c->distance;
cout << "請輸入angle:";

cin >> c->angle;


c->coordinate(polar);
break;
}
delete c;
return 0;
}

You might also like