找出与原始数据中没有的数据的算法

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

用C# 呵NUnit 做开发呵测试工具

using System;

using System.Collections;

using NUnit.Framework;

namespace cn.lovetyping.UnitTest

{

/// <summary>

/// Sort 的摘要说明。

/// </summary>

///

[TestFixture]

public class Sort

{

/// <summary>

/// the orignal data which is used to compare with the new

/// data.If there is some data exist in the newData but not in orignal data.

/// add it to the result.

/// </summary>

/// Input:

/// <param name="orignal"></param>

/// <param name="newData"></param>

/// Output

/// <returns>arrayList</returns>

public ArrayList sortData(string[] orignal,string[] newData)

{

ArrayList result = new ArrayList();

int oIndex=0,nIndex=0;

//according to the condition

while(oIndex<orignal.Length && nIndex<newData.Length)

{

if(newData[nIndex].CompareTo(orignal[oIndex]) <0)

{

result.Add(newData[nIndex]);

nIndex++;

}

else if(newData[nIndex].CompareTo(orignal[oIndex]) ==0)

{

oIndex++;nIndex++;

}

else if(newData[nIndex].CompareTo(orignal[oIndex]) > 0)

{

oIndex++;

continue;

}

}

//if the

if(nIndex == newData.Length || oIndex< orignal.Length)

{

return result;

}

else if( nIndex < newData.Length)

{

while(nIndex< newData.Length)

{

result.Add(newData[nIndex++]);

}

}

return result;

}

[Test]

public void testSort()

{

string[] code1 = new string[]{"0","4","6","9"};

string[] code2 = new string[]{"1","3","6","7","9","12"};

ArrayList result = this.sortData(code1,code2);

Assert.IsTrue(result.Count == 4);

for(int i=0;i<result.Count;i++)

{

Console.WriteLine(result[i]);

}

Console.WriteLine("---------------Another data------------");

code1 = new string[]{"0","4","6","9"};

code2 = new string[]{"1","3","6"};

result = this.sortData(code1,code2);

Assert.IsTrue(result.Count == 2);

for(int i=0;i<result.Count;i++)

{

Console.WriteLine(result[i]);

}

Console.WriteLine("---------------Another data------------");

code1 = new string[]{"0","4","6","9"};

code2 = new string[]{"1","3","7"};

result = this.sortData(code1,code2);

Assert.IsTrue(result.Count == 3);

for(int i=0;i<result.Count;i++)

{

Console.WriteLine(result[i]);

}

Console.WriteLine("---------------Another data------------");

code1 = new string[]{"0","11","13","3","3","9"};

code2 = new string[]{"1","11","15","16","19","3","7"};

result = this.sortData(code1,code2);

Assert.IsTrue(result.Count == 5);

for(int i=0;i<result.Count;i++)

{

Console.WriteLine(result[i]);

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航