Advertisement
Guest User

Untitled

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