Advertisement
Guest User

Untitled

a guest
May 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <iostream>
  5. using namespace std;
  6. const int n = 100;
  7. char a[n + 1], v1[n + 1], v2[n + 1];
  8. char dest[n + 1];
  9. const int p = 10;
  10. int cnt, sgn;
  11. bool rev = false;
  12. void copyValueFromAtoV(char a[],char v[])
  13. {
  14. for(int i = 0;i < n;i++)
  15. v[i] = a[i];
  16. }
  17. void minus1(char a[], bool rvrs){// перевод в доп код отрицательных чисел
  18. int test = 0;
  19. for (int i = 0; i < cnt; ++i)
  20. {
  21. test = a[i] - '0';
  22. test = p - 1 - test;
  23. a[i] = test + '0';
  24. }
  25.  
  26. if(!rvrs) for (int i = cnt; i < n; ++i){ if (a[i] == '0') a[i] = '9';}
  27. if(rvrs) for (int i = cnt; i < n; ++i){ if (a[i] == '9') a[i] = '0';}
  28.  
  29. int j = 0;
  30. bool cor = false;
  31. while(char(int(a[j]) + 1) < '0' || char(int(a[j]) + 1) > '9')
  32. {
  33. cor = true;
  34. a[j] = char((int(a[j] + 1) % 56) + 46);
  35. a[j + 1] = char(int(a[j + 1] + 1));
  36. }
  37. if(j == 0 && cor == false) a[0] = char(int(a[0]) + 1);
  38. rev = false;
  39.  
  40. }
  41.  
  42. void input(char a[]){
  43. char str[n + 2];
  44. cin >> str;
  45. cnt = strlen(str);
  46. cnt--;
  47. int start = 0;
  48. if(str[0] == '-')
  49. {
  50. // int start = 1;
  51. sgn = -1;
  52. for(;start < cnt;)
  53. {
  54. a[start] = str[cnt - start];
  55. // cout << a[start] - '0' << endl;
  56. start++;
  57. }
  58.  
  59. }
  60. else
  61. {
  62. sgn = 1;
  63. for(;start <= cnt;)
  64. {
  65. a[start] = str[cnt - start];
  66. // cout << a[start] - '0' << endl;
  67. start++;
  68. }
  69. }
  70.  
  71.  
  72.  
  73.  
  74. for (; start < n; ++start) a[start] = '0';
  75. cnt = strlen(str);
  76. if (str[0] == '-')
  77. {
  78. cnt = strlen(str) - 1;
  79. //cout << a[4] << endl;
  80. //for(int i = 0;i < n;i++)
  81. // cout << a[i] << ' ' << i << '+';
  82. //cout << endl;
  83. // rev = true;
  84. minus1(a, rev);
  85. }
  86.  
  87. }
  88.  
  89. void output(char a[]){
  90. int sign = 1;
  91. if (a[n - 1] - '0' >= p / 2){
  92. sign = -1;
  93. printf("-");
  94. rev = true;
  95. minus1(a, rev);
  96. }
  97. int i = n - 1;
  98. while (i > 0 && a[i] == '0')
  99. i--;
  100. for (; i >= 0; --i){
  101. int k = a[i] - '0';
  102. cout << k;
  103. }
  104. }
  105.  
  106. void add(char dest[], char a[], char b[]){
  107. int k[n + 1];
  108. for (int i = 0; i < n + 1; i++)
  109. k[i] = a[i] - '0';
  110. int r[n + 1];
  111. for (int i = 0; i < n + 1; i++)
  112. r[i] = b[i] - '0';
  113. for (int i = 0; i < n + 1; ++i)
  114. dest[i] = '0';
  115. int t = 0, x = 0, y = 0;
  116. for (int i = 0; i < n; ++i){
  117. t = dest[i] - '0';
  118. t = k[i] + r[i] + t;
  119. x = t % p;
  120. y = t /= p;
  121. dest[i] = x + '0';
  122. dest[i + 1] = y + '0';
  123. //cout << dest[i];
  124. }
  125. cout << dest;
  126. }
  127. void sub(char a[], char b[]){
  128. rev = true;
  129. minus1(b, rev);
  130. add(dest, a, b);
  131. rev = true;
  132. minus1(dest, rev);
  133. //if(sign == -1) minus1(b);
  134. }
  135. void mul(char dest[], char a[], char b[]){// умножение
  136. for (int i = 0; i < n; ++i)
  137. dest[i] = 0;
  138. int t = 0;
  139. for (int i = 0; i < n; ++i)
  140. for (int j = 0; i + j < n; j++){
  141. t = dest[i + j] - '0' + (a[i] - '0') * (b[j] - '0');
  142. t = t % p;
  143. dest[i + j] = t;
  144. t /= p;
  145. dest[i + j + 1] += t;
  146. }
  147. }
  148. void del(char dest[], char a[], char b[]) {
  149. int i = 0;
  150. /*функция делит число a на короткое число b и возвращает остаток от деления*/
  151. /*r - обозначает текущий остаток, к которому будет приписана очередная цифра*/
  152. dest[i] = 0;
  153. //изначально остаток 0
  154. for (int i = a[0]; i >= 1; --i) {
  155. /*цикл от старших цифр к младшим*/
  156. dest[i] = dest[i] * p + a[i];
  157. //приписывание очередной цифры
  158. a[i] = dest[i] / b[i];
  159. //запись частного в результат
  160. dest[i] %= b[i];
  161. //пересчет остатка
  162. }
  163. /*Частное может содержать меньше цифр,
  164. поэтому нужно при необходимости уменьшить количество цифр*/
  165. while (a[0] > 1 && a[a[0]] == 0) {
  166. --a[0];
  167. }
  168. }
  169.  
  170. int main(){
  171. char b[n + 1];
  172. input(a);
  173. cout << a << endl;
  174. input(b);
  175. cout << b << endl;
  176. copyValueFromAtoV(a, v1);
  177. copyValueFromAtoV(b, v2);
  178. /* add(dest, v1, v2);
  179. cout << "Otvet: " << endl;
  180. output(dest); */
  181.  
  182. cout << endl << endl;
  183. copyValueFromAtoV(a, v1);
  184. copyValueFromAtoV(b, v2);
  185. sub(v1, v2);
  186. cout << "Otvet: " << endl;
  187. output(dest);
  188. cout << endl << endl;
  189.  
  190. system("pause");
  191. return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement