Teen Programmers Unite  


Return to forum top

Math strings

Posted by Cortez [send private reply] at March 17, 2003, 12:01:06 PM

Does anyone know any sample C/C++ code for reading mathematical expressions from the console? I mean, a program that can use such an input string:


Of course, there should be a function string table, and a constant char (here 'x') represents the variable, predefined. I tried to figure it out and ended up with something working, but awfully slow. It's for an application that draws the graph of a function, and since i need a detailed graph the string process function should be called a bag of times... I thought there's already an algorithm written for this, and re-inventing the wheel is quite useless ;)

Posted by CViper [send private reply] at March 17, 2003, 02:31:56 PM

There are probably better solutions to this, but you could use the javascript interpretor (overkill) or use flex/bison to create your own parser (still overkill).

I think someone here (u_l?) was working on some interpreting system too..

[edit]Removed some stupid spelling errors; probably still some/alot left[/edit]

Posted by Mike_L [send private reply] at March 21, 2003, 05:12:53 PM

It looks like you are writing an interpreter. An interpreter usually has four parts:

* Tokenizer - identifies individual items like symbols, numbers, and names
* Parser - processes the tokens and constructs a tree that represents the expression
* Compiler - walks the tree, creating a list of basic instructions
* Interpreter - iterates through the lists of instructions, modifying data as they dictate (running the program)

I wrote an interpreter once. Actually it is split into three programs: compiler, assembler, and virtual machine. The compiler reads a file that is written in the VitaminC programming language and writes a file with Sebae assembly language. The assembler then converts that to a Sebae bytecode program. The virtual machine can then load the bytecode and execute the program.

When I first started the project, I went searching for an elegant way to parse the algebraic expressions, and found some interesting techniques, like the Earley parser. Anyway, because I don't yet have the mathematical computer science background, I didn't understand the fancy parsers. Thus, I wrote a parser free-hand. It turned out pretty well.

The compiler portion on the other hand was much more difficult than I had ever imagined. I think that I won't ever again try to write a compiler in C. Next time I will use a higher level language like Scheme or ML.

Anyway, if you want to take a look at Sebae, it can be found at: <a href="http://tamale.net/sebae">http://tamale.net/sebae</a>

I didn't finish the project, though. It will compile simple algebra math with integers. I was implementing function calls when I came to the brink of my body of knowledge. =)

Posted by unknown_lamer [send private reply] at March 23, 2003, 06:11:43 PM

I have a stupid game parsing library that wouldn't really work for this.

/The C++ Programming Language/ has a section on building a desk calculator that would probably work. Also, the GNU Bison manual has a chapter on building a calculator complete with functions using yacc. You could use Bison++ and use the example math parser in your program if you wanted to.

Posted by regretfuldaydreamer [send private reply] at March 24, 2003, 10:59:33 AM

Is Mapel purely Pascal based or does it have some sort of C libary as well?

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.