fork download
  1. #include "MixedExpression.h"
  2. #include<iostream>
  3. #include<fstream>
  4. #include<cmath>
  5. using namespace std;
  6.  
  7. // Convert a mixed expression to its normal form.
  8. void MixedExpression::reduce(void)
  9. {
  10. long numerator = a*c + b; // Holds the numerator.
  11. long remainder = numerator % c; // Holds the remainder.
  12. long dividend = numerator / c; // Holds the dividend.
  13. long gcd = 1; // Holds the greatest common divisor.
  14.  
  15. // Get the greatest common divisor.
  16. for (int i = 2; i <= remainder; i++)
  17. {
  18. if((c % i ==0) && (remainder % i==0))
  19. {
  20. gcd = i;
  21. }
  22. }
  23. remainder /= gcd; // Divide the remainder by the GCD.
  24. c /= gcd; // Divide the denominator by the GCD.
  25. a = dividend; // Set a equal to the dividend.
  26. b = remainder; // Set b equal to the remainder.
  27. if(b == 0) // If b is zero, set to default.
  28. {
  29. c = 1;
  30. }
  31. }
  32.  
  33. // Set three values in the default constructor to 0.
  34. MixedExpression::MixedExpression()
  35. {
  36. a = 0;
  37. b = 0;
  38. c = 1;
  39. }
  40.  
  41. // Set three values in the normal constructor equal to the arguments.
  42. MixedExpression::MixedExpression(long d, long e, long f)
  43. {
  44. a = d;
  45. b = e;
  46. c = f;
  47. }
  48.  
  49. // Add two mixed expressions and return the result.
  50. MixedExpression MixedExpression::add(MixedExpression op)
  51. {
  52. MixedExpression res;
  53. res.a = a + op.a;
  54. res.b = (b * op.c) + (op.b * c);
  55. res.c = c * op.c;
  56. return MixedExpression(res.a,res.b,res.c);
  57. }
  58.  
  59. // Subtract two mixed expressions and return the result.
  60. MixedExpression MixedExpression::subtract(MixedExpression op)
  61. {
  62. MixedExpression res;
  63. res.a = a - op.a;
  64. res.b = (b * op.c) - (op.b * c);
  65. res.c = c * op.c;
  66. return MixedExpression(res.a,res.b,res.c);
  67. }
  68.  
  69. // Multiply two mixed expressions and return the result.
  70. MixedExpression MixedExpression::multiply(MixedExpression op)
  71. {
  72. long numerator;
  73. long denominator;
  74. long remainder;
  75. MixedExpression res;
  76.  
  77. numerator = (a*c + b) * (op.a * op.c + op.b);
  78. denominator = c * op.c;
  79. remainder = numerator % denominator;
  80. res.a = numerator/denominator;
  81. return MixedExpression(res.a,remainder,denominator);
  82. }
  83.  
  84. // Divide two mixed expressions and return the result.
  85. MixedExpression MixedExpression::divide(MixedExpression op)
  86. {
  87. long numerator;
  88. long denominator;
  89. long remainder;
  90. MixedExpression res;
  91.  
  92. numerator = (a*c + b) * (op.c);
  93. denominator = c * (op.a * op.c + op.b);
  94. remainder = numerator % denominator;
  95. res.a = numerator/denominator;
  96. return MixedExpression(res.a,remainder,denominator);
  97. }
  98.  
  99. // Read each value and character.
  100. void MixedExpression::ReadMixedExp(istream &in)
  101. {
  102. char remove;
  103. in >> remove >> a >> remove >> b >> remove >> c >> remove;
  104. }
  105.  
  106. // Print the results.
  107. void MixedExpression::printData(ostream &out)
  108. {
  109. reduce();
  110. out << "( " << a << " + " << b << " / " << c << " )";
  111. }
  112.  
  113.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:29: fatal error: MixedExpression.h: No such file or directory
 #include "MixedExpression.h"
                             ^
compilation terminated.
stdout
Standard output is empty