Data Structure and Algorithm (C # Implementation) Series --- Demonstration (3)
HEAVENKILLER (original)
Public static void showsortedList_PolyNomial ()
{
// 100 10 * x x ^ 2 1 10 * x 100X ^ 2
Sortedlist tmplista = new sortedlist ();
SortedList Tmplistb = new sortedlist ();
SortedList TMPLISTC = New SortedList (); // used to store the result
SortedList TmpKeylist = new sortedlist (); // used to store all keys of tour polynomials
// init polynomial a and show it
TMPLista.Add (0,100);
TMPLista.Add (1, 10);
TMPLista.Add (2, 1);
ShowsortlishedList_ShowPolyNomial ("Tmplista", TMPLista.GeteNumerator ());
// init polynomial b and show it
TMPListb.Add (0, 1);
TMPListb.Add (1, 10);
TMPListb.Add (2,100);
ShowsortlishedList_ShowPolyNomial ("Tmplistb", TMPListb.GeteNumerator ());
// init the key list Which contains all keys of a and b but everyone overce
IDictionaryenumerator tmpidic = tmplista.getenumerator ();
While (tmpidic.movenext ()! = false)
{
IF (! TMPKEYLIST.CONTAINSKEY (TMPIDIC.KEY))
{
TMPKEYLIST.ADD (Tmpidic.Key, NULL);
}
}
Tmpidic = tmplistb.getenumerator ();
While (tmpidic.movenext ()! = false)
{
IF (! TMPKEYLIST.CONTAINSKEY (TMPIDIC.KEY))
{
TMPKEYLIST.ADD (Tmpidic.Key, NULL);
}
}
// Add a and b and show the result
Tmpidic = TMPKEYLIST.GETENUMERATOR ();
While (tmpidic.movenext ()! = false)
{
Object obja = null, objb = null, objc = NULL
Objc = tmpidic.key;
IF (TMPLista.Containskey (OBJC))
Obja = TMPLISTA [OBJC];
IF (TMPLista.Containskey (OBJC))
Objb = tMPLISTB [OBJC];
// Objc = Obja Objb;
// TMPKEYLIST [OBJC] = (int) Obja (int) Objc;
TMPLISTC.ADD (Objc, (int) Obja (int) Objb);
}
ShowsortlishedList_ShowPolyNomial ("The Addition Result of A and B", TMPListC.GeteNumerator ());
}
Public static void showsortedList_ShowPolyNomial (String Tip, iDictionaryEnumerator IDIC) {
String strexpress = NULL;
Idic.reset ();
While (IDIC.MOVENEXT ()! = false)
{
StrexPress = IDIC.Value.toString () "* x ^" idic.key.toT7tring () " ";
}
Console.WriteLine (TIP ":" strexpress);
}
}