|
Basically that's just the way it works. That shouldn't be a problem though, because you can just make a nonstatic function whose only purpose is to call disp . (not always needed, but that way disp is still static so it wouldn't accidentally break other code which relies on that)
|
|
|
|
|
I don't see a static function (method) in there.
|
|
|
|
|
oh sorry. i done a mistake. i wrote public class int disp() but it is public static int disp().
|
|
|
|
|
I don't think this should even compile, the line
public class int disp()
is not valid.
The best things in life are not things.
|
|
|
|
|
yes u r right sir. i done mistake. i make disp() class but it was a static method.
|
|
|
|
|
*DBAU wrote: public class int disp()
Your code will not compile at all. Did you mean this?
public static int disp()
|
|
|
|
|
|
Hello.
I am still learning C# and as a new project, I wanted to write a simple chemical equation balancer, but dont really know where to start.
I am not asking for code, as I really want to have a go at this myself, so if I show my views below on how I would imagine tackling this problem, maybe someone could point me in the correct direction.
An example of a chemical equation, a simple one, would be...
H2 + O2 = H2O
Balanced, we end up with.....
2H2 + O2 = 2H2O
I was thinking of the following steps...
1) Parse the left side (reactants) into one string, and the right side (Products) into a seperate string.
2) Loop through the string looking for the letters representing the elements and the numbers to count how many we have
3) ** Havent got a clue how to manage this ** maybe use some kind of matriz to store all the values and then use logic to "balance" the equation.
Does anyone have any ideas or tips?
Thank you,
Regards,
Steve
|
|
|
|
|
Well, when you're balancing a chemical equation, all you're really allowed to do (If I remember my high school chemistry) is adjust the amount of each component, right?
So I think you need to do this in a recursive loop with three main steps:
Step 1: Figure out the amount of each individual element, subtract the two sides, and see how much you have to add on each side to balance it.
Step 2: Pick one of these imbalances and figure out which terms contain that element. Prioritize terms based on how many other elements they affect (If you need to increase Hydrogen, prioritize the H2 term above the H2SO4 term).
Step 3: For each of these terms (In order of priority), increase the amount of that term to counter the imbalance, then repeat this process with the modified equation (Using recursion, so you come back to try a different term if it fails).
There's probably a better way, but this is the first one that popped into my mind.
Example:
H2 + O2 = H2O
1) 2H + 2O = 2H + O.... Subtract and you see you need another O on the right side
2) There's only one term that contains O on the right (Only one term, period)
3) Increase H2O to 2H2O to add one more O. New equation is H2 + O2 = 2H2O...
1a) 2H + 2O = 4H + 2O... Subtract, and you now need 2H on the left side
2a) Only one term contains it (H2)
3a) Increase that to 2H2 to get the extra two hydrogens. New equation is 2H2 + O2 = 2H2O
1b) 4H + 2O = 4H + 2O... Subtract, and it's balanced, so return it.
Obviously a simple example.
|
|
|
|
|
Hi,
Thank you, this was pretty much what I was thinking.
Do you have any ideas of the best way to store and sort the terms?
i.e. Should I use a struct for the elements? etc
I am still a beginner so any advice is most welcomed.
Kind Regards,
Stephen
|
|
|
|
|
Well, if you want to keep everything organized, maybe something like:
Element class: Stores a certain quantity of one element (i.e. 2 x H)
- Symbol
- Quantity
FormulaTerm class:
- List/Array of Elements (Each with a quantity)
- Text representation (Taken from the original formula, so it would know to represent 2H,O as "H2O" - More important for complex formulas that could have the same element multiple times)
- Quantity ("2" for "2H2O")
* Methods to add to other FormulaTerms, and to extract Elements (Multiply Quantity by each Element)
FormulaSide class:
- List/Array of FormulaTerms
* Method to combine with another FormulaSide to find the imbalances
|
|
|
|
|
I would probably set this up as two arrays, operands (left side) and results (right side). The arrays would contain structures that have two elements. The first element would be the multiplier for the particular item, and a dictionary with the element symbol as the key and the how much as the value.
The Dictionary object has a sort method built in, but you'll probably want to write your own to meet the requirements for sorting the particular elements according the the standard rules of chemistry. For example S comes before O for SO4.
I hope you find this approach helpful. If you have any questions, feel free to ask.
Scott E. Corbett
Software Engineer/Analyst
|
|
|
|
|
H2 + O2 = H2O
...
2H2 + O2 = 2H2O
hmm...
here's what i'd try for a first attempt:
1. parse the equation into left and right: "H2 + O2" and "H2O"
2. break it into molecules. left side "H2" "O2", right side "H20"
3. break the molecules into elements, pay attention to the counts
4. put each element into a container that will hold a label and a count (multiset, maybe)
ex.
left side:
Add H (container has: H:1)
Add H (container has: H:2)
Add O (container has: H:2, O:1)
Add O (container has: H:2, O:2)
right side:
Add H (container has: H:1)
Add H (container has: H:2)
Add O (container has: H:2, O:1)
5. compare the two containers to see find the differences . in this case the only difference is that the left has O:2 but the right has O:1, so there's the imbalance.
to balance, you have to adjust the molecule counts to get the element counts right... which is probably a bit of work
|
|
|
|
|
Just my tuppenceworth, the other answers have been good.
There is a full balancing method here[^]. It should transfer to an algorithm easily.
For the data representation I'd use two dictionaries, one for the number atoms on the LHS, the other for the right. You can make the chemical symbol the key, but you might also like an element class, that way you can do other handy stuff like work out the molar mass of the products etc.
I actually think the formula parser might be the hard part, but not having worked on the problem fully I could well be wrong.
|
|
|
|
|
No trial and error or inelegant algorithms are needed.
The problem can be solved by solving a system of linear equations, constructed like this:
As2S3 + HNO3 + H2O = H3AsO4 + H2SO4 + NO (unbalanced)
Should be turned into the system: (disclaimer: it was really quite late when I did this, so don't look at the numbers too closely the general principle should be sound though)
2 x0 + 0 x1 + 0 x2 - 1 x3 - 0 x4 - 0 x5 = 0 // for As
3 x0 + 0 x1 + 0 x2 - 0 x3 - 1 x4 - 0 x5 = 0 // for S
0 x0 + 1 x1 + 2 x2 - 3 x3 - 1 x4 - 0 x5 = 0 // for H
0 x0 + 1 x1 + 0 x2 - 0 x3 - 0 x4 - 1 x5 = 0 // for N
0 x0 + 3 x1 + 1 x2 - 4 x3 - 4 x4 - 1 x5 = 0 // for O
The variables correspond to how many times the molecule from the unbalanced equation is needed, the coefficients correspond to how many of the element are in the corresponding molecule.
If I did the math correctly, it gives:
3 As2S3 + 28 HNO3 + 4 H2O = 6 H3AsO4 + 9 H2SO4 + 28 NO
Does that help you any?
|
|
|
|
|
yep, linear equations it is; each different element gives one equation, and the unknowns are the amounts of each of the molecules.
the one funny thing is there could be more equations than unknowns, making either some of them redundant, or the problem unsolvable (obviously you can't balance H2O = H2SO4 ).
|
|
|
|
|
Luc Pattyn wrote: yep, linear equations it is
Geez, man. Is there anything you don't know?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I only know a tiny fraction of all there is to know, however I try and make up for it by applying simple logic.
And chemical formulae are all about preserving the elements...
|
|
|
|
|
Dear David1987,
Sorry, no, it does not help at all unfortunatly. I can't seem to work out what is going on here, although I suspect if I did it may prove a usefull way of approaching the problem.
Is there any chance you could show me what you mean, or maybe simplyfy it a little?
Thank you
|
|
|
|
|
Ok, let's take O2 + H2 = H2O then.
First I'll assign variables to the molecules:
O2 = x0
H2 = x1
H2O = x2
Then I build the equations, one for each element, the coefficients are how much of the current element is in the corresponding molecule:
H: 0 x0 + 2 x1 - 2 x2 = 0 // there is zero H in O2, 2 H in H2, 2 H in H2O
O: 2 x0 + 0 x1 - 1 x2 = 0 // there is 2 O in O2, zero O in H2, 1 O in H2O
The smallest solution where all variables are integers is x0 = 1, x1 = 2, x2 = 2, which corresponds to O2 + 2 H2 = 2 H2O
|
|
|
|
|
Hi,
OK that helps a little better, but I would have no idea how to implement this into code. It has gave me something to think about though.
Thank you again,
Stephen
|
|
|
|
|
As others have noted, the problem can be re-stated in terms of a system of linear equations. A particularly easy method to solve such systems (in relative terms, of course - it's a decent homework assignment for a second-year CS student) is the method of Gaussian Elimination[^].
Here is a Java implementation of what you are trying to build[^], conveniently packaged with a source code. The implementation is very easy to follow, even if you do not know Java (but do know some C#).
Good luck!
|
|
|
|
|
Thank you everyone for your help.
However, I think as a complete beginner, I may have bitten off more than I can chew, as I dont have a clue where to start based on the information provided.
Thank you again,
Stephen
|
|
|
|
|
If college chemistry can offer me anything it was that no equation balancer should be based on text input. The one I had to use was so bad I dared the prof. to fail me because I wasn't going to waste my time discovering how it wanted me to type.
Graphics and numeric up downs in a visually appealing manner are the way to go.
|
|
|
|
|
Lets start with a piece from the book:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TernaryOperator
{
class Time
{
int Year;
int Month;
int Date;
int Hour;
int Minute;
int Second;
public void DisplayCurrentTime()
{
Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
Month, Date, Year, Hour,Minute,Second);
Console.ReadKey();
}
public Time(System.DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
}
class Program
{
static void Main(string[] args)
{
System.DateTime currentTime = System.DateTime.Now;
Time t = new Time(currentTime);
t.DisplayCurrentTime();
}
}
}
For the constructor portion, where I declared or created the reference in the constructor, the values aren't actively stored in my variables yet, am I right?
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
So that means my 'Time t = new Time(currentTime)' is storing the active vars into my constructor using the 'dt' reference?. Correct? I feel like I am almost lost.
|
|
|
|
|