-
April 10th, 2015, 04:58 PM
#1
Building a parser using two differnt classes
I am in desperate need of some help on creating a parser that has to deal with two different classes that will output a complex math problem. I have written all but the parser in which i don't understand how to start, the purpose is to take the real number and an imaginary number out of the input box and then separates the two for the calculations. What I'm dealing with is like complex numbers. The number of the form is a+bi where a and b are rational and a is the real part of the number and b is the imaginary part.
Also the parser will check to make sure that valid objects are entered. Here is the class that is needing the parser
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rational
{
class CGauss
{
private CRational fcrReal;
private CRational fcrImg;
private bool fbValid;
public CGauss()
{
fbValid = true;
}
public CGauss(string strCGauss)
{
Parse(strCGauss);
}
public override string ToString()
{
string strGauss;
if (!fbValid)
strGauss = "NaN";
else
{
strGauss = fcrReal.ToString() + fcrImg.ToString() + "i";
}
return strGauss;
}
public void Parse(string strCGauss)
{
bool bSlash = false, bLegal = true, bNeg = false;
string strReal = "0/1", strImg = "1/3";
}
public static CGauss operator + (CGauss gNum1, CGauss gNum2)
{
CGauss gSum = new CGauss();
gSum.fcrReal = gNum1.fcrReal + gNum2.fcrReal;
gSum.fcrImg = gNum1.fcrImg + gNum2.fcrImg;
gSum.fbValid = true;
return gSum;
}
public static CGauss operator - (CGauss gNum1, CGauss gNum2)
{
CGauss gSum = new CGauss();
gSum.fcrReal = gNum1.fcrReal - gNum2.fcrReal;
gSum.fcrImg = gNum1.fcrImg - gNum2.fcrImg;
return gSum;
}
public static CGauss operator * (CGauss gNum1, CGauss gNum2)
{
CGauss gSum = new CGauss();
gSum.fcrReal = (gNum1.fcrReal * gNum2.fcrReal) - (gNum1.fcrImg * gNum2.fcrImg);
gSum.fcrImg = (gNum1.fcrReal * gNum2.fcrImg) + (gNum1.fcrReal + gNum2.fcrReal);
return gSum;
}
public static CGauss operator / (CGauss gNum1, CGauss gNum2)
{
CGauss gSum = new CGauss();
gSum.fcrReal = (gNum1.fcrReal * gNum2.fcrReal) + (gNum1.fcrImg * gNum2.fcrImg) / (gNum2.fcrReal * gNum2.fcrReal) + (gNum2.fcrImg * gNum2.fcrImg);
gSum.fcrImg = (gNum1.fcrImg * gNum2.fcrReal) - (gNum1.fcrReal * gNum2.fcrImg) / (gNum2.fcrReal * gNum2.fcrReal) + (gNum2.fcrImg * gNum2.fcrImg);
return gSum;
}
}
}
-
April 12th, 2015, 09:50 AM
#2
Re: Building a parser using two differnt classes
That shouldn't be too hard to make. You would use some of the methods provided by the string class. Note that none of these methods alter the original string (string type is immutable) - they all create a new, altered string, and return that.
If your input is a string of the form "a + bi", you can use the Split() method to split the input at the '+' character.
This method returns an ordinary string array with the results of the split - in your case, it would be {"a ", " bi"}.
E.g.:
string[] parts = input.Split('+');
You can also use Trim() to trim the whitespace on either side (e.g. to turn "a " into "a").
string trimmedPart1 = part1.Trim();
Anyway, once you've got the two parts, use Remove() to remove the "i" from the imaginary component.
string imag = part2.Remove(part2.Length - 1);
That should leave you, if I understood you well, with two strings of the form "p/q".
If so, you can use Split() again on each to split them by the '/' character. (And maybe Trim() as well.)
Once each string is just a number, you can parse them into an appropriate type, e.g., using int.Parse(), like so:
int p = int.Parse(pAsString);
int q = int.Parse(qAsString);
After that, you can use the integers to create the two instances of your CRational class (fcrReal, fcrImg).
-
April 12th, 2015, 09:55 AM
#3
Re: Building a parser using two differnt classes
P.S. For validation, you would check things like if the split results in the expected number of components, and you would catch any exceptions thrown by int.Parse(), etc. If you detect any problems, then you would show a message telling the user that the validation failed.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|