473,732 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

interesting problem

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

Nov 14 '05 #1
16 1726
-----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)

iD8DBQFB8QqvagV FX4UWr64RAnfhAK DSEGoKDT0pBPrGb mK/+pfq91RmwACfSDH l
bBxjlAt+dxVAdzV E3EgHIww=
=jM5G
-----END PGP SIGNATURE-----
Nov 14 '05 #2
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
Nov 14 '05 #3
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
Nov 14 '05 #4
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.
Nov 14 '05 #5
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.c om, 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

Nov 14 '05 #6
In article <11************ **********@c13g 2000cwb.googleg roups.com>,
makko <el*****@gmail. com> 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
Nov 14 '05 #7
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.l earn.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

Nov 14 '05 #8
Thomas Matthews <Th************ *************@s bcglobal.net> 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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #9
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
jv*@lessspamfor mother.virginia .edu
^^^^^^^^^^^^^^^ ^^^
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)
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

56
4118
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation error. (Not as infrequent as some bugs that have been discussed here in the past, but maybe once in an overnight run of the program when it was configured to aggressively exercise the section that the bug was in.) It was easy enough to trap the...
23
2035
by: Bruno R. Dias | last post by:
Perhaps it would be interesting to program a virtual machine simulating an ancient computer (such as the pdp-7). Then, it would be rather interesting to code for it (porting gcc to it maybe?). I think it would be fun to play with the long-forgotten art of coding in machine language. And what about a fictional computer, such as one that works on an entirely different way (such as a non-binary computer)? It wouldn't be very useful, but...
7
2867
by: David Sworder | last post by:
Hi, I'm developing an application that will support several thousand simultaneous connections on the server-side. I'm trying to maximize throughput. The client (WinForms) and server communicate via a socket connection (no remoting, no ASP.NET). The client sends a message to the server that contains some instructions and the server responds in an asynchronous fashion. In other words, the client doesn't block while waiting for the...
1
259
by: Rakesh Roberts | last post by:
I think I have a very interesting cookie problem. I use form authentications on my application. Through out my application I started using a toggle control that persists its value for the session using cookies that it writes to on the client side (using javascript). What happens sometimes it that the application forces the user back to the authentication page ( the login page). I suspect this has something to do with the limit on the...
2
2101
by: sasifiqbal | last post by:
Hi, One of my developers are facing an interesting problem regarding UserControl invalidation. The problem is: We have two forms in our application. Form A does nothing except loading of Form B and Form B contains an array of UserButton kind of contol (we have created our own buttons deriving from UserButton). All the drawings activities in Form B are okay and nothing gets wrong if we
27
2335
by: Frederick Gotham | last post by:
I thought it might be interesting to share experiences of tracking down a subtle or mysterious bug. I myself haven't much experience with tracking down bugs, but there's one in particular which comes to mind. I was writing usable which dealt with strings. As per usual with my code, I made it efficient to the extreme. One thing I did was replace, where possible, any usages of "strlen" with something like: struct PtrAndLen { char *p;
6
3271
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls to the function in the container. I allocate (and free in c) arrays to get garbage collection in both C and C#. After a few seconds I get an exception. I assume GarbageCollector moves the delegate (or collects is) and when I don't find it in C...
5
1967
by: Will Honea | last post by:
I've hit an interesting trap trying to migrate data off an OS/2 server running version 7.2 (fp14) over to 8.2 on Linux. Seems that one table has a column defined in the DDL as "BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH +1, INCREMENT BY +2, NO CACHE)". Any rows in that table will kill db2move. EXPORT to IXF succeeds, but attempting to load/import the file refuses to load any rows. The really scary part is that this table...
11
1571
by: onkar.n.mahajan | last post by:
Is it possible to from function call on fly in C programming language ? I am faced with an interesting problem in that I need to take function name and arguments on fly (actually from Database as string ) and form a function call based on that ? something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this pair function call must be
4
1880
by: Andrew | last post by:
I am having an interesting namespace conflict. When we use a third party lib we create a company assembly for any descending classes to go in. I have simplified the problem into the example below. We are using the third party assembly Abc.Reports so we have an assembly called ComanyName.Abc. The problem is when I try to use the class Abc.Reports.Thing it tries to find it in CompanyName.Abc.Reports.Thing.
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9306
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.