/*------------------------------------------------------------------------------ * problem017.c: Primary Arithmetic (5.9.1) **----------------------------------------------------------------------------*/ /* Copyright (c) 2009 Peter Bui. All Rights Reserved. * * Peter Bui * **------------------------------------------------------------------------------ * Includes **----------------------------------------------------------------------------*/ #include #include #include /*------------------------------------------------------------------------------ * Constants **----------------------------------------------------------------------------*/ #define NMAX_DIGITS 11 #define c2i(c) (int)((c) - '0') /*------------------------------------------------------------------------------ * Functions **----------------------------------------------------------------------------*/ int count_carries(char a[], char b[]) { char *ap, *bp; int ia, ib, s, c; ap = &a[strlen(a) - 1]; bp = &b[strlen(b) - 1]; s = 0; c = 0; while (ap >= a || bp >= b) { ia = (ap >= a) ? c2i(*ap--) : 0; ib = (bp >= b) ? c2i(*bp--) : 0; s += ia + ib; if (s > 9) { s -= 9; c++; } else { s = 0; } } return (c); } /*------------------------------------------------------------------------------ * Main Execution **----------------------------------------------------------------------------*/ int main(int argc, char* argv[]) { char a[NMAX_DIGITS]; char b[NMAX_DIGITS]; int c; while (scanf("%s %s", a, b) != EOF) { if (strcmp(a, "0") == 0 && strcmp(b, "0") == 0) break; c = count_carries(a, b); switch (c) { case 0: printf("No carry operation.\n"); break; case 1: printf("1 carry operation.\n"); break; default: printf("%d carry operations.\n", c); break; } } return (EXIT_SUCCESS); } /*------------------------------------------------------------------------------ * vim: sts=4 sw=4 ts=8 ft=cpp **----------------------------------------------------------------------------*/