473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

parsing equations

hi all,

i have a question.
Given the string '3+2*6' i need to parse it and obtain the result, so
15. Does a module for this operation exists? i need to do simple
operations, but also on floating point numbers.

many thanks

Jul 18 '05 #1
4 2143
> i have a question.
Given the string '3+2*6' i need to parse it and obtain the result, so
15. Does a module for this operation exists? i need to do simple
operations, but also on floating point numbers.


Use the builtin function eval to evaluate an arbitrary python expression:
eval("3+2*6")

15
Regards,

Diez
Jul 18 '05 #2
Diez B. Roggisch wrote:
i have a question.
Given the string '3+2*6' i need to parse it and obtain the result, so
15. Does a module for this operation exists? i need to do simple
operations, but also on floating point numbers.


Use the builtin function eval to evaluate an arbitrary python expression:
eval("3+2*6") 15


Be aware that this is powerful:
eval("cleanMyHarddisk()")

'done'

Just to make sure the OP is aware of the inherent danger of the unsuspecting
"arbitrary python expression"...

Peter
Jul 18 '05 #3
> Be aware that this is powerful:
eval("cleanMyHarddisk()")

'done'

Just to make sure the OP is aware of the inherent danger of the
unsuspecting "arbitrary python expression"...


Yup - should have mentioned that.

Diez
Jul 18 '05 #4
Diez B. Roggisch wrote:
Use the builtin function eval to evaluate an arbitrary python expression:
eval("3+2*6")

many thanks... it's exactly what i need :)

Jul 18 '05 #5

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

Similar topics

0
by: smjmitchell | last post by:
Hi All, I need to display some equations on a form in VB (I will print the result in a text box beside the equation). The equations will in some cases be quite complicated and include...
7
by: Paddy McCarthy | last post by:
Hi, I am trying to use eval as little as possible but solve this problem. #If given:two or more lambda equations x=lambda : A < B y=lambda : C+6 >= 7 .... How do I create another lambda...
5
by: gamehack | last post by:
Hi all, I was thinking about parsing equations but I can't think of any generic approach. Basically I have a struct called math_term which is something like: struct math_term { char sign; int...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
4
by: Thomas | last post by:
Hi, I've to solve a system of linear equations and inequations in C++. Are there (free) librarys for that topic? Regards, Thomas
8
by: Blah | last post by:
Hi, this is a small program to print out three lists of values. One is a simple increment count the others are values from two equations. I've run this without problems before but when I've put in...
1
by: easternshredman | last post by:
I've got a project due tomorrow. I tried to do it in Maple because of the math functions available but it wasn't very user friendly so I switched over to VB. I do need to know how to make equations...
2
by: DaRok28 | last post by:
// Program Description: // This program solves quadratic equations to find their roots. This // program takes values of a, b, and c as input and outputs the root(s). // The user can repeat the...
4
by: sdufoo | last post by:
Hallo guys, I have to solve a system of Differential equations: http://picasaweb.google.de/sdufoo/EcuacionesDiferenciales02/photo#5228386949051570594 these are the equations of an induction...
1
by: HypeBeast McStreetwear | last post by:
Hello everyone. I got a assignment that states. The set of linear equations a11X1 = a12X2 = c1 a21X1 = a22X2 = c2 May be solved using Cramer’s rule: X1 = c1a22 – c2a12 a11a22 –...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.