coding a simple calculator

Discussion in 'C Programming' started by Paul, Mar 7, 2004.

  1. Paul

    Paul Guest

    I want to make a simple calculator program but dont know where to get
    started. This is not GUI but a simple terminal program.

    It would get input like this

    Enter number:

    5
    +
    10
    =
    the result is 15.

    Iw would keep adding or subtracting... until the user enters '='

    I want to loop this so that if the person would enter 'q' it would
    quit.

    Iam thinking maybe of a while loop in the beginning like this

    while (scanf("%d", &num) == 1)

    but after this I dont know where to go.

    Dont post the whole code just need a little help getting started.

    BTW: Iam still learning C from a book called C Primer Plus and I just
    finished a chapter about if else switch() break. So I dont know much
    but I think its enough for this simple project.
     
    Paul, Mar 7, 2004
    #1
    1. Advertising

  2. * (Paul) wrote:

    > I want to make a simple calculator program but dont know where to get
    > started. This is not GUI but a simple terminal program.
    >
    > It would get input like this
    >
    > Enter number:
    >
    > 5
    > +
    > 10
    > =
    > the result is 15.
    >
    > Iw would keep adding or subtracting... until the user enters '='
    >
    > I want to loop this so that if the person would enter 'q' it would
    > quit.
    >
    > Iam thinking maybe of a while loop in the beginning like this
    >
    > while (scanf("%d", &num) == 1)
    >
    > but after this I dont know where to go.


    Read a line - check if it is a number or a known operater and put it
    on a stack or refuse the entry.

    If the user enters '=', then go through the stack and pop the elements
    from the stack. May be this is not the correct way to do it and you
    have to do some operations on the stack already when a operator is
    entered.


    hth, mabu

    --
    Are you Anonymous? Where? ... I don't think so ...

    [ devnull{at}chaosfactory{dot}org | http://www.chaosfactory.org/ ]
     
    Martin Buchleitner, Mar 7, 2004
    #2
    1. Advertising

  3. Paul

    osmium Guest

    Paul writes:

    > I want to make a simple calculator program but dont know where to get
    > started. This is not GUI but a simple terminal program.
    >
    > It would get input like this
    >
    > Enter number:
    >
    > 5
    > +
    > 10
    > =
    > the result is 15.
    >
    > Iw would keep adding or subtracting... until the user enters '='
    >
    > I want to loop this so that if the person would enter 'q' it would
    > quit.
    >
    > Iam thinking maybe of a while loop in the beginning like this
    >
    > while (scanf("%d", &num) == 1)
    >
    > but after this I dont know where to go.


    What you describe is an adding machine, not a calculator. It recognizes
    integer numbers, +, - and =. You might assume an error free input to get
    started. That is not a good assumption for a usable thing but I don't think
    that is your goal, anyway. I think you could have an inner loop that has
    (partial pseudocode):

    do
    get number
    get operator
    if (+ or -)
    add to accumulator
    else
    display result note: assumed '='
    set done flag to true
    while not done

    This does one series of adds and then quits, so it isn't quite what you
    asked for. You could enclose this in a menu to get the 'q' thingy.
    Providing for user errors will require that you parse the input yourself,
    IOW the user inputs a string of characters and *you* do the conversion to
    numbers etc.

    If you decide to later do an actual calculator, I suggest you look into
    reverse Polish notation (RPN). An RPN calculator is easier to code than the
    usual (algebraic) calculator. K&R has a lengthy (for them) calculator
    example.
     
    osmium, Mar 7, 2004
    #3
  4. Paul

    Malcolm Guest

    "Paul" <> wrote in message
    > I want to make a simple calculator program but dont know where > to get

    started. This is not GUI but a simple terminal program.
    >
    > I am thinking maybe of a while loop in the beginning like this
    >
    > while (scanf("%d", &num) == 1)
    >
    > but after this I dont know where to go.
    >

    The first thing is that you want to do all your calulations in double. This
    is no harder than using ints.

    You just keep a running total, as a handheld calculator does.

    Your user has got to enter numbers and operations alternately.

    int main(void)
    {
    double x = 0; /* this is your accumulator */
    char op = '+'; /* hold the operation, '+', '*' etc */
    /* set as a dummy + to start */
    char buff[1024]; /* your input buffer */

    /* print out a message saying how to use the program */
    /* run this do while loop until you get a q */
    do
    {
    fgets(buff, 1024, stdin);

    /* if op is 0 we are expecting an operation */
    if(op == 0)
    {
    /* read a character from the input, which should be +-/*= or q*/
    /* set op, or print x if you have = */
    }
    /* if op is non-zero we are expecting a long */
    else
    {
    double y;

    /* read in y. Take action if not a number */
    if(sscanf(buff, "%lf", &y) != 1)
    {
    /* check for 'q' here */
    printf("Must have a number\n");
    /* continue */
    }
    /* do the arithmetic */
    switch(op)
    {
    case '+':
    case '-':
    case '/':
    case '*':
    default:
    /* default should never happen */
    }
    op = 0;
    }
    }
    while( !strchr(buff, 'q'))

    return 0;
    }
     
    Malcolm, Mar 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makko

    calculator

    makko, Feb 28, 2005, in forum: Perl
    Replies:
    2
    Views:
    2,999
    makko
    Mar 1, 2005
  2. Ron
    Replies:
    14
    Views:
    1,010
  3. calmar
    Replies:
    11
    Views:
    960
    calmar
    Feb 21, 2006
  4. Replies:
    11
    Views:
    334
    Ted Zlatanov
    Jun 8, 2006
  5. SIMPLE Javascript Calculator

    , Jul 10, 2006, in forum: Javascript
    Replies:
    23
    Views:
    340
    Randy Webb
    Jul 13, 2006
Loading...

Share This Page