Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to use c/c++ to simplify an arithmetic expression but it includes unknown symbol;
etc:

(3*x+2*y)*2+x+3 the result will be:7*x+4*y+3

looking for help

thank you
Posted
Comments
H.Brydon 26-Mar-13 23:03pm    
Your question is not clear. What is the unknown symbol?
WaveCQU 26-Mar-13 23:04pm    
thank you for your reply,just like the expression (3*x+2*y)*2+x+3,the 'x'and 'y' is the unknown symbol,

I don't think you really understand it: what you want is called Computer Algebra System (CAS). Please see:
http://en.wikipedia.org/wiki/Computer_algebra_system[^].

At this time, I don't know a CAS library which can be embedded into C++. Besides, I don't think your formulation of the problem is any good. Imaging you got the possibility to perform the symbolic string calculations, but what are you going to do with it? To use it in some sensible way, you would ultimately need to compile it as a C++ expression, but how? C++ is not really a self-compiling system (but I knew some naive people who thought it is, and I would not blame them).

This is not how real CAS systems work. Basically, they are generally based on Expression Trees:
http://en.wikipedia.org/wiki/Expression_tree[^].

Now, if you read this, you can get some ideas. If you are going to implement your own CAS library in C++ and offer the reasonable ways to use it, it would be great. This is quite a feasible problem, but it would require a lot of work.

Good luck,
—SA
 
Share this answer
 
v2
Comments
WaveCQU 27-Mar-13 0:04am    
thank you for your reply, your answer is helpful to me,at least I knew what my question was.
Sergey Alexandrovich Kryukov 27-Mar-13 0:21am    
You are welcome.
—SA
H.Brydon 27-Mar-13 11:22am    
I like your answer better than mine. Yet another +5...
Sergey Alexandrovich Kryukov 27-Mar-13 11:48am    
Thank you very much,
—SA
If you are saying that 'x' and 'y' are unknowns, then what you have is an expression that describes a linear plot. Traditionally you would solve for y, then plot y vs x for some range. Solving for y, you would get

y = -(7*x + 3)/4

-[30]-
 
Share this answer
 
Comments
WaveCQU 26-Mar-13 23:20pm    
maybe you misunderstood my queston, my question is that: in the c++ program, I hava a string like: (3*x+2*y)*2+x+3 , then I want to change this string into a style like: a*x+b*y+c , the a,b,c are constant , I do not have useful way to solve this problem. thank you for your reply
Sergey Alexandrovich Kryukov 26-Mar-13 23:52pm    
I did understand your question correctly in first place. Please see my answer.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900