fork download
  1. #include <iostream>
  2. #include <cassert>
  3. #
  4.  
  5. using namespace std;
  6.  
  7. void Add(int num1, int den1, int num2, int den2, int & result_num, int & result_den)
  8. {
  9. int add_num, add_den;
  10. result_den = lcm(den1, den2);
  11. result_num = num1*den2 + den1*num2;
  12. Reduction(result_num, result_den, add_num, add_den);
  13. }
  14.  
  15. void Sub(int num1, int den1, int num2, int den2, int& result_num, int& result_den){
  16.  
  17. int sub_num, sub_den;
  18. result_den = lcm(den1, den2);
  19. result_num = num1*den2 - den1*num2;
  20. Reduction(result_num, result_den, sub_num, sub_den);
  21. }
  22.  
  23. void Mpy(int num1, int den1, int num2, int den2, int& result_num, int& result_den){
  24.  
  25.  
  26. int mpy_num, mpy_den;
  27. result_num = num1*num2;
  28. result_den = den1*den2;
  29. Reduction(result_num, result_den, mpy_num, mpy_den);
  30. }
  31.  
  32. void Div(int num1, int den1, int num2, int den2, int result_num, int result_den){
  33.  
  34. int div_num, div_den;
  35. result_num = num1*den2;
  36. result_den = num2*den1;
  37. Reduction(result_num, result_den, div_num, div_den);
  38. }
  39.  
  40. int lcm(int m, int n){
  41.  
  42.  
  43. int m1 = m / gcd(n, m); // 최소공배수 통분했을때 나오는 최대공약수 제외한 나머지 값
  44. int n1 = n / gcd(n, m);
  45.  
  46. return gcd(n, m) * m1 * n1; // 최대 공약수 랑 통분했을 시 나오는 수들의 곱셈
  47. }
  48.  
  49. int gcd(int n, int m){
  50.  
  51. assert((n > 0) && (m > 0));
  52. if (n < m)
  53. {
  54. int temp = n;
  55. n = m;
  56. m = temp;
  57.  
  58. return gcd(n%m, n);
  59. }
  60.  
  61. return gcd(n%m, m);
  62.  
  63. }
  64.  
  65. void Reduction(int num, int den, int& result_num, int& result_den) //reduction함수의 첫번째 두번째 인자는 사칙연산의 결과의 분자/분모이다.
  66. {
  67. gcd(num, den);
  68.  
  69. result_num = result_num / gcd(num, den);
  70.  
  71. result_den = result_den / gcd(num, den);
  72.  
  73. }
  74.  
  75. int main()
  76. {
  77.  
  78. int a, b, c, d;
  79. assert((b > 0) && (d > 0));
  80. int resultNum, resultDen;
  81.  
  82. cout << "분자/분모를 차례대로 입력하시오 :";
  83. cin >> a >> b >> c >> d;
  84.  
  85.  
  86. cout << "(" << a << "/" << b << ")+" << "(" << c << "/" << d << ")=";
  87.  
  88. Add(a, b, c, d, resultNum, resultDen);
  89.  
  90. cout << "(" << a << "/" << b << ")-" << "(" << c << "/" << d << ")=";
  91.  
  92. Sub(a, b, c, d, resultNum, resultDen);
  93.  
  94. cout << "(" << a << "/" << b << ")*" << "(" << c << "/" << d << ")=";
  95.  
  96. Mpy(a, b, c, d, resultNum, resultDen);
  97.  
  98. cout << "(" << a << "/" << b << ")/" << "(" << c << "/" << d << ")=";
  99.  
  100. Div(a, b, c, d, resultNum, resultDen);
  101.  
  102.  
  103. return 0;
  104. }
  105.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'void Add(int, int, int, int, int&, int&)':
prog.cpp:10:29: error: 'lcm' was not declared in this scope
  result_den = lcm(den1, den2);
                             ^
prog.cpp:12:52: error: 'Reduction' was not declared in this scope
  Reduction(result_num, result_den, add_num, add_den);
                                                    ^
prog.cpp: In function 'void Sub(int, int, int, int, int&, int&)':
prog.cpp:18:29: error: 'lcm' was not declared in this scope
  result_den = lcm(den1, den2);
                             ^
prog.cpp:20:52: error: 'Reduction' was not declared in this scope
  Reduction(result_num, result_den, sub_num, sub_den);
                                                    ^
prog.cpp: In function 'void Mpy(int, int, int, int, int&, int&)':
prog.cpp:29:52: error: 'Reduction' was not declared in this scope
  Reduction(result_num, result_den, mpy_num, mpy_den);
                                                    ^
prog.cpp: In function 'void Div(int, int, int, int, int, int)':
prog.cpp:37:52: error: 'Reduction' was not declared in this scope
  Reduction(result_num, result_den, div_num, div_den);
                                                    ^
prog.cpp: In function 'int lcm(int, int)':
prog.cpp:43:23: error: 'gcd' was not declared in this scope
  int m1 = m / gcd(n, m);  // 최소공배수 통분했을때 나오는 최대공약수 제외한 나머지 값
                       ^
stdout
Standard output is empty