Velocity Reviews > Calculator

# Calculator

Flipke
Guest
Posts: n/a

 05-09-2005
Hello,

I wanna make a calculator where i can give the input in one word. for
example : (15*3) / (2+3)
I think i most read this in as a string and then share it in different parts
to do the calculation. But i don't now how tho start with it. Can somebody
help me.

David Resnick
Guest
Posts: n/a

 05-09-2005

Flipke wrote:
> Hello,
>
> I wanna make a calculator where i can give the input in one word. for
> example : (15*3) / (2+3)
> I think i most read this in as a string and then share it in

different parts
> to do the calculation. But i don't now how tho start with it. Can

somebody
> help me.
>

to get started on your homework.

You could also google for "recursive descent parser", that being a
standard approach for this sort of problem. K&R also has an
calculator, though
their example program uses RPN.

-David

Mike Wahler
Guest
Posts: n/a

 05-09-2005

"Flipke" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I wanna make a calculator where i can give the input in one word. for
> example : (15*3) / (2+3)
> I think i most read this in as a string and then share it in different
> parts
> to do the calculation.

Yes, 'divide and conquer' is a good practice in programming.

First, as you said, read the input. The next step
is to parse it into meaningful segments (e.g. (15 * 3),
15, etc.). You'll need to keep track of the parentheses
in order to implement the correct precedence of the
arithmetic operations. A recursive approach is the most
common approach to this kind of problem.

>Can somebody
> help me.

write it for you. Show the code of your best attempt, and

-Mike

Julian V. Noble
Guest
Posts: n/a

 05-09-2005
Flipke wrote:
>
> Hello,
>
> I wanna make a calculator where i can give the input in one word. for
> example : (15*3) / (2+3)
> I think i most read this in as a string and then share it in different parts
> to do the calculation. But i don't now how tho start with it. Can somebody
> help me.
>

Have a look at Kruse, "Data Structures and Program Design". He shows how
to develop a simple formula evaluator. IIRC he uses a parse tree rather
than recursive descent. His original version used Pascal, but that's close
enough to C to translate. Or you might look at a more recent edition--
I think he has ceased fighting the inevitable and now uses C or C++ as his
illustration language.

--
Julian V. Noble
Professor Emeritus of Physics
http://www.velocityreviews.com/forums/(E-Mail Removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"For there was never yet philosopher that could endure the
toothache patiently."

Neil Kurzman
Guest
Posts: n/a

 05-10-2005

Flipke wrote:

> Hello,
>
> I wanna make a calculator where i can give the input in one word. for
> example : (15*3) / (2+3)
> I think i most read this in as a string and then share it in different parts
> to do the calculation. But i don't now how tho start with it. Can somebody
> help me.
>

As I recall in data structures you can use stacks to convert it to RPN, then
push that on to 2 stacks to calculate it.
BTW the question is off topic.

osmium
Guest
Posts: n/a

 05-10-2005
"Julian V. Noble" writes:

> Flipke wrote:
>>
>> Hello,
>>
>> I wanna make a calculator where i can give the input in one word. for
>> example : (15*3) / (2+3)
>> I think i most read this in as a string and then share it in different
>> parts
>> to do the calculation. But i don't now how tho start with it. Can
>> somebody
>> help me.
>>

>
> Have a look at Kruse, "Data Structures and Program Design". He shows how
> to develop a simple formula evaluator. IIRC he uses a parse tree rather
> than recursive descent. His original version used Pascal, but that's close
> enough to C to translate. Or you might look at a more recent edition--
> I think he has ceased fighting the inevitable and now uses C or C++ as his
> illustration language.

Just out of curiosity I looked up the C/C++ version of the Kruse book on
Amazon to read the reviews. I have never seen such a collection of hateful
reviews of a book! I don't think it is possible to "salt" Amazon without a
great deal of effort so the book is likely despised by these people (31),
mostly students. As I understand it, he uses STL to explain data structures
which seems like a bad approach to me. A data structures class should be
*writing* STL , not *using* it. In any event I simply must see a copy of
this book. AFAIK there is no ACCU review.

Back to the OPs question. My guess is that most of the answers are too
high falutin' for what he is expected to do. If this is a first semester
class, surely the instructor doesn't expect him to get into recursive
descent parsers and all that stuff. I would expect a student in a later
course to phrase the question differently. And there is also the slim
possibility that it isn't a student problem at all.

