Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. char *infAdd(int det, int cur, int stock, char **av)
  6. {
  7. char *result;
  8. int sign[2];
  9. int curs[2];
  10. int terms[2];
  11.  
  12. sign[0] = (av[1][0] == '-') ? -1 : 1;
  13. sign[1] = (av[2][0] == '-') ? -1 : 1;
  14. curs[0] = strlen(av[1]);
  15. curs[1] = strlen(av[2]);
  16. result = malloc((curs[0] > curs[1] ? curs[0] : curs[1]) + 2);
  17. result[(curs[0] > curs[1] ? curs[0] : curs[1]) + 1] = '\0';
  18. memset(result, '0', (curs[0] > curs[1] ? curs[0] : curs[1]) + 1);
  19. det = 0;
  20. while (((cur = 0) == 1) || curs[0] >= 0 || curs[1] >= 0 || det > 0)
  21. {
  22. terms[0] = --curs[0] < 0 || av[1][curs[0]] == '-' ?
  23. 0 : sign[0] * (av[1][curs[0]] - 48);
  24. terms[1] = --curs[1] < 0 || av[2][curs[1]] == '-' ?
  25. 0 : sign[1] * (av[2][curs[1]] - 48);
  26. stock = terms[0] + terms[1] + det; /* Manage negative here */
  27. cur = (stock < 0 ? -stock : stock) % 10;
  28. det = (cur < 0) ? -1 : (stock / 10);
  29. result[(curs[0] > curs[1] ? curs[0] : curs[1]) + 1] = cur + 48;
  30. }
  31. return (result);
  32. }
  33.  
  34. int main(int ac, char **av)
  35. {
  36. char *result;
  37.  
  38. if (ac != 3)
  39. return (fprintf(stderr, "Use: %s [terms_one] [terms_two]\n", av[0]));
  40. result = infAdd(0, 0, 0, av);
  41. printf("%s\n", result);
  42. free(result);
  43. return (0);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement