944,111 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3811
  • C# RSS
You are currently viewing page 6 of this multi-page discussion thread; Jump to the first page
Oct 5th, 2009
0

Re: Math output

Okay, the FormatText function works, i wrote and tested it in a seperate app. Can you use a breakpoint and tell me what the input is at the start of the method and what it is at the end please.

Also, go line by line to see where it is hanging.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Oct 5th, 2009
0

Re: Math output

Ignore last post...thread hadnt updated to show your last message

Glad you got it working. Took some doing but we got there
Last edited by Ryshad; Oct 5th, 2009 at 10:57 am.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 4th, 2009
0
Re: Math output
For finding nth root , latex format is "\sqrt[]{}" and on button click the form which displays in textbox is : "root:[]{}" . Now to convert this "root:[]{}" into latex format i am using following code :
C# Syntax (Toggle Plain Text)
  1. const string matchNumerator1 = @"root:\((?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
  2. const string matchDenominator1 = @"\((?<Denominator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
  3. const string matchBoth1 = matchNumerator1 + matchDenominator1;
  4. System.Text.RegularExpressions.Match n = System.Text.RegularExpressions.Regex.Match(input, matchBoth1);
  5. while (n.Success)
  6. {
  7. string sNumerator1 = n.Groups["Numerator4"].Value;
  8. string sDenominator2 = n.Groups["Denominator4"].Value;
  9. string latex1 = @"\sqrt[" + sNumerator1.ToString() + "]{" + sDenominator2.ToString() + "}";
  10. input = input.Remove(n.Index, n.Length);
  11. input = input.Insert(n.Index, latex1);
  12. n = System.Text.RegularExpressions.Regex.Match(input, matchBoth1);
  13.  
  14. }

And I using this code just after the fraction code.
But this code is not giving desired result .
Kindly help me out..........
Last edited by vinnijain; Nov 4th, 2009 at 8:55 am.
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Nov 4th, 2009
2
Re: Math output
Of course it isnt working; you told me the format was root:()() so thats what it is looking for.

C# Syntax (Toggle Plain Text)
  1. const string matchNumerator1 = @"root:\[(?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\]";
  2. const string matchDenominator1 = @"\{(?<Denominator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\}";

the above code will find the string you need.


Once again, i cannot understate the importance of learning what the code is doing. The numerator and denimonator code hasnt changed at all from the fraction regex. All that has changed is the enclosing parentheses.

\((?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)

root:\[(?<Numerator4>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\]

You could have easily changed the symbols yourself. I'm not trying to rant or condescend but, if you are trying to learn how to code, these are the kind of problems you will face all the time and learning how to overcome them yourself will make you a much stronger programmer. Every time you encounter new syntax, or a structure you havent used before, experiment with it, read the documentation for it, disect it. You need to understand exactly what its doing so that you can adapt it for future problems. The same goes for code that you are given in forums. We will gladly help you out and provide you with code, but its SO important that you dont just copy and paste the code and move on...learn why the code you are given works. Look at how it has been structured so that you can learn how other coders solve problems.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 4th, 2009
0
Re: Math output
Thanks for replying ........
This I have already done.......but problem is that it is not working when I am trying to enter fraction...........
I don't what's the problem............
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Nov 5th, 2009
0
Re: Math output
Click to Expand / Collapse  Quote originally posted by vinnijain ...
Thanks for replying ........
This I have already done.......but problem is that it is not working when I am trying to enter fraction...........
I don't what's the problem............

Have you added the code i gave you for powers, or have you replaced the old code with the new?

The old code replace fractions, the new code replaces powers. You need to have both. If you have implemented both correctly it will work fine for fractions within powers and vice versa:

C# Syntax (Toggle Plain Text)
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. textBox2.Text = FormatText(textBoxEquation.Text);
  4. }
  5.  
  6. private string FormatText(string input)
  7. {
  8. input = ReplaceFrac(input);
  9. input = ReplacePowers(input);
  10.  
  11. return input;
  12. }
  13.  
  14. private string ReplaceFrac(string input)
  15. {
  16. const string matchNumerator = @"\((?<Numerator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
  17. const string matchDenominator = @"\((?<Denominator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\)";
  18. const string matchBoth = matchNumerator + @"\/" + matchDenominator;
  19.  
  20. System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
  21. while (m.Success)
  22. {
  23. string dNumerator = m.Groups["Numerator"].Value;
  24. string dDenominator = m.Groups["Denominator"].Value;
  25. string latex = @"\frac {" + dNumerator.ToString() + "}{" + dDenominator.ToString() + "}";
  26. input = input.Remove(m.Index, m.Length);
  27. input = input.Insert(m.Index, latex);
  28.  
  29.  
  30. m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
  31. }
  32.  
  33. return input;
  34. }
  35.  
  36. private string ReplacePowers(string input)
  37. {
  38. const string matchNumerator = @"root:\[(?<Numerator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\]";
  39. const string matchDenominator = @"\{(?<Denominator>((?<p>\()|[^\(\)]|(?(p)(?<-p>\))|))*)\}";
  40. const string matchBoth = matchNumerator + matchDenominator;
  41.  
  42. System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
  43. while (m.Success)
  44. {
  45. string dNumerator = m.Groups["Numerator"].Value;
  46. string dDenominator = m.Groups["Denominator"].Value;
  47. string latex = @"\sqrt [" + dNumerator.ToString() + "]{" + dDenominator.ToString() + "}";
  48. input = input.Remove(m.Index, m.Length);
  49. input = input.Insert(m.Index, latex);
  50.  
  51.  
  52. m = System.Text.RegularExpressions.Regex.Match(input, matchBoth);
  53. }
  54. return input;
  55. }
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 5th, 2009
0
Re: Math output
Thanks a lot Ryshad...........
It works............
I hav'nt removed the code but the mistake which I have done was that I added both codes one after the another instead of making functions and the calling those functions.................

I understood my mistake..........
Now the code works fine......................
Thanks once again...............
Reputation Points: 11
Solved Threads: 12
Junior Poster
vinnijain is offline Offline
145 posts
since Jul 2009
Nov 5th, 2009
0
Re: Math output
Click to Expand / Collapse  Quote originally posted by vinnijain ...
Thanks a lot Ryshad...........
It works............
I hav'nt removed the code but the mistake which I have done was that I added both codes one after the another instead of making functions and the calling those functions.................

I understood my mistake..........
Now the code works fine......................
Thanks once again...............
You can call them one after the other, it shouldnt cause a problem.(EDIT thats exactly what happens when you call the methods one after the other)

I just moved them to methods to make the code tidier/more readable/easier to maintain. Also, the regex variables are declared as const so you cant change them so if you wanted both pieces of code in the main method you would need to declare new variables for the new regex

Either way, glad it all works for ya...remember to mark the thread as solved and turn out the lights when you leave
Last edited by Ryshad; Nov 5th, 2009 at 6:52 am.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 5th, 2009
0
Re: Math output
Click to Expand / Collapse  Quote originally posted by vinnijain ...
I added both codes one after the another instead of making functions and the calling those functions
Just a footnote, in C# there arent technically functions. A function is generally a code block that exists outside of an object, a stand alone block of code. As C# is object oriented, all code is associated with a class and is therefore a method.

Many people (including myself on occasion) misuse the terms, so i thought i'd just throw this in to try and un-muddy the waters of c# jargon :p
Last edited by Ryshad; Nov 5th, 2009 at 6:53 am.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC