fork download
  1. #include<iostream>
  2. #include<cassert>
  3.  
  4. using namespace std;
  5.  
  6. class FractionType
  7. {
  8. public:
  9.  
  10. void Initialize(int num, int den);
  11. FractionType Add(FractionType second);
  12. FractionType Sub(FractionType second);
  13. FractionType Mult(FractionType second);
  14. FractionType Div(FractionType second);
  15. void Reduction();
  16. void Print();
  17. int gcd(int m, int n);
  18. int lcm(int m, int n);
  19.  
  20. private:
  21. int num;
  22. int den;
  23. };
  24.  
  25. int main()
  26. {
  27. FractionType bunsu1, bunsu2, result_bunsu;
  28. int bunja, bunmo;
  29. int pre_bunja, pre_bunmo;
  30. cout << "분수 계산기" << endl;
  31. cout << "음수인 분수를 입력하시려면 분자에다가 음수 입력 요망" << endl;
  32. cout << "분모에 0을 넣으면 오류 발생" << endl;
  33. cout << "2개의 분수를 분자, 분모 순으로 입력해주세요 :";
  34.  
  35. cin >> bunja >> bunmo;
  36. pre_bunja = bunja;
  37. pre_bunmo = bunmo;
  38. bunsu1.Initialize(bunja, bunmo);// 분수 1개 입력완료, 초기 내용 설정
  39. //뒤의 입력을 통해 새로 입력받는 값에의해 분수 1의 분자 분모가 삭제되므로 다른 변수에 입력값을 저장해놓는다.
  40. bunsu1.Print(); // initialize에 입력한 분수를 출력(bunsu1.initialize에 초기화된값은 첫번째 분수의 분자, 분모이다.)
  41. cin >> bunja >> bunmo; // 분수 2의 분자, 분모 입력<전의 분수 1의 값은 입력으로 사라짐>
  42. bunsu2.Initialize(bunja, bunmo); //두번째 분수 입력완료, 초기 내용 설정
  43. cout << "+";
  44. bunsu2.Print();//initialize에 입력한 분수를 출력(bunsu2.initialize에 초기화된값은 두번째 분수의 분자, 분모이다.)
  45. cout << "=";
  46. result_bunsu = bunsu1.Add(bunsu2); //계산식 연산<첫번째분수는 private의 저장된값으로, 두번째분수는 인자로써 들어가 연산>
  47. result_bunsu.Reduction();
  48. result_bunsu.Print();
  49.  
  50. bunsu1.Initialize(bunja, bunmo); // 분수 1개 입력완료, 초기 내용 설정
  51. bunsu1.Print(); // initialize에 입력한 분수를 출력(bunsu1.initialize에 초기화된값은 첫번째 분수의 분자, 분모이다.)
  52. bunsu2.Initialize(bunja, bunmo); //두번째 분수 입력완료, 초기 내용 설정
  53. cout << "-";
  54. bunsu2.Print();//initialize에 입력한 분수를 출력(bunsu2.initialize에 초기화된값은 두번째 분수의 분자, 분모이다.)
  55. cout << "=";
  56. result_bunsu = bunsu1.Sub(bunsu2);
  57.  
  58. result_bunsu.Reduction();
  59. result_bunsu.Print();
  60. cout << endl;
  61.  
  62. bunsu1.Initialize(bunja, bunmo); // 분수 1개 입력완료, 초기 내용 설정
  63. bunsu1.Print(); // initialize에 입력한 분수를 출력(bunsu1.initialize에 초기화된값은 첫번째 분수의 분자, 분모이다.)
  64. bunsu2.Initialize(bunja, bunmo); //두번째 분수 입력완료, 초기 내용 설정
  65. cout << "*";
  66. bunsu2.Print();//initialize에 입력한 분수를 출력(bunsu2.initialize에 초기화된값은 두번째 분수의 분자, 분모이다.)
  67. cout << "=";
  68. result_bunsu = bunsu1.Mult(bunsu2);
  69.  
  70. result_bunsu.Reduction();
  71. result_bunsu.Print();
  72. cout << endl;
  73.  
  74. bunsu1.Initialize(bunja, bunmo); // 분수 1개 입력완료, 초기 내용 설정
  75. bunsu1.Print(); // initialize에 입력한 분수를 출력(bunsu1.initialize에 초기화된값은 첫번째 분수의 분자, 분모이다.)
  76. bunsu2.Initialize(bunja, bunmo); //두번째 분수 입력완료, 초기 내용 설정
  77. cout << "/";
  78. bunsu2.Print();//initialize에 입력한 분수를 출력(bunsu2.initialize에 초기화된값은 두번째 분수의 분자, 분모이다.)
  79. cout << "=";
  80. result_bunsu = bunsu1.Div(bunsu2);
  81.  
  82. result_bunsu.Reduction();
  83. result_bunsu.Print();
  84.  
  85.  
  86.  
  87. return 0;
  88. }
  89.  
  90. void FractionType::Initialize(int input_num, int input_den) //Initialize : 초기 내용 설정, 수를 입력받은 것을 class의 private의 num, den에 저장
  91. {
  92.  
  93. num = input_num;
  94. den = input_den;
  95.  
  96. }
  97.  
  98. FractionType FractionType::Add(FractionType second){
  99.  
  100. FractionType result_bunsu;
  101.  
  102. result_bunsu.num = (num * second.den) + (second.num * den);
  103. result_bunsu.den = lcm(den, second.den);
  104.  
  105. return result_bunsu;
  106. }
  107. FractionType FractionType::Sub(FractionType second){
  108.  
  109. FractionType result_bunsu;
  110.  
  111. result_bunsu.num = (num * second.den) - (second.num * den);
  112. result_bunsu.den = lcm(den, second.den);
  113.  
  114. return result_bunsu;
  115. }
  116. FractionType FractionType::Mult(FractionType second){
  117.  
  118. FractionType result_bunsu;
  119.  
  120. result_bunsu.num = num* second.num;
  121. result_bunsu.den = den*second.den;
  122.  
  123. return result_bunsu;
  124. }
  125. FractionType FractionType::Div(FractionType second){
  126.  
  127. FractionType result_bunsu;
  128.  
  129. result_bunsu.num = num*second.den;
  130. result_bunsu.den = den*second.num;
  131.  
  132. return result_bunsu;
  133. }
  134. void FractionType::Reduction()
  135. {
  136. gcd(num, den);
  137. num = num / gcd(num, den);
  138. den = den / gcd(num, den);
  139. }
  140.  
  141. void FractionType::Print()
  142. {
  143. if (den != 1){
  144. cout <<"("<< num << "/" << den <<")";
  145. }
  146. else
  147. {
  148. cout << num;
  149. }
  150. }
  151.  
  152. int FractionType::gcd(int m, int n) {
  153.  
  154.  
  155. if (n > m) // 최대공약수 통분할때 뒷수가 앞수보다 크면 자리 배치 바꿈, 손으로 쓰는 통분은 앞수가 작아도 되는데 이 방법은 좀 상이해서 바꿔줘야한다.
  156. {
  157. int temp;
  158. temp = n;
  159. n = m;
  160. m = temp; // 자리를 바꾸었다.
  161.  
  162. if (m == 0){ // 중첩 if문 만약 gcd에 0이들어오면<그럴일은 없겠지만...> 그냥 n이 최대공약수
  163. return n;
  164. }
  165. else // 0이아니면
  166. {
  167. return gcd(m, n%m);
  168. }
  169.  
  170. }
  171. else
  172. {
  173. if (m == 0)
  174. return n;
  175. else
  176. return gcd(m, n%m);
  177.  
  178. }
  179.  
  180. }
  181. int FractionType::lcm(int m, int n)
  182. {
  183. assert(m>0 && n>0);
  184.  
  185. int m1 = m / gcd(m, n); // 최소공배수 통분했을때 나오는 최대공약수 제외한 나머지 값
  186. int n1 = n / gcd(m, n);
  187.  
  188. return gcd(n, m) * m1 * n1; // 최대 공약수 랑 통분했을 시 나오는 수들의 곱셈
  189.  
  190. }
  191.  
Time limit exceeded #stdin #stdout 5s 3144KB
stdin
Standard input is empty
stdout
분수 계산기
음수인 분수를 입력하시려면 분자에다가 음수 입력 요망
분모에 0을 넣으면 오류 발생
2개의 분수를 분자, 분모 순으로 입력해주세요 :