# c calculator

• 09-15-2008
c calculator
Hi, I'm new in c programming and I'm doing this problem where I basically have to program a calculator in c , the problem is it has to be able to handle large numbers in addition, subtraction, multiplication and division

I already know I can't use int or long variables so I have to store numbers in arrays and then I have to deal with digits in arrays (adding individual digits of an array) and I would have to deal with carries as well.

I wrote a code for a calculator but it doesn't handle large numbers so I didn't know should I actually post it here as it is unnecessary I think. (I can delete it if it's against the rules)

I know the code for the large number calculator has to be completely different but if you could just give me a couple of hints on arrays and how to add digits from two arrays, how to handle carries and stuff like that, it would be greatly appreciated.

And this is like only for addition so I'm afraid I still have a lot of work and questions to go....

Code:

```#include <stdio.h> #include <stdlib.h> #include <string.h> int main () {   char line[80];   while (fgets(line, 80, stdin)) {     int n;     char* t = strtok(line, " \n");     char operator = t[0];     t = strtok(0, " \n");     n = atoi(t);     t = strtok(0, " \n");     while (t) {       int z = atoi(t);       switch (operator) {       case '+': n += z; break;       case '*': n *= z; break;       case '-': n -= z; break;       case '/': n /= z; break;       }       t = strtok(0, " \n");     }     fprintf(stdout, "%d\n", n);   }   return 0; }```
• 09-16-2008
zacs7
Well assuming you can't use libraries like bignum, you *should* write some "datatype" along with various operations on those types. The array may resize, or it could have a fixed length.

You'll have to consider how complex you want to go, or how simple you want to make it. For example:

Code:

``` /* keeps, i.r  * where i = integer part, r = real number part */ typedef struct largenum_s {     char * real;        /* the real part number digits */     size_t realPrec;    /* number of real # digits */     char * integer;    /* the integer part */     size_t integerPrec; /* number of integer digits */ } largenum_t; /* ... */ largenum_t add(largenum_t * a, largenum_t * b) {     /* step 1, add the real # part together, if you get overflow add 1 to the 'integer part'     * and keep the left over bit */     /* add the 2 integer parts (including the part from step 1)     * resize the array if nessisary */     return answer;      /* return as a "largenum_t" */ }```