Wed, 23 Aug 2000 crzhang wrote:
> Hi all:
> Now i have a string "3*2*sin2+2*1", i want to get this string's mathematic
> result, but i don't know how to do.Can you help me?
Well, if you want to do it fast and have full control over your code, you can
hack an expression parser of some kind. The easiest way is probably to use
bison or some other parser generator. These take some kind of syntax
description, your custom "action" functions to execute when constructs are
recognized, and generate C or C++ source code.
If you want more speed in the actual calculations (not that these tools are
necessarily slow - they do work on the ASCII string level, though), you could
write a bytecode compiler rather than a parser, still using the same parser
generator. Not very different; instead of performing the actions right away, you
build a list of assembly-like instructions (byte code string or list of event
structures) that you execute with an optimized function when you want to do
the actual calculation. It's quite some work to design a real virtual machine +
compiler, but it's much simpler without flow control, structured data types and
all that.
Of course, it's not exactly as if you were the first person to need something
like this, so there are probably various ready-to-use solutions floating
around. I've seen quite a few for Delphi (Object Pascal) and other development
tools for that other "OS", but I haven't actively looked for more generic C/C++
code; at least not for simple expression parsing. However, there are probably a
few of those among the countless parser/interpreter/compiler resources on the
net. (Unfortunately, I don't seem to have the links on this machine...)
David Olofson
Programmer
Reologica Instruments AB
[EMAIL PROTECTED]
..- M u C o S --------------------------------. .- David Olofson ------.
| A Free/Open Multimedia | | Audio Hacker |
| Plugin and Integration Standard | | Linux Advocate |
`------------> http://www.linuxdj.com/mucos -' | Open Source Advocate |
..- A u d i a l i t y ------------------------. | Singer |
| Rock Solid Low Latency Signal Processing | | Songwriter |
`---> http://www.angelfire.com/or/audiality -' `-> [EMAIL PROTECTED] -'
-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
---
For more information on Real-Time Linux see:
http://www.rtlinux.org/rtlinux/