Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > interesting problem

Reply
Thread Tools

interesting problem

 
 
makko
Guest
Posts: n/a
 
      01-21-2005
Hello,
anyone know how to writre a program that take a commandline formula
and prints the calculated result? example;
$program 1+(2x3(3/2))-8

reagrds;
Makkko

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      01-21-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

makko wrote:
> Hello,
> anyone know how to writre a program that take a commandline formula
> and prints the calculated result? example;
> $program 1+(2x3(3/2))-8


Yes, I know how.

For a clue as to how you can know how too, google for "recursive descent
parser"

- --

Lew Pitcher, IT Consultant, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB8QqvagVFX4UWr64RAnfhAKDSEGoKDT0pBPrGbmK/+pfq91RmwACfSDHl
bBxjlAt+dxVAdzVE3EgHIww=
=jM5G
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
David REsnick
Guest
Posts: n/a
 
      01-21-2005
makko wrote:

> Hello,
> anyone know how to writre a program that take a commandline formula
> and prints the calculated result? example;
> $program 1+(2x3(3/2))-8
>
> reagrds;
> Makkko
>

#!/usr/bin/perl -w
print eval("@ARGV") . "\n";

That'll do it

Your question feels lame to me. The answer is that yes,
most regulars here know how to write such a program. Do
you? Have you tried? If you present your efforts here,
working or not, people are usually quite helpful. Or even
if you ask nicely for hints on how to get started. But
asking folks to write your homework for you -- whether for school
or you are trying to learn to program on your own --
is feeble.

If you just want the answers and to learn nothing, google
will give you source code for this...

-David
 
Reply With Quote
 
Michael Coyne
Guest
Posts: n/a
 
      01-21-2005
On Fri, 21 Jan 2005 05:22:55 -0800, makko said to the parser:

> anyone know how to writre a program that take a commandline formula and
> prints the calculated result? example; $program 1+(2x3(3/2))-8


Yes.

Parsing mathematical expressions is pretty standard 1st or 2nd year
comp. sci. fare, which is likely where you were given this assignment...

Bang away at it and then post your c ode here if you've got specific
issues. No one's going to sit down and write it for you.


Michael
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-21-2005
makko wrote:
> Hello,
> anyone know how to writre a program that take a commandline formula
> and prints the calculated result? example;
> $program 1+(2x3(3/2))-8


Yes.
If you want to steal one, get the freely available source to 'bc'.
And this is not a question about C, so is off-topic here.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-21-2005
makko wrote:
>
> anyone know how to writre a program that take a commandline formula
> and prints the calculated result? example;
> $program 1+(2x3(3/2))-8


Yes.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      01-21-2005
In article <(E-Mail Removed) .com>,
makko <(E-Mail Removed)> wrote:

>anyone know how to writre a program that take a commandline formula
>and prints the calculated result? example;
>$program 1+(2x3(3/2))-8


Unlike some languages, C does not have an "eval" function. So you
will just have to write code to parse and evaluate the expression.
Representing the parsed expressions is a fairly standard problem in
the use of structures. The difficult part is parsing, but C has a
long history of parser-generators many of which are freely available.
Search for "bison" for example.

-- Richard
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      01-21-2005
makko wrote:
> Hello,
> anyone know how to writre a program that take a commandline formula
> and prints the calculated result? example;
> $program 1+(2x3(3/2))-8
>
> reagrds;
> Makkko
>


The interesting part is the kind of data structure
you will use. Are you going to use a stack, queue,
or binary tree? What data structures are you allowed
to use?

If you just need a calculator program, there are free
ones available that do a better job.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      01-21-2005
Thomas Matthews <(E-Mail Removed)> writes:

> makko wrote:
>> anyone know how to writre a program that take a commandline formula
>> and prints the calculated result? example;
>> $program 1+(2x3(3/2))-8

>
> The interesting part is the kind of data structure
> you will use. Are you going to use a stack, queue,
> or binary tree? What data structures are you allowed
> to use?


If you just want to evaluate a formula, there's no need for any
of those data structures, as long as you're allowed to use
recursion.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
Julian V. Noble
Guest
Posts: n/a
 
      01-21-2005
makko wrote:
>
> Hello,
> anyone know how to writre a program that take a commandline formula
> and prints the calculated result? example;
> $program 1+(2x3(3/2))-8
>
> reagrds;
> Makkko


Yes.

I have done it in several languages (not C). Kruse's book "Data Structures and
Program Design" develops a tree-based parser in Pascal. I don't recall whether
or not it is recursive descent, but I don't think so.

My own formula parsers were all recursive descent and used a stack of pointers
into the input string, rather than a parse tree. So it was easy enough to
do it that way. (I am no guru: if I could, you can.)

One thing I hope you have mastered already is RPN ("reverse Polish") or postfix
notation; you should understand why you need to rearrange an algebraic formula
into this form before you can evaluate it.

You might also look at the "dragon book" (Alfred V. Aho, Ravi Sethi, Jeffrey
D. Ullman, "Compilers: principles, techniques, and tools"). They discuss some
important aspects of compiler design and rule-based programs in general, in
particular the BNF notation for expressing rules. If you can state the rules
for your formula evaluator in BNF you will see why recursion is a natural
way to program it.

OTOH, you can do it with "Operator Precedence Grammar" (look it up!), which
might lead to a slightly simpler program.

Once you have done the reading part of your HW, you can tackle the
programming part. I suggest you look into FINITE STATE MACHINES because
that is the easiest way to program pattern-recognition subroutines.

Test the program in bite-sized pieces, i.e. make sure the parser part can
recognize the parts of your formula (terms, factors, operators, functions,
constants, variables, arrays, whatever). Only after that is working should
you decide what to do with the pieces. It would be a good idea to include
diagnostic code while you construct the program--for example something that
will display the current state of the parse tree (if you decide on a tree) or
the expression stack (if you use that), as you decompose the input into
its parts.

If you run into difficulties with C, the folks in this group will help you out,
as I can attest from personal experience. But they really have little interest
in doing your HW for you.

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

"As democracy is perfected, the office of president represents, more and
more closely, the inner soul of the people. On some great and glorious
day the plain folks of the land will reach their heart's desire at last
and the White House will be adorned by a downright moron."

--- H. L. Mencken (1880 - 1956)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting nat problem G.G. Cisco 2 12-02-2005 09:22 PM
Interesting problem with NAT and VPN (not the usual question) Jim Westwood Cisco 6 10-15-2005 05:07 PM
Interesting ping problem Cpt_CAM Cisco 2 04-18-2005 08:51 AM
Interesting Ping Problem with Gigaman Circuits and Cisco 4006 Switches - Anyone? Cpt_Cam Cisco 7 10-05-2004 05:29 AM
BGP and NAT... interesting problem Gollum Cisco 3 12-17-2003 06:22 PM



Advertisments