找出与原始数据中没有的数据的算法
找出与原始数据中没有的数据的算法 用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]);
}
}
}
}