Guest User

Untitled

a guest
Dec 14th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<stdlib.h>
  6. const int N = 1000;
  7.  
  8. int complong(int *a, int *b) {
  9. int i = 0;
  10. if (a[0] < b[0]) return -1;
  11. if (a[0] > b[0]) return 1;
  12. for (i = a[0]; i > 0; i--) {
  13. if (a[i] < b[i]) return -1;
  14. if (a[i] > b[i]) return 1;
  15. }
  16. return 0;
  17. }
  18.  
  19. void readlong(int *a) {
  20. int i=0,k=0;
  21. char s[N+1];
  22.  
  23. gets_s(s,N+1);
  24. a[0] = strlen(s);
  25. for (i = 0; i <a[0]; i++) {
  26. a[a[0] - i] = s[i] - '0';
  27. }
  28.  
  29. }
  30.  
  31. void writelong(int *a) {
  32. int i = 0;
  33. for (i = a[0]; i >= 1; i--)
  34. printf("%d", a[i]);
  35.  
  36. }
  37.  
  38. void main(void) {
  39. int a[N + 5] = { 0 };
  40. int b[N + 5] = { 0 };
  41. int y[N + 1] = { 0 };
  42.  
  43. int m = 0;
  44. int c = 0;
  45. int i = 0;
  46. char p = 0,g=0;
  47. int j = 0;
  48. int k = 0;
  49. int cr = 0;
  50. int u = 0;
  51. int l = 0;
  52. int q = 0;
  53. printf("Enter the opearation");
  54. scanf_s("%c", &p);
  55. scanf_s("%*c", &p);
  56. fflush(stdin);
  57. switch (p) {
  58. case'!':
  59. {
  60. printf("Enter the number to factorize ");
  61. scanf_s("%d", &l);
  62. int d = 1000000;
  63. int A[N] = { 1 };
  64. int len = 1, r, s;
  65. for (k = 2; k <= l; k++) {
  66. i = 0; //
  67. r = 0; //
  68. while (i < len
  69. || r > 0) {
  70. s = A[i] * k + r;
  71. A[i] = s % d;
  72. r = s / d;
  73. i++;
  74. }
  75. len = i;
  76. }
  77. for (i = len - 1; i >= 0; i--)
  78. if (i == len - 1) printf("%d", A[i]);
  79. else printf("%.6d", A[i]);
  80. break;
  81.  
  82. }
  83.  
  84. case'-': {
  85.  
  86.  
  87. printf("Enter the first number ");
  88.  
  89.  
  90. readlong(a);
  91. printf("nEnter the second number ");
  92. readlong(b);
  93.  
  94.  
  95. if (complong(a, b) < 0) {
  96. printf("-");
  97. c = 0;
  98. for (i = 1; i <= b[0]; i++) {
  99. c = c + b[i] - a[i] + 10;
  100. b[i] = c % 10;
  101. if (c < 10) { c = -1; }
  102. else { c = 0; }
  103. }
  104. while ((b[b[0]] == 0 && b[0] > 1)) b[0] = b[0] - 1;
  105. writelong(b);
  106. break;
  107. }
  108. else {
  109. c = 0;
  110. for (i = 1; i <= a[0]; i++) {
  111. c = c + a[i] - b[i] + 10;
  112. a[i] = c % 10;
  113. if (c < 10) { c = -1; }
  114. else { c = 0; }
  115. }
  116. while ((a[a[0]] == 0 && a[0] > 1)) a[0] = a[0] - 1;
  117. writelong(a);
  118. }
  119. break;
  120. }
  121.  
  122.  
  123.  
  124. case'+': {
  125. printf("Enter the first number ");
  126.  
  127.  
  128. readlong(a);
  129. printf("nEnter the second number ");
  130. readlong(b);
  131. if (a[0] > b[0]) {
  132. m = a[0];
  133. }
  134. else { m = b[0]; }
  135. c = 0;
  136. for (i = 1; i <= m; i++) {
  137. c = c + a[i] + b[i];
  138. a[i] = c % 10;
  139. c = c / 10;
  140. }
  141. if (c > 0) {
  142. m = m + 1;
  143. a[m] = c;
  144. }
  145. a[0] = m;
  146. writelong(a);
  147. break;
  148. }
  149.  
  150. case'*': {
  151.  
  152. printf("Enter the first number ");
  153. readlong(a);
  154. printf("nEnter the second number ");
  155. readlong(b);
  156.  
  157. cr = 0;
  158. for (i = 1; i <= a[0]; i++) {
  159. for (j = 1; j <= b[0]; j++) {
  160. cr = a[i] * b[j];
  161. k = i + j - 1;
  162. while (cr > 0) {
  163. cr = cr + y[k];
  164. y[k] = cr % 10;
  165. cr = cr / 10;
  166. if (k > y[0]) y[0] = k;
  167. k = k + 1;
  168. }
  169. }
  170. }
  171.  
  172.  
  173. writelong(y);
  174. break;
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. case'^': { //возведение в степень
  187. int n = 0;
  188. printf("Enter the first number ");
  189. readlong(a);
  190. printf("nEnter the stepen ");
  191. scanf_s("%d", &n);
  192.  
  193. cr = 0;
  194. for (i = 1; i <= a[0]; i++) {
  195. for (j = 1; j <= a[0]; j++) {
  196. cr = a[i] * a[j];
  197. k = i + j - 1;
  198. while (cr > 0) {
  199. cr = cr + y[k];
  200. y[k] = cr % 10;
  201. cr = cr / 10;
  202. if (k > y[0]) y[0] = k;
  203. k = k + 1;
  204.  
  205. for (q = 1; q < N - 1; q++) {
  206. m = y[q];
  207. y[q] = a[q];
  208. a[q] = m;
  209. }
  210.  
  211. }
  212. }
  213. }
  214.  
  215.  
  216.  
  217. writelong(a);
  218. break;
  219. }
  220.  
  221.  
  222.  
  223. default: printf("Incorrect operation"); break;
  224.  
  225.  
  226.  
  227. }
  228.  
  229.  
  230. _getch();
  231.  
  232. }
  233.  
  234. a^n = (a^{n/2})^2 = a^{n/2} * a^{n/2}
  235.  
  236. a^n = a^{n-1} * a
Add Comment
Please, Sign In to add comment