OOP HomeWork
This commit is contained in:
91
oop_hw3/hw2/main.cpp
Normal file
91
oop_hw3/hw2/main.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#define M_PI 3.14159265358979323846
|
||||
using namespace std;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
class Fraction {
|
||||
private:
|
||||
int* p;
|
||||
public:
|
||||
Fraction():p(nullptr){}
|
||||
//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>СΪ30<33><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Fraction(int* x) {
|
||||
if (p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30 ;i++) {
|
||||
p[i] = x[i];
|
||||
}
|
||||
}
|
||||
Fraction(const Fraction& x) {
|
||||
if(p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30;i++) {
|
||||
p[i] = x.p[i];
|
||||
}
|
||||
}
|
||||
Fraction& operator=(const Fraction& x) {
|
||||
if(p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30;i++) {
|
||||
p[i] = x.p[i];
|
||||
}
|
||||
}
|
||||
friend ostream& operator<<(ostream& os, const Fraction& x) {
|
||||
for (int i = 0;i < 30;i++) {
|
||||
os << x.p[i] << " ";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
~Fraction() {
|
||||
delete[] p;
|
||||
}
|
||||
void DuiYingFenshu(int x) {
|
||||
long long fenzi = 1, fenmu = 0;
|
||||
for (int i = x - 1; i >= 0; --i) {
|
||||
long long temp = fenzi;
|
||||
fenzi = p[i] * fenzi + fenmu;
|
||||
fenmu = temp;
|
||||
}
|
||||
double val = (double)fenzi / fenmu;
|
||||
double cha = M_PI - val;
|
||||
cout << setprecision(16);
|
||||
cout << "<EFBFBD><EFBFBD>" << x << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>" << fenzi << "/" << fenmu << endl;
|
||||
cout << "<EFBFBD><EFBFBD>" << x << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>" << val << endl;
|
||||
cout << "<EFBFBD><EFBFBD> PI <20><><EFBFBD>" << cha << endl;
|
||||
}
|
||||
};
|
||||
//<2F><>һ<EFBFBD><D2BB>С<EFBFBD><D0A1>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
||||
int* out(double x) {
|
||||
double t = x;
|
||||
double temp;
|
||||
int* tmp;
|
||||
tmp = new int[30];
|
||||
tmp[0] = floor(t);
|
||||
t -= tmp[0];
|
||||
for (int i = 1;i < 30;i++) {
|
||||
temp = 1 / (t);
|
||||
tmp[i] = floor(temp);
|
||||
temp -= tmp[i];
|
||||
t = temp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//<2F><><EFBFBD><EFBFBD>out<75><74><EFBFBD><EFBFBD>
|
||||
int* a;
|
||||
a = out(M_PI);
|
||||
for (int i = 0;i < 30;i++) {
|
||||
cout << a[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Fraction test(a);
|
||||
for (int i = 1;i < 30;i++) {
|
||||
test.DuiYingFenshu(i);
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user