fork(1) download
  1. #include<iostream>
  2. #include<cassert>
  3.  
  4. using namespace std;
  5.  
  6. struct FractionType
  7. {
  8. int num;
  9. int den;
  10.  
  11. };
  12. FractionType Add(FractionType a, FractionType b);// FractionType의 자료형을 가진 Add함수 인자는 FractionType의 자료형의 a<이름>(a안에 num, den존재)와 b(b안에 num, den 존재) a란 존재를 분수의 구성요소라 봐도됨
  13. FractionType Sub(FractionType a, FractionType b);
  14. FractionType Mpy(FractionType a, FractionType b);
  15. FractionType Div(FractionType a, FractionType b);
  16. void Reduction(FractionType& a);
  17. void Reduction_NoPrint(FractionType& a);
  18. int lcm(int m, int n);
  19. int gcd(int m, int n);
  20. int exit_Func();
  21.  
  22. int main()
  23. {
  24. FractionType a, b;
  25. cout << "분자/분모를 차례대로 입력하시오(분모에 0넣으면 Error!)" << endl; // 기본적으로 분모가 0인 분수는 없다.
  26. cout << "(음수인 분수는 분자에 음수를 입력해주세요 !! : "; //사용자가 입력시에 유의사항을 주었다. 음수 분수 입력하고싶으면 분자에다가 넣어라!
  27. cin >> a.num >> a.den >> b.num >> b.den;
  28.  
  29. assert(a.den != 0 && b.den != 0); // 분수의 분모는 절때 0이 되어선 안된다. 절대로!! (메인함수에 넣어주면 굳이 다른 함수에서 0이 될 걱정이없음 분모가)
  30. assert(a.den > 0 && b.den > 0); //입력받는 값이 음수인 분수일때 분자에 음수를 입력하는 조건이 있다.. 이를 위해 분모에 음수를 입력하면 바로 Debuging에러를 뜨게끔 해놓았다.
  31.  
  32. cout << "(" << a.num << "/" << a.den << ")+" << "(" <<b.num << "/" << b.den << ")=";
  33. Reduction(Add(a, b));
  34.  
  35. cout << "(" << a.num << "/" << a.den << ")-" << "(" << b.num << "/" << b.den << ")=";
  36. Reduction(Sub(a, b));
  37.  
  38. cout << "(" << a.num << "/" << a.den << ")*" << "(" << b.num << "/" << b.den << ")=";
  39. Reduction(Mpy(a, b));
  40.  
  41. cout << "(" << a.num << "/" << a.den << ")/" << "(" << b.num << "/" << b.den << ")=";
  42. Reduction(Div(a, b));
  43.  
  44. cout << endl;
  45. return 0; // 종료
  46. }
  47.  
  48.  
  49. FractionType Add(FractionType a, FractionType b)
  50. {
  51. FractionType result;
  52. Reduction_NoPrint(a);
  53. Reduction_NoPrint(b);
  54. result.den = lcm(a.den, b.den);
  55. result.num = a.num*(result.den / a.den) + b.num*(result.den / b.den);
  56.  
  57. return result;
  58.  
  59. }
  60.  
  61. FractionType Sub(FractionType a, FractionType b)
  62. {
  63. FractionType result;
  64. Reduction_NoPrint(a);
  65. Reduction_NoPrint(b);
  66. result.den = lcm(a.den, b.den);
  67. result.num = a.num*(result.den / a.den) - b.num*(result.den / b.den);
  68.  
  69. return result;
  70.  
  71. }
  72. FractionType Mpy(FractionType a, FractionType b)
  73. {
  74. FractionType result;
  75. Reduction_NoPrint(a);
  76. Reduction_NoPrint(b);
  77. result.den = a.den*b.den;
  78. result.num = a.num * b.num;
  79. return result;
  80. }
  81.  
  82. FractionType Div(FractionType a, FractionType b)
  83. {
  84. FractionType result;
  85. Reduction_NoPrint(a);
  86. Reduction_NoPrint(b);
  87. result.den = a.den * b.num;
  88. result.num = a.num * b.den;
  89.  
  90. if (result.den == 0)
  91. {
  92. cout << "분모엔 0이 못들어가지요.. 에러코드방출!" << endl;
  93. assert(result.den != 0);
  94. }
  95. else
  96. {
  97.  
  98. }
  99. return result;
  100. }
  101.  
  102.  
  103.  
  104.  
  105. int lcm(int m, int n)
  106. {
  107. assert(m>0 && n>0);
  108.  
  109. int m1 = m / gcd(n, m); // 최소공배수 통분했을때 나오는 최대공약수 제외한 나머지 값
  110. int n1 = n / gcd(n, m);
  111.  
  112. return gcd(n, m) * m1 * n1; // 최대 공약수 랑 통분했을 시 나오는 수들의 곱셈
  113.  
  114. }
  115. int gcd(int n, int m) {
  116.  
  117.  
  118.  
  119. if (m > n) // 최대공약수 통분할때 뒷수가 앞수보다 크면 자리 배치 바꿈, 손으로 쓰는 통분은 앞수가 작아도 되는데 이 방법은 좀 상이해서 바꿔줘야한다.
  120. {
  121. int temp;
  122. temp = n;
  123. n = m;
  124. m = temp; // 자리를 바꾸었다.
  125.  
  126. if (m == 0){ // 중첩 if문 만약 gcd에 0이들어오면<그럴일은 없겠지만...> 그냥 n이 최대공약수
  127. return n;
  128. }
  129. else // 0이아니면
  130. {
  131. return gcd(m, n%m);
  132. }
  133.  
  134. }
  135. else
  136. {
  137. if (m == 0)
  138. return n;
  139. else
  140. return gcd(m, n%m);
  141.  
  142. }
  143.  
  144. }
  145.  
  146.  
  147. void Reduction(FractionType& a)
  148. {
  149.  
  150. FractionType real_result;
  151. if (a.num < 0 && a.den > 0) // Add함수와 Sub함수를 위한 조건(덧셈/뺄셈은 lcm<분모계산식>으로인해 분모는 양수가나올수 밖에없으니 만약 계산결과값이 음수라면 필자가 위에서 분자에 음수를 넣게끔 분자 계산방법이 짜져있을것이다.
  152. {
  153. int num;
  154. num = -a.num;
  155.  
  156. gcd(num, a.den);
  157.  
  158. real_result.num = (a.num / gcd(num, a.den)); //음수인 함수를 약분하는 것이므로 num에 양수화를 시켰으면 다시 결과값은 되돌려서 주는게 맞다.
  159. //만약 이방법으로 하기 싫다면 지역변수를 하나 선언하여 양수화된 값을 그 지역변수에 넣고 gcd를 돌린다.
  160.  
  161. real_result.den = (a.den / gcd(num, a.den));
  162. }
  163. else if (a.num > 0 && a.den < 0) // Mpy함수와 Div 함수를 위한 조건(곱셈/뺄셈은 굳이 통분해서 계산하는게 아니라 그냥 곱해서 약분만 하면되므로 결과가 음수가 나온 분수를 약분하려면 양수화시켜야하므로 사용
  164. {
  165. int den;
  166. den = -a.den;
  167.  
  168. gcd(a.num, den);
  169.  
  170. real_result.num = -(a.num / gcd(a.num, den)); // 이것도 연산값은 음수이므로 통일 되게끔 분자에 음수를 넣어주겠다.
  171.  
  172. real_result.den = (a.den / gcd(a.num, den));
  173.  
  174. }
  175. else if (a.num < 0 && a.den < 0) //분자 분모 둘다 음수일경우 둘다 양수화해서 gcd처리를 하고 어짜피 음수가 2개이면 양수이므로 양수화 시킨다.
  176. {
  177. int num, den;
  178. num = -a.num;
  179. den = -a.den;
  180.  
  181. gcd(num, den);
  182.  
  183. real_result.num = (num / gcd(num, den));
  184.  
  185. real_result.den = (den / gcd(num, den));
  186.  
  187. }
  188. else
  189. { // 그냥 양수 분수를 약분하는 것이다.
  190. gcd(a.num, a.den);
  191.  
  192. real_result.num = (a.num / gcd(a.num, a.den));
  193.  
  194. real_result.den = (a.den / gcd(a.num, a.den));
  195. }
  196.  
  197.  
  198. if (real_result.den == 1)
  199. {
  200. cout << real_result.num << endl; //약분 결과값의 분모가 1이면 정수출력
  201. }
  202. else if (real_result.den == 0)
  203. {
  204. exit_Func(); //약분 결과값의 분모가 0이면 프로그램 종료
  205. }
  206. else
  207. {
  208. cout << real_result.num << "/" <<real_result.den<< endl;
  209.  
  210. }
  211.  
  212.  
  213.  
  214. }
  215. void Reduction_NoPrint(FractionType& a)
  216. {
  217. FractionType real_result;
  218. if (a.num < 0 && a.den > 0) // Add함수와 Sub함수를 위한 조건(덧셈/뺄셈은 lcm<분모계산식>으로인해 분모는 양수가나올수 밖에없으니 만약 계산결과값이 음수라면 필자가 위에서 분자에 음수를 넣게끔 분자 계산방법이 짜져있을것이다.
  219. {
  220. int num;
  221. num = -a.num;
  222.  
  223. gcd(num, a.den);
  224.  
  225. real_result.num = (a.num / gcd(num, a.den)); //음수인 함수를 약분하는 것이므로 num에 양수화를 시켰으면 다시 결과값은 되돌려서 주는게 맞다.
  226. //만약 이방법으로 하기 싫다면 지역변수를 하나 선언하여 양수화된 값을 그 지역변수에 넣고 gcd를 돌린다.
  227.  
  228. real_result.den = (a.den / gcd(num, a.den));
  229. }
  230. else if (a.num > 0 && a.den < 0) // Mpy함수와 Div 함수를 위한 조건(곱셈/뺄셈은 굳이 통분해서 계산하는게 아니라 그냥 곱해서 약분만 하면되므로 결과가 음수가 나온 분수를 약분하려면 양수화시켜야하므로 사용
  231. {
  232. int den;
  233. den = -a.den;
  234.  
  235. gcd(a.num, den);
  236.  
  237. real_result.num = -(a.num / gcd(a.num, den)); // 이것도 연산값은 음수이므로 통일 되게끔 분자에 음수를 넣어주겠다.
  238.  
  239. real_result.den = (a.den / gcd(a.num, den));
  240.  
  241. }
  242. else if (a.num < 0 && a.den < 0) //분자 분모 둘다 음수일경우 둘다 양수화해서 gcd처리를 하고 어짜피 음수가 2개이면 양수이므로 양수화 시킨다.
  243. {
  244. int num, den;
  245. num = -a.num;
  246. den = -a.den;
  247.  
  248. gcd(num, den);
  249.  
  250. real_result.num = (num / gcd(num, den));
  251.  
  252. real_result.den = (den / gcd(num, den));
  253.  
  254. }
  255. else
  256. { // 그냥 양수 분수를 약분하는 것이다.
  257. gcd(a.num, a.den);
  258.  
  259. real_result.num = (a.num / gcd(a.num, a.den));
  260.  
  261. real_result.den = (a.den / gcd(a.num, a.den));
  262. }
  263. }
  264.  
  265. int exit_Func()
  266. {
  267. return 0;
  268. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:33:21: error: invalid initialization of non-const reference of type 'FractionType&' from an rvalue of type 'FractionType'
  Reduction(Add(a, b));
                     ^
prog.cpp:16:6: note: in passing argument 1 of 'void Reduction(FractionType&)'
 void Reduction(FractionType& a);
      ^
prog.cpp:36:21: error: invalid initialization of non-const reference of type 'FractionType&' from an rvalue of type 'FractionType'
  Reduction(Sub(a, b));
                     ^
prog.cpp:16:6: note: in passing argument 1 of 'void Reduction(FractionType&)'
 void Reduction(FractionType& a);
      ^
prog.cpp:39:21: error: invalid initialization of non-const reference of type 'FractionType&' from an rvalue of type 'FractionType'
  Reduction(Mpy(a, b));
                     ^
prog.cpp:16:6: note: in passing argument 1 of 'void Reduction(FractionType&)'
 void Reduction(FractionType& a);
      ^
prog.cpp:42:21: error: invalid initialization of non-const reference of type 'FractionType&' from an rvalue of type 'FractionType'
  Reduction(Div(a, b));
                     ^
prog.cpp:16:6: note: in passing argument 1 of 'void Reduction(FractionType&)'
 void Reduction(FractionType& a);
      ^
stdout
Standard output is empty