Thread: c calculator

  1. #1
    Registered User
    Join Date
    Sep 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....

    #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;

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    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:

    /* 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" */
    And your calculator wouldn't really change in structure, just you'd call add() instead of using "+".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 12:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM