Advertisement
Guest User

0000 Programming

a guest
Jan 16th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. const int N = 12;     // essential of number
  5. int main() {
  6.     char a[N], b[N];
  7.     int result[N];
  8.  
  9.     //------------------- initial  value -------------------
  10.     for (int i = 0; i < N; ++i) {
  11.         a[i] = b[i] = '0';
  12.         result[i] = -1;
  13.     }
  14.  
  15.     //------------------------ input -----------------------
  16.     scanf("%s %s", a, b);
  17.  
  18.     // --------- find length of string 'a' and 'b' ---------
  19.     int len_a = strlen(a);
  20.     int len_b = strlen(b);
  21.  
  22.     int i;
  23.     //-------------- swap string 'a' and 'b' ---------------
  24.     for (i = 0; i < len_a/2; ++i) {
  25.         char c = a[i];
  26.         a[i] = a[len_a - i - 1];
  27.         a[len_a - i - 1] = c;
  28.     }
  29.     for (i = 0; i < len_b/2; ++i) {
  30.         char c = b[i];
  31.         b[i] = b[len_b - i - 1];
  32.         b[len_b - i - 1] = c;
  33.     }
  34.  
  35.     //------------ calculate plussing - processing ----------
  36.     i = 0;
  37.     while (i < len_a && i < len_b) {
  38.         result[i] = a[i] - '0' + b[i] - '0';
  39.         ++i;
  40.     }
  41.     while (i < len_a) {
  42.         result[i] = a[i] - '0';
  43.         ++i;
  44.     }
  45.     while (i < len_b) {
  46.         result[i] = b[i] - '0';
  47.         ++i;
  48.     }
  49.     int tod = i = 0;
  50.     int ans[N];
  51.     int ans_i = 0;
  52.     while (result[i] != -1) { // using array to be stack
  53.         ans[ans_i++] = (result[i]+tod) % 10;
  54.         tod = (result[i]+tod) / 10;
  55.         ++i;
  56.     }
  57.     if(tod > 0)ans[ans_i++] = tod;
  58.  
  59.     //--------------------- output --------------------------
  60.     while (--ans_i >= 0) {
  61.         printf("%d",  ans[ans_i]);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement