fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. struct DonThuc {
  6. int he_so;
  7. int he_so_mu;
  8. };
  9.  
  10. class Bai3 {
  11. private:
  12. DonThuc *arr;
  13. int sl;
  14.  
  15. public:
  16. Bai3(int s = 0) {
  17. sl = s;
  18. arr = (sl > 0) ? new DonThuc[sl] : nullptr;
  19. }
  20. ~Bai3() {
  21. delete[] arr;
  22. arr = nullptr;
  23. sl = 0;
  24. }
  25. void Nhap() {
  26. cin >> sl;
  27. delete[] arr;
  28. arr = new DonThuc[sl];
  29. for (int i = 0; i < sl; i++) {
  30. cin >> arr[i].he_so >> arr[i].he_so_mu;
  31. }
  32. }
  33. void Xuat() {
  34. bool co_so_hang = false;
  35. for (int i = 0; i < sl; i++) {
  36. if (arr[i].he_so == 0) continue;
  37. if (co_so_hang) {
  38. if (arr[i].he_so > 0) cout << " + ";
  39. else cout << " - ";
  40. } else {
  41. if (arr[i].he_so < 0) cout << "-";
  42. }
  43. int h = abs(arr[i].he_so);
  44. int m = arr[i].he_so_mu;
  45. if (m == 0) {
  46. cout << h;
  47. } else {
  48. if (h != 1) cout << h;
  49. cout << "x";
  50. if (m != 1) {
  51. if (m >= 0) cout << "^" << m;
  52. else cout << "^(" << m << ")";
  53. }
  54. }
  55. co_so_hang = true;
  56. }
  57. if (!co_so_hang) cout << "0";
  58. cout << "\n";
  59. }
  60. void TinhGiaTri(double x) {
  61. double tong = 0;
  62. for (int i = 0; i < sl; i++) {
  63. tong += arr[i].he_so * pow(x, arr[i].he_so_mu);
  64. }
  65. cout << "Gia tri cua da thuc tai " << x << " la: " << tong << "\n";
  66. }
  67. void DaoHamCap1() {
  68. bool co_so_hang = false;
  69. for (int i = 0; i < sl; i++) {
  70. if (arr[i].he_so_mu == 0 || arr[i].he_so == 0) continue;
  71. int moi_hs = arr[i].he_so * arr[i].he_so_mu;
  72. int moi_mu = arr[i].he_so_mu - 1;
  73. if (co_so_hang) {
  74. if (moi_hs > 0) cout << " + ";
  75. else cout << " - ";
  76. } else {
  77. if (moi_hs < 0) cout << "-";
  78. }
  79. int h = abs(moi_hs);
  80. if (moi_mu == 0) {
  81. cout << h;
  82. } else {
  83. if (h != 1) cout << h;
  84. cout << "x";
  85. if (moi_mu != 1) {
  86. if (moi_mu >= 0) cout << "^" << moi_mu;
  87. else cout << "^(" << moi_mu << ")";
  88. }
  89. }
  90. co_so_hang = true;
  91. }
  92. if (!co_so_hang) cout << "0";
  93. cout << "\n";
  94. }
  95. };
  96.  
  97. int main() {
  98. Bai3 a;
  99. a.Nhap();
  100. a.Xuat();
  101. double x;
  102. cin >> x;
  103. a.TinhGiaTri(x);
  104. a.DaoHamCap1();
  105. return 0;
  106. }
  107.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
0
Gia tri cua da thuc tai 9.88131e-324 la: 0
0