Nguythang

addBignumber

Jul 13th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. void Reverse(char s[]) {
  5.     char temp;
  6.     int i, n = strlen(s);
  7.     for (i = 0; i < (n / 2); i++) {
  8.         temp = s[n - i - 1];
  9.         s[n - i - 1] = s[i];
  10.         s[i] = temp;
  11.     }
  12. }
  13.  
  14. int checkInput(char s[]) {
  15.     int i;
  16.     if ((s[0] > '9' || s[0] < '0') && s[0] != '-') return 0;
  17.     for (i = 1; i < strlen(s); i++) {
  18.         if (s[i] > '9' || s[i] < '0') return 0;
  19.         if (s[i] == ' ') return 0;
  20.     }
  21.     return 1;
  22. }
  23.  
  24. void enter(char s[]) {
  25.     int check = 1;
  26.     do {
  27.         gets(s);
  28.         check = checkInput(s);
  29.         if (!check) printf("Please enter again :\n");
  30.     } while (!check);
  31. }
  32.  
  33. int compareNumber(char num1[], char num2[]) {
  34.     if (strlen(num1) > strlen(num2)) return 1;
  35.     if (strlen(num2) > strlen(num1)) return 2;
  36.     int x = strcmp(num1, num2);
  37.     if (x > 0) return 1;
  38.     else if (x < 0) return 2;
  39.     return 3;
  40. }
  41.  
  42. void add0(char num1[], char num2[]) {
  43.     Reverse(num1);
  44.     Reverse(num2);
  45.     while (strlen(num1) < strlen(num2)) strcat(num1, "0");
  46.     while (strlen(num1) > strlen(num2)) strcat(num2, "0");
  47. }
  48.  
  49. void delete0(char s[]) {
  50.     char st[1000];
  51.     int i, j = 0, d;
  52.     for (i = 0; i < strlen(s); i++)
  53.         if (s[i] != '0') {
  54.             d = i;
  55.             break;
  56.         }
  57.     for (i = d; i < strlen(s); i++)
  58.         st[j++] = s[i];
  59.     st[j] = '\0';
  60.     strcpy(s, st);
  61.     if (j == 0) strcpy(s, "0");
  62. }
  63.  
  64. void addNumber(char num1[], char num2[], char sum[1001]) {
  65.     int i, carry = 0;
  66.     add0(num1, num2);
  67.         for (i = 0; i < strlen(num1); i++) {
  68.         sum[i] = num1[i] + num2[i] - 96 + carry;
  69.         carry = sum[i] / 10;
  70.         sum[i] = sum[i] % 10 + 48;
  71.     }
  72.     sum[strlen(num1)] = '\0';
  73.     Reverse(sum);
  74. }
  75.  
  76. void SubNumber(char s1[], char s2[], char s[], int minus) {
  77.     add0(s1, s2);
  78.     int i, h, carry = 0;
  79.     for (i = 0; i < strlen(s1); i++) {
  80.         h = s1[i] - s2[i] - carry;
  81.         if (h < 0) {
  82.             carry = 1;
  83.             s[i] = (h + 10) % 10 + 48;
  84.         } else {
  85.             carry = 0;
  86.             s[i] = h + 48;
  87.         }
  88.     }
  89.     s[strlen(s1)] = '\0';
  90.     Reverse(s);
  91.     delete0(s);
  92.     Reverse(s);
  93.     if (minus  == -1) strcat(s, "-");
  94.     Reverse(s);
  95. }
  96.  
  97. void switchToPositive(char s[]) {
  98.     int i;
  99.     for (i = 0; i < strlen(s) - 1; i++)
  100.         s[i] = s[i + 1];
  101.     s[strlen(s) - 1] = '\0';
  102. }
  103.  
  104. void printResult(char s1[], char s2[], char resultAdd[]) {
  105.     printf("%s + %s = %s\n", s1, s2, resultAdd);
  106. }
  107.  
  108. void addTwoPos(char s1[], char s2[], char resultAdd[]) {
  109.     char st1[1000], st2[1000];
  110.     strcpy(st1, s1);
  111.     strcpy(st2, s2);
  112.     addNumber(st1, st2, resultAdd);
  113. }
  114.  
  115. void addNagPos(char s1[], char s2[], char resultAdd[]) {
  116.     char st1[1000], st2[1000];
  117.     strcpy(st1, s1);
  118.     strcpy(st2, s2);
  119.     switchToPositive(st1);
  120.     int x = compareNumber(st1, st2);
  121.     if (x == 1) {
  122.         SubNumber(st1, st2, resultAdd, -1);
  123.     } else if (x == 2) {
  124.         SubNumber(st2, st1, resultAdd, 1);
  125.     }  else {
  126.         strcpy(resultAdd, "0");
  127.     }
  128. }
  129.  
  130. void addTowNag(char s1[], char s2[], char resultAdd[]) {
  131.     char st1[1000], st2[1000];
  132.     strcpy(st1, s1);
  133.     strcpy(st2, s2);
  134.     switchToPositive(st1);
  135.     switchToPositive(st2);
  136.     char temp[1000];
  137.     addNumber(st1, st2, temp);
  138.     strcpy(resultAdd, "-");
  139.     strcat(resultAdd, temp);
  140. }
  141.  
  142. int main() {
  143.     char s1[1000], s2[1000], resultAdd[1000];
  144.     while (1) {
  145.         printf("Enter first number  :  ");
  146.         enter(s1);
  147.         printf("Enter second number :  ");
  148.         enter(s2);
  149.         if (s1[0] != '-' && s2[0] != '-') {
  150.             addTwoPos(s1, s2, resultAdd);
  151.         } else if (s1[0] == '-' && s2[0] != '-') {
  152.             addNagPos(s1, s2, resultAdd);
  153.         } else if (s1[0] != '-' && s2[0] == '-') {
  154.             addNagPos(s2, s1, resultAdd);
  155.         } else {
  156.             addTowNag(s1, s2, resultAdd);
  157.         }
  158.         printResult(s1, s2, resultAdd);
  159.     }
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment