Code của em:
Kết quả bị lỗi ở chỗ max, min và sum, các bạn (anh/chị) gợi ý mình fix với nhé :happy:
aq.png
À quên , lỗi là kết quả xuất ra là phân số nhưng lại xuất ra kiểu double.:surrender:
Code:
#include<iostream> #include<math.h> using namespace std; class frac { long num;//numerator long den;//denomirator public: friend class fracArray; friend ostream& operator<<(ostream&, frac &); friend istream& operator>>(istream&, frac &); friend frac operator + (frac a, frac b); friend frac operator - (frac a, frac b); friend frac operator * (frac a, frac b); friend frac operator / (frac a, frac b); void input() { cout << "enter numerator = " << endl; cin >> num; cout << "enter denomirator = " << endl; cin >> den; } long max_divisor(long a, long b) { int m; while (b != 0) { m = b; b = a%b; a = m; } return a; } void easy() { long usc = max_divisor(num, den); num /= usc; den /= usc; if (den < 0) { den = -den, num = -num; } if (num == 0) den = 1; } void set(long t, long m) { if (m) { t = num, m = den; easy(); } } frac(long t, long m) :num(t), den(m) { set(t, m); } frac(long t) :num(t), den(1) { set(t, 1); } bool operator > (frac &b) const { return ((num*b.den - den*b.num)*(den*b.den) > 0); } operator double() const { return double(num) / den; } }; long max_divisor(long a, long b) { int m; while (b != 0) { m = b; b = a%b; a = m; } return a; } void restore(double &a) { double b; long c = 1, d; for (int i = 1; i <= 100; i++) { b = a*pow(10, i); d = (long)(a*pow(10, i)); if ((b == d)) { c = i; break; } } long e = pow(10, c); long f = max_divisor(d, e); d /= f; e /= f; cout << d << "/" << e << endl; } frac operator - (frac a, frac b) { return frac(a.num*b.den - a.den*b.num, a.den*b.den); } frac operator + (frac a, frac b) { return frac(a.num*b.den + a.den*b.num, a.den*b.den); } frac operator * (frac a, frac b) { return frac(a.num*b.num, a.den*b.den); } frac operator / (frac a, frac b) { return frac(a.num*b.den, a.den*b.num); } ostream& operator<<(ostream &os,frac &b) { b.easy(); return os << b.num << "/" << b.den; } istream& operator>>(istream &is,frac &b) { return is>>b.num>>b.den; } class fracArray { int k; frac *a; public: void inputArray() { cout << "enter number of frac-array = " << endl; cin >> k; a = (frac*)malloc(sizeof(frac)*k); for (int i = 1; i <= k; i++) { cout << "enter a[" << i << "] = " << endl; cin >> a[i]; } } void output() { for (int i = 1; i <= k; i++) { cout << "a[" << i << "] = " << a[i]<<endl; } } frac sum() { frac b = a[1]; for (int i = 2; i <= k; i++) { b = b + a[i]; } return b; } frac max() { frac b = a[1]; for (int i = 1; i <= k; i++) { if (a[i] > b) b = a[i]; } return b; } frac min() { frac b = a[1]; for (int i = 1; i <= k; i++) { if (b > a[i]) b = a[i]; } return b; } void sort() { for (int i = 1; i < k; i++) { for (int j = i + 1; j <= k; j++) { if (a[i] > a[j]) swap(a[i], a[j]); } } } void swap(frac &a, frac &b) { frac m = a; a = b; b = m; } }; int main() { frac a(9, 4),b(0,1),c(0,1); double B = sqrt(a); cout << "=========================================================================================" << endl; cout << "sqrt(a) ="; restore(B); cout << "=========================================================================================" << endl; cout << "enter frac a :" << endl; a.input(); cout << "enter frac b :" << endl; b.input(); c = a + b; cout << "a + b = " << c<<endl; c = a - b; cout << "a - b = " << c << endl; c = a*b; cout << "a x b = " << c << endl; c = a/b; cout << "a : b = " << c << endl; cout << "=========================================================================================" << endl; fracArray k; cout << "enter frac array :" << endl; k.inputArray(); k.output(); cout << "=========================================================================================" << endl; cout << "maximum = " << k.max() << endl; cout << "=========================================================================================" << endl; cout << "minimum = " << k.min() << endl; cout << "=========================================================================================" << endl; cout << "sum all = " << k.sum() << endl; cout << "=========================================================================================" << endl; cout << "from small to big :" << endl; k.sort(); k.output(); cout << "===========================================THE END===========================================" << endl; system("pause"); return 0; }
aq.png
À quên , lỗi là kết quả xuất ra là phân số nhưng lại xuất ra kiểu double.:surrender:
Comment