italy
Guest
Posts: n/a

 05-10-2005

Flipke wrote:
> Hello,
>
> I wanna make a calculator where i can give the input in one word. for
> example : (15*3) / (2+3)

It's an expression, not a word. (note an expression is a collection of
operands and operators, or in some cases only operand(s))

> I think i most read this in as a string and then share it in

different parts
> to do the calculation. But i don't now how tho start with it. Can

somebody
> help me.

Break the problem down. I recommend writing a simple RPN calculator
first, because it's the easiest.
>

Walter Roberson
Guest
Posts: n/a

 05-10-2005
In article <(E-Mail Removed)>,
osmium <(E-Mail Removed)> wrote:
>Just out of curiosity I looked up the C/C++ version of the Kruse book on
>Amazon to read the reviews. I have never seen such a collection of hateful
>reviews of a book! I don't think it is possible to "salt" Amazon without a
>great deal of effort so the book is likely despised by these people (31),
>mostly students.

There was a scandal not long ago in which it turned out that
a bunch of Amazon "reviewers" were the authors themselves -- and
sometimes more than one "review" of their own books. "Salting" Amazon
is, then, perhaps a bit easier than you might expected.
[NB, I know nothing about the merits of the book in question.]
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers

Malcolm
Guest
Posts: n/a

 05-10-2005

"Flipke" <(E-Mail Removed)> wrote
>
> I wanna make a calculator where i can give the input in one word. for
> example : (15*3) / (2+3)
> I think i most read this in as a string and then share it in different
> parts
> to do the calculation. But i don't now how tho start with it. Can somebody
> help me.
>

To write a parser, firstly many of the rules of good programming don't apply
to parsers.
Normally the caller should ensure that the input is valid. For a parser, the
caller cannot do this without without writing another parser himself. So you
have to be very careful about handling errors.
Normally you should not have mutually recursive functions. For a parser,
this structure is natural.
Normally you should not have global variables. A parser is one place where
code is often neater if you do use globals.

How do you do it? The basic principle is that when you hit a bracket, you
call the top-level function on the contents. This is why parsers are
mutually recursive.

Arithmetical expressions have the bodmas (brackets, of, divide, multiply,

What you want to do is declare a global pointer to input so far. Then you
write a function called gettoken() which returns the token (number,
operator, parentheses) in the input stream, but does not remove it. Then
write a function called match() which discards the token from the stream,
but only if it is of the type passed. Otherwise it flags an error.

Then write three functions, expression(), term() and factor(). A factor is
either a raw number or an open parenthesis containing an expression
(recursion). A term is either a raw factor, or a series of factos connected
by divides and multiplies. An expression is either a raw term, or a series
of terms connected by additions and subtractions.

Once you have all this working, you basically have your calculator. It is
quite hard, so post your attempt if you get stuck.

Walter Roberson
Guest
Posts: n/a

 05-10-2005
In article <d5rc47\$mlu\$(E-Mail Removed)-infra.bt.com>,
Malcolm <(E-Mail Removed)> wrote:
>To write a parser, firstly many of the rules of good programming don't apply
>to parsers.

>Normally the caller should ensure that the input is valid.

[Good] programs are full of "validate" functions which don't know if
their input is valid until they look at it.

A database "find and lock" (to avoid race conditions) routine won't
know whether the input field is a match until it does the search.

It's normal defensive programming to assume that the input might not
be valid and to protect against such cases. Think of all the verbage
about how strcpy() is a fault in the design of C and it becomes clear
that it's often a bad idea to -assume- that all necessary checks
have been done before a routine is called.

>Normally you should not have mutually recursive functions.

Sez who?

The difficulty with mutually recursive functions is in the possibility
of indefinite stack space usage. The alternative to recursive functions
is to write the equivilent iterative function -- but the equivilent
iterative function will often need indefinite memory allocation
and so suffer the same fate [except that you can detect failed
memory allocation but you can't detect whether there's enough room
to call any particular function.]

>Normally you should not have global variables.

Ah? This is comp.lang.c . The operation of fopen() and kin use
global variables almost by definition, and there's no other I/O
operators available, so compliant hosted programs will almost always
use global variables.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers