CSDN博客

数据结构与算法(C#实现)系列---演示篇(三)

数据结构与算法(C#实现)系列---演示篇()

Heavenkiller(原创)

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 two polynomials

//init polynomial A and show it

ShowSortedList_ShowPolynomial("tmpListA",tmpListA.GetEnumerator());

//init polynomial B and show it

ShowSortedList_ShowPolynomial("tmpListB",tmpListB.GetEnumerator());

//init the key list which contains all keys of A and B but everyone once

IDictionaryEnumerator tmpIDic=tmpListA.GetEnumerator();

while(tmpIDic.MoveNext()!=false)

{

if(!tmpKeyList.ContainsKey(tmpIDic.Key))

{

}

}

tmpIDic=tmpListB.GetEnumerator();

while(tmpIDic.MoveNext()!=false)

{

if(!tmpKeyList.ContainsKey(tmpIDic.Key))

{

}

}

//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;

}

ShowSortedList_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.ToString()+"+";

}

Console.WriteLine(tip+":"+strExpress);

}

}

0 0