Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello there, Can anyone help me get started on this:
What i need is: to read a text file containing equations like these
 
2x - 5y + 3z = 10
5x + y - 2z = 4
-4x +3y -z = 6
 
and then do jacobi on them. Can you tell me how I can read those equations from file. I am planning to have those before the "=" sign in an A[][] matrix and the numbers after "=" in B[] matrix.
Posted 9-Oct-12 12:22pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please have a look to the answers in these messages:
Runtime mathematical Formulla[^]
How Can I use the Math char set?[^]
Reduce equation with c#[^]
 
there are plenty of usefull answers and links that can be helpful for you
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 9-Oct-12 19:13pm
   
Well... um... relevant. Oh, my past answer is also there? Sure, a 5. :-)
I also provided a fresh answer here (this time, way more detailed) -- please see.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to create a well-known data structure called "expression tree". A node of a tree should represent an expression and it should have 0, 1 or 2 child nodes representing sub-expressions. The node should be of one of few different classes derived from the common abstract class, let's call this class "Expression". Each terminal derived class representing a node should represent a concrete mathematical operator, like − or +. You need to define some virtual functions used to transform the sub-trees (sub-expressions) under each node, according to the algebra you are going to define. These function should be overridden in each derived class. This way, you will be able to do the operations on the tree based on defined operations on the sub-trees, recursively.
 
In other words, you should define the algebra over the C++ tree structure made of the node (expression) classes I mentioned above and the mapping between this algebra and some traditional mathematical algebra you need to cover in your calculations. Naturally, these two algebras should be isomorphic, and the mapping should be the on-to-one correspondence.
 
After that, you should develop the method to "output" the expression in the form of text, pretty much like the one you've shown. You can also consider support of HTML, XML, and, in particular, MathML, for most advanced option, or some notation of your own. I'm not sure you need it, but this is something you better be aware of:
http://en.wikipedia.org/wiki/MathML[^].
 
And finally, the procedure if population of the expression tree will need to be developed as some kind of parsing. You may need to learn some theory of parsers or… not, as you can do it all by yourself. Anyway, if you feel a need to learn this area, this is a good place to start:
http://en.wikipedia.org/wiki/Parsing_expression_grammar[^].
 
To learn more of expression trees, please see:
http://en.wikipedia.org/wiki/Expression_tree[^].
 
They are widely used at least in two areas: in compilers from a computer languages and, more closely to our topic, in computer algebra systems:
http://en.wikipedia.org/wiki/Compiler[^],
http://en.wikipedia.org/wiki/Computer_algebra_system[^].
 
Please also see my answer and other answers to this question:
Reduce equation with c#[^].
 
The computer algebra systems is something you need to learn, but, in my opinion, general mathematical and programming culture and a reasonably good brain in one's head would be enough to develop such system on your own, especially in case when it does not have to be most comprehensive in the world. As you can see, most of the knowledge you would need is a commonplace pretty well covered by Wikipedia.
 
Wish you the best of luck,
—SA
  Permalink  
v2
Comments
Kuthuparakkal at 9-Oct-12 22:11pm
   
My 5+
Sergey Alexandrovich Kryukov at 10-Oct-12 1:22am
   
Thank you,
--SA
pasztorpisti at 10-Oct-12 4:15am
   
5ed, I hope this wasn't wasted effort! :-)
Sergey Alexandrovich Kryukov at 10-Oct-12 10:53am
   
Thank you. At least, I'll be able to refer to this post. :-)
--SA
Nelek at 10-Oct-12 4:33am
   
Nice one :) +5
Sergey Alexandrovich Kryukov at 10-Oct-12 10:53am
   
Thank you, Nelek.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think the question was how to read the strings from the text file.
So, it depends on what platform Sam is going to use. He has tagged the question as C++, so, the best guess is that he is going to use an stl. So, for reading from files he will need sdt::istream with getline(). The he will need to parse each line using std::string's methods find(), rfind(), etc. methods.
 
If his library of choice is MFC then it will be CStdioFile with its ReadString method. Then he will need to process CStrings using Find, Tokenize methods. ATL has similar classes
 
If it is C#, then there are similar classes there as well.
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 309
1 CPallini 300
2 Sergey Alexandrovich Kryukov 290
3 George Jonsson 274
4 Gihan Liyanage 230
0 OriginalGriff 4,895
1 CPallini 4,010
2 Sergey Alexandrovich Kryukov 3,514
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 9 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100