fork download
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <cctype>
  6. #include <iterator>
  7. #include <array>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <cstddef>
  11. #include <string>
  12. #include <sstream>
  13. #include <math.h>
  14. #include <cmath>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <algorithm>
  19.  
  20. using std::vector; using std::cout; using std::endl;
  21. using std::cin; using std::string; using std::end;
  22. using std::begin; using std::stringstream;
  23. using namespace std;
  24.  
  25. template<typename T>
  26. std::vector<T> split(const std::string& line) {
  27. std::istringstream is(line);
  28. return std::vector<T>(std::istream_iterator<T>(is), std::istream_iterator<T>());
  29. }
  30.  
  31. long long nwd(long long a, long long b){
  32. long long c;
  33. while(b != 0){
  34. c = a % b;
  35. a = b;
  36. b = c;
  37. }
  38. return a;
  39. }
  40.  
  41. void add(long long *x1, long long *y1, long long *x2, long long *y2){
  42. long long bottom = (*y1) * (*y2);
  43. long long top = ((*x1) * (*y2)) + ((*x2) * (*y1));
  44. //cout << bottom << " " << top << endl;
  45. long long frac;
  46. if(bottom != 0||top != 0){
  47. frac = nwd(top,bottom);
  48. }else{
  49. frac = 1;
  50. }
  51. string sign = "";
  52. if(top * bottom < 0){
  53. sign = "-";
  54. }else{
  55. sign = "";
  56. }
  57. printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
  58. }
  59.  
  60. void sub(long long *x1, long long *y1,long long *x2, long long *y2){
  61. long long bottom = (*y1) * (*y2);
  62. long long top = ((*x1) * (*y2)) - ((*x2) * (*y1));
  63. long long frac;
  64. if(bottom != 0||top != 0){
  65. frac = nwd(top,bottom);
  66. }else{
  67. frac = 1;
  68. }
  69. string sign = "";
  70. if(top * bottom < 0){
  71. sign = "-";
  72. }else{
  73. sign = "";
  74. }
  75. printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
  76. }
  77.  
  78. void divi(long long *x1, long long *y1, long long *x2, long long *y2){
  79. long long top = (*x1) * (*y2);
  80. long long bottom = (*x2) * (*y1);
  81. long long frac;
  82. if(bottom != 0||top != 0){
  83. frac = nwd(top,bottom);
  84. }else{
  85. frac = 1;
  86. }
  87. string sign = "";
  88. if(top * bottom < 0){
  89. sign = "-";
  90. }else{
  91. sign = "";
  92. }
  93. printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
  94. }
  95.  
  96. void mult(long long *x1, long long *y1, long long *x2, long long *y2){
  97. long long top = (*x1) * (*x2);
  98. long long bottom = (*y2) * (*y1);
  99. long long frac;
  100. if(bottom != 0||top != 0){
  101. frac = nwd(top,bottom);
  102. }else{
  103. frac = 1;
  104. }
  105. string sign = "";
  106. if(top * bottom < 0){
  107. sign = "-";
  108. }else{
  109. sign = "";
  110. }
  111. printf("%s%lld / %lld\n",sign.c_str(),abs(top/frac),abs(bottom/frac) );
  112. }
  113.  
  114. int main()
  115. {
  116.  
  117. int numOp;
  118. scanf("%d", &numOp);
  119. while(numOp != 0){
  120. long long x1,x2,y1,y2;
  121. char op[2];
  122. scanf("%lld %lld %s %lld %lld", &x1, &y1, op, &x2, &y2);
  123. if( op[0] == '+'){
  124. add(&x1, &y1, &x2,&y2);
  125. }
  126. else if(op[0] == '-'){
  127. sub(&x1,&y1,&x2,&y2);
  128. }
  129. else if(op[0] == '/'){
  130. divi(&x1,&y1,&x2,&y2);
  131. }
  132. else{
  133. mult(&x1,&y1,&x2,&y2);
  134. }
  135. numOp--;
  136. }
  137. return 0;
  138. }
Success #stdin #stdout 0s 3420KB
stdin
4
1 3 + 1 2
1 3 - 1 2
123 287 / 81 -82
12 -3 * -1 -1
stdout
5 / 6
-1 / 6
-82 / 189
-4 / 1