Advertisement
huyhung94

Tổng, Hiệu, Tích của 2 số a,b

Nov 30th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAXLEN 2002
  5. typedef char string[MAXLEN];
  6. void reverse(string a)
  7. {
  8.     int i, n = strlen(a);
  9.     char c;
  10.     for(i=0; i<n/2; i++)
  11.     {
  12.         c = a[n-i-1];
  13.         a[n-i-1] = a[i];
  14.         a[i] = c;
  15.     }
  16. }
  17. void add_zero(string a, string b)
  18. {
  19.     while(strlen(a)<strlen(b))
  20.         strcat(a, "0");
  21.     while(strlen(b)<strlen(a))
  22.         strcat(b, "0");
  23. }
  24. void remove_zero(string a, string b)
  25. {
  26.     int n = strlen(a)-1;
  27.     while(n>1 && a[n]=='0')
  28.         n--;
  29.     strncpy(a, a, n);
  30.     a[n+1] = '\0';
  31.     n = strlen(b)-1;
  32.     while(n>1 && b[n]=='0')
  33.         n--;
  34.     strncpy(b, b, n);
  35.     b[n+1] = '\0';
  36. }
  37. int greater(string a, string b)
  38. {
  39.     remove_zero(a, b);
  40.     int na = strlen(a), nb = strlen(b), i;
  41.     if(na>nb)
  42.         return 1;
  43.     else if(na<nb)
  44.         return 0;
  45.     for(i = na-1; i>=0; i--)
  46.         if(a[i]>b[i])
  47.             return 1;
  48.         else if(a[i]<b[i])
  49.             return 0;
  50.     return 1;
  51. }
  52. void add(string a, string b, string c)
  53. {
  54.     strcpy(c, "");
  55.     int i, n, s, r = 0;
  56.     char z;
  57.     add_zero(a, b);
  58.     n = strlen(a);
  59.     for(i=0; i<n; i++)
  60.     {
  61.         s = (a[i]-'0') + (b[i]-'0') + r;
  62.         c[i] = s%10 + '0';
  63.         r = s/10;
  64.     }
  65.     if(r>0)
  66.     {
  67.         c[n] = r + '0';
  68.         n++;
  69.     }
  70.     while(c[n-1]=='0' && n>1)
  71.         n--;
  72.     c[n] = '\0';
  73.     reverse(c);
  74. }
  75. void sub(string a, string b, string c)
  76. {
  77.     strcpy(c, "");
  78.     int i, n, s, r = 0;
  79.     char z;
  80.     add_zero(a, b);
  81.     n = strlen(a);
  82.     for(i=0; i<n; i++)
  83.     {
  84.         s = a[i] - b[i] - r;
  85.         if(s<0)
  86.         {
  87.             s += 10;
  88.             r = 1;
  89.         }
  90.         else
  91.             r = 0;
  92.         c[i] = s%10 + '0';
  93.     }
  94.     while(c[n-1]=='0' && n>1)
  95.         n--;
  96.     c[n] = '\0';
  97.     reverse(c);
  98. }
  99. void multiply(string a, string b, string c)
  100. {
  101.     strcpy(c, "");
  102.     int i, j, len, s, r = 0, n = strlen(a)-1, m;
  103.     remove_zero(a, b);
  104.     n = strlen(a);
  105.     m = strlen(b);
  106.     len = n+m-1;
  107.     for(i=0; i<len; i++)
  108.     {
  109.         s = 0;
  110.         for(j=0; j<=i; j++)
  111.             if(j<n && (i-j)<m)
  112.                 s += (a[j]-'0')*(b[i-j]-'0');
  113.         s += r;
  114.         r = s/10;
  115.         c[i] = s%10+'0';
  116.     }
  117.     if(r>0)
  118.     {
  119.         c[len] = r+'0';
  120.         len++;
  121.     }
  122.     while(c[len-1]=='0' && len>1)
  123.         len--;
  124.     c[len] = '\0';
  125.     reverse(c);
  126. }
  127. int main()
  128. {
  129.     string a, b, c;
  130.     gets(a);
  131.     gets(b);
  132.     reverse(a);
  133.     reverse(b);
  134.     add(a, b, c);
  135.     puts(c);
  136.     if(greater(a, b))
  137.         sub(a, b, c);
  138.     else
  139.     {
  140.         sub(b, a, c);
  141.         printf("-");
  142.     }
  143.     puts(c);
  144.     multiply(a, b, c);
  145.     puts(c);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement