Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const long LEN = 1000004;//troche wiecej, by dmuchac na zimno
  6.  
  7. struct readResult{
  8. bool isABigger;
  9. bool isANegative;
  10. bool isBNegative;
  11. };
  12.  
  13. void readN(char *n, string s){
  14. long ss = s.size();
  15. long comma = 0;
  16. for(long i=1; i<=ss; i++){
  17. if(s[ss-i] == ',')
  18. comma = i;
  19. }
  20. for(long i=1; i<=ss; i++){
  21. if(s[ss-i] == ',')
  22. continue;
  23. n[LEN-102+comma-(i>comma)-i] = s[ss-i] - '0';
  24. }
  25. }
  26.  
  27. readResult read2Nums(char *a, char *b){
  28. string sa, sb;
  29. cin >> sa >> sb;
  30. bool isANegative = (sa[0] == '-');
  31. bool isBNegative = (sb[0] == '-');
  32. readN(a, sa.substr(isANegative || (sa[0] == '+'), string::npos));
  33. readN(b, sb.substr(isBNegative || (sb[0] == '+'), string::npos));
  34.  
  35. bool isABigger = false;
  36. for(long i=0; i<LEN; i++){
  37. if(a[i] > b[i]){
  38. isABigger = true;
  39. break;
  40. }else if(b[i] > a[i]){
  41. break;
  42. }
  43. }
  44.  
  45. return {
  46. isABigger,
  47. isANegative,
  48. isBNegative
  49. };
  50. }
  51.  
  52. void add(char *a, char *b){
  53. char r = 0;
  54. for(long i=LEN-1; i>=0; i--){
  55. a[i] = r + a[i] + b[i];
  56. r = a[i] / 10;
  57. a[i]%= 10;
  58. }
  59. }
  60.  
  61. void substract(char *a, char *b){
  62. long j;
  63. for(long i=LEN-1; i>=0; i--){
  64. if(a[i] < b[i]){
  65. a[i]+= 10;
  66. j = 1;
  67. while(true){
  68. if(a[i-j] == 0){
  69. a[i-j] = 9;
  70. }else{
  71. a[i-j]--;
  72. break;
  73. }
  74. j++;
  75. }
  76. }
  77. a[i]-= b[i];
  78. }
  79. }
  80.  
  81. bool isZero(char *n){
  82. for(long i=0; i<LEN; i++){
  83. if(n[i] != 0) return false;
  84. }
  85. return true;
  86. }
  87.  
  88. int main(){
  89. ios_base::sync_with_stdio(0);
  90. cin.tie(0);
  91. cout.tie(0);
  92.  
  93. char a[LEN] = {0};
  94. char b[LEN] = {0};
  95. readResult result = read2Nums(a, b);
  96.  
  97. char *bigger = (result.isABigger) ? a : b;
  98. char *smaller = (result.isABigger) ? b : a;
  99.  
  100. if(!result.isANegative && !result.isBNegative){
  101. add(bigger, smaller);
  102. }else if(result.isANegative && result.isBNegative){
  103. add(bigger, smaller);
  104. if(!isZero(bigger)) cout << '-';
  105. }else{
  106. substract(bigger, smaller);
  107. if(!isZero(bigger)){
  108. if(result.isABigger){
  109. if(result.isANegative) cout << '-';
  110. }else{
  111. if(result.isBNegative) cout << '-';
  112. }
  113. }
  114. }
  115.  
  116. bool flag = false;
  117. for(long i=0; i<LEN; i++){
  118. if(!flag){
  119. if(bigger[i] != 0){
  120. flag = true;
  121. cout << int(bigger[i]);
  122. }
  123. } else{
  124. cout << int(bigger[i]);
  125. }
  126. }
  127. if(!flag) cout << 0;
  128.  
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement