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. Advertisements

  2. 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
     
    Martin Buchleitner, Mar 7, 2004
    #2
    1. Advertisements

  3. Paul

    osmium Guest

    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

    started. This is not GUI but a simple terminal program.
    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.