Click here to Skip to main content
15,921,179 members
Home / Discussions / C#
   

C#

 
GeneralRe: PolyLib Modifying the ToString method Pin
Ryano12113-Feb-11 7:43
Ryano12113-Feb-11 7:43 
AnswerRe: PolyLib Modifying the ToString method Pin
Luc Pattyn14-Feb-11 1:03
sitebuilderLuc Pattyn14-Feb-11 1:03 
GeneralRe: PolyLib Modifying the ToString method Pin
Ryano12114-Feb-11 2:22
Ryano12114-Feb-11 2:22 
Sorry about that

Basically the Polynomial class has a Complex[] field that stores the coefficients of the equation. e.g 1,7,12 for x^2 + 7x + 12. The class has an overriden ToString method that gives a representation of what the equation is. However the given implementation given below provides 12 + 7x + x^2 instead of the conventional x^2 + 7x + 12 that i am looking for.

public override string ToString()
          {
              if (this.IsZero()) return "0";
              else
              {
                  string s = "";                               
  
                  for (int i = 0; i < Degree + 1; i++)
                  {
                      if (Coefficients[i] != Complex.Zero)
                      {
                          if (Coefficients[i] == Complex.I)
                              s += "i";
                          else if (Coefficients[i] != Complex.One)
                          {
                              if (Coefficients[i].IsReal() && Coefficients[i].Re > 0)
                                  s += Coefficients[i].ToString();
                              else
                                  s += "(" + Coefficients[i].ToString() + ")";
                                  
                          }
                          else if (/*Coefficients[i] == Complex.One && */i == 0)
                              s += 1;
                                                  
                          if (i == 1)
                              s += "x";
                          else if (i > 1)
                              s += "x^" + i.ToString();                        
                      }
  
                      if (i < Degree && Coefficients[i + 1] != 0 && s.Length > 0)                    
                          s += " + ";                    
                  }
  
                  return s;
              }


Or also here

http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=19446[^]

I have tried to modify the for loop to

for (int i = Degree -1; i >= 0; i--)


But that gives the + signs after the equation. I have also tried to modify other parts of the method yet it usually results in array index out of bounds exceptions.

Can anyone give me some pointers on how to modify the method to give the 'conventional' interpretation of equations?

Thanks again
GeneralMessage Removed Pin
14-Feb-11 2:24
Ryano12114-Feb-11 2:24 
GeneralRe: PolyLib Modifying the ToString method Pin
Luc Pattyn14-Feb-11 2:46
sitebuilderLuc Pattyn14-Feb-11 2:46 
Questionerror in passing value from one form to other Pin
aeman13-Feb-11 1:28
aeman13-Feb-11 1:28 
AnswerRe: error in passing value from one form to other Pin
Wendelius13-Feb-11 1:49
mentorWendelius13-Feb-11 1:49 
AnswerRe: error in passing value from one form to other Pin
Luc Pattyn13-Feb-11 1:52
sitebuilderLuc Pattyn13-Feb-11 1:52 
QuestionPlease can someone do me a favour and run a C# test for me Pin
Dave Midgley12-Feb-11 23:49
Dave Midgley12-Feb-11 23:49 
AnswerRe: Please can someone do me a favour and run a C# test for me Pin
OriginalGriff13-Feb-11 0:36
mveOriginalGriff13-Feb-11 0:36 
GeneralRe: Please can someone do me a favour and run a C# test for me Pin
Dave Midgley13-Feb-11 6:05
Dave Midgley13-Feb-11 6:05 
GeneralRe: Please can someone do me a favour and run a C# test for me Pin
OriginalGriff13-Feb-11 6:08
mveOriginalGriff13-Feb-11 6:08 
AnswerRe: Please can someone do me a favour and run a C# test for me Pin
PIEBALDconsult13-Feb-11 3:22
mvePIEBALDconsult13-Feb-11 3:22 
GeneralRe: Please can someone do me a favour and run a C# test for me Pin
Dave Midgley13-Feb-11 6:06
Dave Midgley13-Feb-11 6:06 
GeneralRe: Please can someone do me a favour and run a C# test for me Pin
OriginalGriff13-Feb-11 6:09
mveOriginalGriff13-Feb-11 6:09 
GeneralRe: Please can someone do me a favour and run a C# test for me Pin
Richard MacCutchan13-Feb-11 6:24
mveRichard MacCutchan13-Feb-11 6:24 
GeneralRe: Please can someone do me a favour and run a C# test for me Pin
PIEBALDconsult13-Feb-11 14:58
mvePIEBALDconsult13-Feb-11 14:58 
AnswerRe: Please can someone do me a favour and run a C# test for me Pin
Dave Kreskowiak13-Feb-11 10:59
mveDave Kreskowiak13-Feb-11 10:59 
GeneralRe: Please can someone do me a favour and run a C# test for me Pin
Dave Midgley13-Feb-11 20:19
Dave Midgley13-Feb-11 20:19 
QuestionVariable to be Accessed throughout code [modified] Pin
MWRivera12-Feb-11 16:37
MWRivera12-Feb-11 16:37 
AnswerRe: Variable to be Accessed throughout code Pin
OriginalGriff12-Feb-11 21:21
mveOriginalGriff12-Feb-11 21:21 
GeneralRe: Variable to be Accessed throughout code Pin
MWRivera13-Feb-11 4:07
MWRivera13-Feb-11 4:07 
GeneralRe: Variable to be Accessed throughout code Pin
OriginalGriff13-Feb-11 5:42
mveOriginalGriff13-Feb-11 5:42 
GeneralRe: Variable to be Accessed throughout code Pin
MWRivera14-Feb-11 15:31
MWRivera14-Feb-11 15:31 
AnswerRe: Variable to be Accessed throughout code Pin
RichardGrimmer15-Feb-11 3:55
RichardGrimmer15-Feb-11 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.