fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double rect_integral(double a, double b, int n, int choice);
  7. double trap_integral(double a, double b, int n, int choice);
  8.  
  9. class FunctionInterface
  10. {
  11. public:
  12. virtual ~FunctionInterface() {};
  13. virtual double Calculate(double x) = 0;
  14. virtual std::string GetName() = 0;
  15. };
  16.  
  17. class Function1: public FunctionInterface
  18. {
  19. public:
  20. FunctionInterface() {}
  21.  
  22. double Calculate (double x)
  23. {
  24. return 2.0*pow(x,5.0) +pow(x,3.0) -10*x +2;
  25. }
  26.  
  27. std::string GetName()
  28. {
  29. return m_Name;
  30. }
  31. private:
  32. static const std::string m_Name = "2x^5 + x^3 -10x+2";
  33. };
  34.  
  35. class Function2: public FunctionInterface
  36. {
  37. public:
  38. FunctionInterface() {}
  39.  
  40. double Calculate (double x)
  41. {
  42. return 6*pow(x,2.0) -x +10;
  43. }
  44.  
  45. std::string GetName()
  46. {
  47. return m_Name;
  48. }
  49. private:
  50. static const m_Name = "6x^2 - x +10";
  51. };
  52.  
  53.  
  54.  
  55. int main () {
  56. FunctionInterface* functions[2];
  57. functions[0] = new Function1();
  58. functions[1] = new Function2();
  59.  
  60. start:
  61. int choice;
  62. int n;
  63. double a;
  64. double b;
  65. bool trap = false;
  66. bool rect = false;
  67.  
  68. cout << "Functions available: \n\n";
  69. for (int i=0; i<2; i++)
  70. cout << " " << i << " " << functions[i].GetName() << '\n';
  71. cout << endl;
  72.  
  73. cout << "Choose a function (1, 2, 3, 4, 5, other(quit)) " << endl;
  74. cin >> choice;
  75.  
  76. if (choice >= 0 && choice < 2)
  77. {
  78. char method = ' ';
  79. do {
  80. cout << "Would you like to calculate the area using the rectangle,"
  81. << " trapezoid, or both (1, 2, 3): " << endl;
  82. cin >> method;
  83. } while (method < '1' || method > '3');
  84.  
  85. switch (method) {
  86. case '1':
  87. rect = true;
  88. cout <<"How many rectangles do you want? " << endl;
  89. break;
  90. case '2':
  91. trap = true;
  92. cout <<"How many trapezoids do you want? " << endl;
  93. break;
  94. case '3':
  95. trap = true;
  96. rect = true;
  97. cout <<"How many rectangles/trapezoids do you want? " << endl;
  98. }
  99.  
  100. cin >> n;
  101.  
  102. cout <<"Please select a starting point, a: " << endl;
  103. cin >> a;
  104.  
  105. cout <<"Please select an ending point, b: " << endl;
  106. cin >> b;
  107.  
  108. if (rect) {
  109. cout << "The area under " << function[choice]->GetName()
  110. << " between " << a << " and " << b
  111. << " is: " << rect_integral(a, b, n, *function[choice]) <<" in rectangles" << endl;
  112. }
  113.  
  114. if (trap) {
  115. cout << "The area under " << function[choice]->GetName()
  116. << " between " << a << " and " << b
  117. << " is: " << trap_integral(a, b, n, *function[choice]) << " in trapezoids" << endl;
  118. }
  119. }
  120. int again;
  121. cout << "Would you like to go again? Yes = 1 " <<endl;
  122. cin >> again;
  123.  
  124. if (again == 1)
  125. goto start;
  126.  
  127. return 0;
  128. }
  129. // end of int main() start of functions
  130.  
  131.  
  132. double rect_integral(double a, double b, int n, FunctionInterface& function)
  133. {
  134. double width = (b-a)/n;
  135.  
  136. double x = a;
  137. double total_area = 0;
  138. for(int i = 1; i <= n; i++) {
  139. x = width *i;
  140. total_area += width * function.Calculate(x);
  141. }
  142.  
  143. return total_area;
  144. }
  145.  
  146. double trap_integral(double a, double b, int n, FunctionInterface& function)
  147. {
  148. double x =a;
  149.  
  150. double interiml, interimr;
  151. double total_area = 0;
  152. for(int i = 1; i <= n; i++) {
  153. x = width *i;
  154. interiml = width * fx(x);
  155. x = width*i;
  156. interimr = width * function.Calculate(x);
  157. total_area += (interiml + interimr)/2;
  158. }
  159.  
  160. return total_area;
  161. }
  162.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:20:23: error: ISO C++ forbids declaration of ‘FunctionInterface’ with no type [-fpermissive]
prog.cpp:32:39: error: invalid in-class initialization of static data member of non-integral type ‘const string {aka const std::basic_string<char>}’
prog.cpp:32:39: error: initializer invalid for static member with constructor
prog.cpp:32:39: error: (an out of class initialization is required)
prog.cpp:32:39: error: ‘Function1::m_Name’ cannot be initialized by a non-constant expression when being declared
prog.cpp: In member function ‘int Function1::FunctionInterface()’:
prog.cpp:20:26: warning: no return statement in function returning non-void [-Wreturn-type]
prog.cpp: At global scope:
prog.cpp:38:23: error: ISO C++ forbids declaration of ‘FunctionInterface’ with no type [-fpermissive]
prog.cpp:50:18: error: ‘m_Name’ does not name a type
prog.cpp: In member function ‘int Function2::FunctionInterface()’:
prog.cpp:38:26: warning: no return statement in function returning non-void [-Wreturn-type]
prog.cpp: In member function ‘virtual std::string Function2::GetName()’:
prog.cpp:47:16: error: ‘m_Name’ was not declared in this scope
prog.cpp: In function ‘int main()’:
prog.cpp:70:51: error: request for member ‘GetName’ in ‘functions[i]’, which is of pointer type ‘FunctionInterface*’ (maybe you meant to use ‘->’ ?)
prog.cpp:109:42: error: ‘function’ was not declared in this scope
prog.cpp:115:42: error: ‘function’ was not declared in this scope
prog.cpp: In function ‘double trap_integral(double, double, int, FunctionInterface&)’:
prog.cpp:153:13: error: ‘width’ was not declared in this scope
prog.cpp:154:32: error: ‘fx’ was not declared in this scope
prog.cpp: In member function ‘virtual std::string Function2::GetName()’:
prog.cpp:48:5: warning: control reaches end of non-void function [-Wreturn-type]
stdout
Standard output is empty