Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.33 KB | None | 0 0
  1. ///\file proiect.c
  2. ///\brief C library implementation for operations with large numbers.
  3. ///
  4. /// Created by Cristescu Ovidiu
  5. /// Implements addition, substraction, multiplication, division and the square root
  6.  
  7.  
  8. #include<string.h>
  9. #include<stdio.h>
  10. #define MAX 10000
  11. ///\def #define MAX 10000
  12. ///\brief Define the maximum number possible as 10000.
  13.  
  14. typedef int bool;
  15.  
  16.  
  17. void addition(char s1[256], char s2[256])
  18. {
  19. ///\fn void addition(char s1[256], char s2[256])
  20. ///\brief In this function, we calculate the addition of two numbers
  21. ///\param s1 First number
  22. ///\param s2 Second number
  23. /// We calculate the addition by transforming the input numbers from char to integers
  24.  
  25. int num1[255], num2[255], sum[255];
  26. ///num1 is an array used to store the first number
  27. ///num2 is an array used to store the second number
  28. ///sum is an array used to calculate the addition of the two numbers num1 and num 2
  29.  
  30. int l1, l2;
  31. ///l1 is the lenght of the first number
  32. ///l2 is the lenghtof the second number
  33.  
  34.  
  35. for (l1 = 0; s1[l1] != '\0'; l1++)
  36. {
  37. ///Transformation from char to integer;
  38. num1[l1] = s1[l1] - '0';
  39. }
  40.  
  41.  
  42. for (l2 = 0; s2[l2] != '\0'; l2++)
  43. {
  44. ///Transformation from char to integer;
  45. num2[l2] = s2[l2] - '0';
  46. }
  47.  
  48. ///Carry is a variable used when the sum of num1[i] and num2[j] is bigger than 9
  49. int carry = 0;
  50. int k = 0;
  51. int i = l1 - 1;
  52. int j = l2 - 1;
  53.  
  54. for (; i >= 0 && j >= 0; i--, j--, k++)
  55. {
  56. ///Calculate the sum
  57. sum[k] = (num1[i] + num2[j] + carry) % 10;
  58. ///Calculate the carry (if we have one)
  59. carry = (num1[i] + num2[j] + carry) / 10;
  60. }
  61.  
  62.  
  63.  
  64. if (l1 > l2)
  65. {
  66. ///If the first number is longer than the other, we add the carry that remains (if it exists) to the next digit
  67. while (i >= 0) {
  68.  
  69. sum[k++] = (num1[i] + carry) % 10;
  70. carry = (num1[i--] + carry) / 10;
  71. }
  72.  
  73. }
  74. else
  75. ///Otherwise, we do the same thing, but with the second number
  76. if (l1 < l2)
  77. {
  78. while (j >= 0)
  79. {
  80. sum[k++] = (num2[j] + carry) % 10;
  81. carry = (num2[j--] + carry) / 10;
  82. }
  83. }
  84. ///If we finished the numbers, but we still have the carry, we add him to the sum
  85. else
  86. {
  87. if (carry > 0)
  88. sum[k++] = carry;
  89. }
  90.  
  91.  
  92. ///We print the sum from the end the of array
  93. for (k--; k >= 0; k--)
  94. printf("%d", sum[k]);
  95. }
  96.  
  97.  
  98. void reverse(char *from, char *to )
  99. {
  100. ///\fn void reverse(char *from, char *to )
  101. ///\brief This function is used to reverse a number
  102. ///\param from Pointer to the number we want to reverse
  103. ///\param to Pointer to the number we want to store the reversed number
  104.  
  105. int len = strlen(from);
  106. int l;
  107.  
  108. for(l=0;l<len;l++)
  109. {
  110. ///here, the process of reversing is made
  111. to[l]=from[len-l-1];
  112. }
  113.  
  114. to[len] = '\0';
  115. }
  116.  
  117.  
  118. int call_minus(char *large, char *small, char *result)
  119. {
  120. ///\fn int call_minus(char *large, char *small, char *result)
  121. ///\brief In this function, we calculate the substraction of two numbers
  122. ///\param large Pointer to the first number, who is supposed to be larger
  123. ///\param small Pointer to the second number which is supposed to be lower
  124. ///\param result Pointer to a char where we calculate the result of the substraction
  125.  
  126.  
  127. char L[MAX], S[MAX];
  128. int l, s, now, hold, diff;
  129.  
  130. /// the lenght of the first number
  131. l = strlen(large);
  132.  
  133. ///the lenght of the second number
  134. s = strlen(small);
  135.  
  136. ///sign is a boolean variable, we store in it the sign of our substraction
  137. bool sign = 0;
  138.  
  139.  
  140. if(l < s)
  141. {
  142. ///If the first number is smaller than the second, we interchange them, and our sign is 1, that means our result is negative
  143. ///Using the function strcpy we iterchange the numbers
  144. strcpy(result, large);
  145. strcpy(large, small);
  146. strcpy(small, result);
  147.  
  148. ///Using the variable now, we also interchange the lenghts of the numbers
  149. now = l;
  150. l = s;
  151. s = now;
  152. sign = 1;
  153. }
  154.  
  155. if(l == s)
  156. {
  157. ///If the lenghts of the numbers are equals
  158. if(strcmp(large, small) < 0)
  159. {
  160. ///if the first one is smaller:
  161. ///We interchange them, and our sign is 1, that means our result is negative
  162. strcpy(result, large);
  163. strcpy(large, small);
  164. strcpy(small, result);
  165.  
  166. ///Using the variable now, we also interchange the lenghts of the numbers
  167. now = l;
  168. l = s;
  169. s = now;
  170. sign = 1;
  171. }
  172. }
  173. ///We reverse the numbers to calculate the substraction.
  174. ///In L we store the bigger number.
  175. reverse(large,L);
  176. ///In S we store the smaller number.
  177. reverse(small,S);
  178.  
  179. for(; s<l; s++)
  180. {
  181. ///We complete the smaller number with 0, because it doesn't affect our calculations
  182. S[s] = '0';
  183. }
  184. ///the end of the string
  185. S[s]='\0';
  186.  
  187. for(now=0,hold=0;now<l;now++)
  188. {
  189. ///we calculate the difference between L[now] and S[now]
  190. ///hold is a variable used when L[now] (a digit of the first number) is smaller than S[now] (a digit from the second number
  191. diff = L[now] - (S[now]+hold);
  192.  
  193. if(diff < 0)
  194. {
  195. ///If our difference is smaller than 0, hold becomes 1 and at our result we add diff (the differrece) at the result
  196. hold = 1;
  197. ///We add ten because we borrow it from the next digit, this is why our hold becomes 1 */
  198. result[now] = 10 + diff + '0';
  199. }
  200.  
  201. else
  202. {
  203. ///Otherwise , we simply add our difference to the result
  204. result[now] = diff + '0';
  205. ///Hold becomes 0 because we didn't have to borrow, because the digit L[now] was greater than the digit S[now]
  206. hold = 0;
  207. }
  208. }
  209.  
  210. for(now=l-1; now>0; now--){
  211. if(result[now] != '0')
  212. break;
  213. }
  214. ///end of string
  215. result[now + 1] = '\0';
  216. ///We reverse the result, because when we store it, we start with the end of the result
  217. reverse(result, L);
  218. ///We copy the new result in the char result
  219. strcpy(result, L);
  220. /// we return the sign because we have to know if the output will be positive of negative
  221. return sign;
  222. }
  223.  
  224. void subtraction(char fir[256], char sec[256])
  225. {
  226. ///\fn void subtraction(char fir[256], char sec[256])
  227. ///\brief In this function we call the function call_minus and we print the result of the substraction
  228. ///\param *fir First number
  229. ///\param sec Second number
  230. int i;
  231. ///In res we store the result, calculated in the function call_minus
  232. char res[MAX];
  233.  
  234.  
  235. if(call_minus(fir, sec, res) == 1)
  236. {
  237. ///If the function call_minus return 1, it means our result is negative and we have to print "-" ar the beginning of the result
  238. printf("\n-");
  239. }
  240.  
  241. int len = strlen(res);
  242.  
  243.  
  244. for(i=0; i<len; i++)
  245. {
  246. ///Printing the result of the substraction
  247. printf("%c",res[i]);
  248. }
  249.  
  250. printf("\n");
  251. }
  252.  
  253.  
  254. char * multiply(char a[256],char b_1[256])
  255. {
  256. ///\fn char * multiply(char a[256],char b_1[256])
  257. ///\brief This function is used to calculate the multiplication of two numbers
  258. ///\param a First number
  259. ///\param b Second number
  260. ///The operation is made transforming the numbers from char to integers
  261.  
  262. static char mul[MAX];
  263. char c[MAX];
  264. char temp[MAX];
  265. int la, lb;
  266. int i, j, k=0, x=0, y;
  267. long int r=0;
  268. long sum = 0;
  269. char d[MAX];
  270.  
  271. /// We copy our number in the string d because in this function there are made some changes, and the number is modified on the way
  272. strcpy(d,a);
  273.  
  274. la = strlen(d) - 1;
  275. lb = strlen(b_1) - 1;
  276.  
  277. for(i=0; i<=la; i++)
  278. {
  279. /// We transform the first number from char to integer, every character becomes a digit of the number
  280. d[i] = d[i] - '0';
  281. }
  282.  
  283. for(i=0; i<=lb; i++)
  284. {
  285. /// We transform the second number from char to integer, every character becomes a digit of the number
  286. b_1[i] = b_1[i] - '0';
  287. }
  288.  
  289. for(i=lb; i>=0; i--)
  290. {
  291. r = 0;
  292. for(j=la; j>=0; j--)
  293. {
  294. ///We multiply the numbers, digit by digit, and we store them in parameter temp
  295. temp[k++] = (b_1[i]*d[j] + r)%10;
  296. ///r is a parameter used to store the div by ten of the multiplication of the numbers, digit by digit.
  297. r = (b_1[i]*d[j]+r)/10;
  298. }
  299.  
  300. ///If at the end of the digits, we still have the r, we add him in our temp
  301. temp[k++] = r;
  302. x++;
  303.  
  304. for(y = 0; y<x; y++)
  305. {
  306. temp[k++] = 0;
  307. }
  308. }
  309.  
  310. k = 0;
  311. r = 0;
  312.  
  313. for(i=0; i<la+lb+2; i++)
  314. {
  315.  
  316. ///After we store the multiplication of the digits, we sum them, to obtain the final result
  317. sum = 0;
  318. y = 0;
  319.  
  320. for(j=1; j<=lb+1; j++)
  321. {
  322. if(i <= la+j)
  323. {
  324. sum = sum + temp[y+i];
  325. }
  326.  
  327. y += j + la + 1;
  328. }
  329.  
  330. c[k++] = (sum + r) % 10;
  331. r = (sum + r)/10;
  332. }
  333.  
  334. c[k] = r;
  335. j=0;
  336.  
  337. for(i=k-2; i>=0; i--)
  338. {
  339. ///We transform from integer to char
  340. mul[j++]=c[i] + '0';
  341. }
  342.  
  343. mul[j]='\0';
  344. return mul;
  345. }
  346.  
  347. void multiplication(char a[256], char b_1[256])
  348. {
  349. ///\fn void multiplication(char a[256], char b_1[256])
  350. ///\brief In this function we call the function multiply and we print the result using parameter c
  351. ///\param a First number
  352. ///\param b_1 Second number
  353.  
  354. char *c;
  355.  
  356. printf("Multiplication of two numbers : ");
  357. c = multiply(a,b_1);
  358. printf("%s\n",c);
  359.  
  360.  
  361. }
  362.  
  363. char * division(char a[256],unsigned long b)
  364. {
  365. ///\fn char * division(char a[256],unsigned long b)
  366. ///\brief The function is used to calculate the division of two numbers
  367. ///\param a First number
  368. ///\param b Second number
  369. static char c[MAX];
  370. int la;
  371. int i, k=0, flag = 0;
  372. unsigned long temp = 1, reminder;
  373. char number[256];
  374.  
  375. ///We copy the number a in the parameter number, because during this function, the number is modified and it may cause errors at the other functions
  376. strcpy( number,a);
  377.  
  378. la=strlen( number);
  379.  
  380. for(i=0;i<=la;i++)
  381. {
  382. ///We transform from char to integer
  383. number[i] = number[i] - '0';
  384. }
  385.  
  386. temp = number[0];
  387. reminder = number[0];
  388.  
  389. for(i=1; i<=la; i++)
  390. {
  391. ///Here, we calculate the division
  392. if(b <= temp)
  393. {
  394. ///If the divisor (b ) is smaller or equal than the first digit, we devide it and we store the rest in the parameter reminder
  395. ///Parameter c is used to store the result
  396. c[k++] = temp/b;
  397. temp = temp % b;
  398. reminder = temp;
  399. temp = temp * 10 + number[i];
  400. flag = 1;
  401.  
  402. }
  403. else
  404. {
  405. ///Otherwise, we store the temp in the reminder
  406. reminder = temp;
  407. temp = temp * 10 + number[i];
  408.  
  409. if(flag == 1 )
  410. c[k++] = 0;
  411. }
  412. }
  413.  
  414. for(i=0;i<k;i++)
  415. {
  416. ///We trasform from integer to char
  417. c[i]=c[i] + 48;
  418. }
  419.  
  420. c[i]= '\0';
  421.  
  422. printf("Reminder of division: %lu ", reminder);
  423. return c;
  424. }
  425.  
  426. void division_function(char a[256])
  427. {
  428. ///\fn void division_function(char a[256])
  429. ///\brief This function is used to call the function division and print the result, using parameter c
  430. ///\param a Our dividend
  431. char *c;
  432. unsigned long b;
  433.  
  434. printf("\nInsert the divisor: ");
  435. scanf("%lu", &b);
  436. c = division(a,b);
  437. printf("\nResult of the division : ");
  438. printf("%s\n",c);
  439.  
  440. }
  441.  
  442.  
  443. void square_root(char numar[256])
  444. {
  445. ///\fn void square_root(char numar[256])
  446. ///\brief This function is used to calculate the square root of our number, grouping the digits of the number in twos
  447. ///\param a The number
  448. ///The result is printed digit by digit, every time we execute the operation of the pairs of two digits
  449.  
  450.  
  451. unsigned long reminder, lungime, i, k, modulo, nr, rez = 0, lung;
  452. int j;
  453.  
  454. lungime = strlen(numar);
  455.  
  456. for(i=0; i<lungime; i++)
  457. {
  458. ///We transform from char to integer
  459. numar[i] = numar[i] - '0';
  460. }
  461.  
  462. ///We need to know if the lenght of our number is even or odd
  463. modulo = lungime % 2;
  464.  
  465. if(modulo == 0)
  466. {
  467. ///If the lenght is even we calculate the square root, grouping the digits in twos
  468. lung = 0;
  469. reminder = 0;
  470. ///First, we calculate for the first two digits and we need to find the biggest digit that has his square smaller or equal with them
  471. nr = (reminder * 100) + (numar[0] * 10) + numar[1];
  472. ///Parameter j is used to find the digit, and we start with the biggest one and we decrease it untill we find it
  473. j=9;
  474. while(!(((j*j) <= nr)))
  475. {
  476.  
  477. j--;
  478. }
  479. ///After we found it, we use the reminder to store the rest between our number consisted of the two first digits and the square of the digit found
  480. reminder = nr - (j*j);
  481. ///We calculate the result in parameter rez
  482. rez = rez * 10 + j;
  483. ///To continue the calculations for the rest of the digits, we need to store the double of the result in the parameter k
  484. k = 2 * rez;
  485.  
  486. printf("%d", j);
  487.  
  488. for (i=2; i<lungime; i=i+2)
  489. {
  490. ///For the rest of the digits, we do the same as above
  491. nr = (reminder * 100) + (numar[i] * 10) + numar[i+1];
  492. j=9;
  493. ///This time, we have to find the biggest digit that added to the end of k (our result doubled) and we multiply the obtained number by that digit and the result must be smaller or equal than our number consists of the two digits
  494. while(!(((((k*10) + j) * j)) <= nr))
  495. {
  496.  
  497. j--;
  498. }
  499.  
  500. reminder = nr - (((k*10) + j) * j);
  501. rez =rez * 10 + j;
  502. k = 2 * rez;
  503.  
  504. printf("%d", j);
  505. }
  506. }
  507. else
  508. {
  509. ///Otherwise, if the lenght is odd, we do the same as above, but we start with the first digit, and then continue with the others, making pairs of two
  510. lung = 0;
  511. reminder = 0;
  512. nr = (reminder * 100) + numar[0];
  513. j=9;
  514. while(!(((j*j) <= nr)))
  515. {
  516. j--;
  517. }
  518. reminder = nr - (j*j);
  519.  
  520. rez =rez * 10 + j;
  521. k = 2 * rez;
  522. printf("%d", j);
  523.  
  524. for (i=1; i<lungime; i=i+2)
  525. {
  526. nr = (reminder * 100) + (numar[i] * 10) + numar[i+1];
  527. j=9;
  528. while(!(((((k*10) + j) * j)) <= nr))
  529. {
  530.  
  531. j--;
  532. }
  533. reminder = nr - (((k*10) + j) * j);
  534. rez = rez * 10 + j;
  535. k = 2 * rez;
  536. printf("%d", j);
  537.  
  538. }
  539. }
  540.  
  541. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement