Velocity Reviews > coding a simple calculator

coding a simple calculator

Paul
Guest
Posts: n/a

 03-07-2004
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.

Martin Buchleitner
Guest
Posts: n/a

 03-07-2004
* http://www.velocityreviews.com/forums/(E-Mail Removed) (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/ ]

osmium
Guest
Posts: n/a

 03-07-2004
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 -)
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.

Malcolm
Guest
Posts: n/a

 03-07-2004

"Paul" <(E-Mail Removed)> 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;
}