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

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

using System;
using System.Collections;
using NUnit.Framework;

namespace cn.lovetyping.UnitTest
{
///

1<summary>   
2/// Sort 的摘要说明。   
3/// </summary>

///
[TestFixture]
public class Sort
{
///

1<summary>   
2/// the orignal data which is used to compare with the new   
3/// data.If there is some data exist in the newData but not in orignal data.   
4/// add it to the result.   
5/// </summary>

/// Input:
///

1<param name="orignal"/>

///

1<param name="newData"/>

/// Output
///

1<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

 1<orignal.length &&="" <0)="" else="" if(newdata[nindex].compareto(orignal[oindex])="" nindex++;="" nindex<newdata.length)="" oindex++;nindex++;="" result.add(newdata[nindex]);="" {="" }=""> 0)   
 2{   
 3oIndex++;   
 4continue;   
 5}   
 6}   
 7//if the   
 8if(nIndex == newData.Length || oIndex&lt; orignal.Length)   
 9{   
10return result;   
11}   
12else if( nIndex &lt; newData.Length)   
13{   
14  
15while(nIndex&lt; newData.Length)   
16{   
17result.Add(newData[nIndex++]);   
18}   
19}   
20return result;   
21} 
22
23[Test]   
24public void testSort()   
25{   
26string[] code1 = new string[]{"0","4","6","9"};   
27string[] code2 = new string[]{"1","3","6","7","9","12"}; 
28
29ArrayList result = this.sortData(code1,code2);   
30Assert.IsTrue(result.Count == 4);   
31for(int i=0;i&lt;result.Count;i++)   
32{   
33Console.WriteLine(result[i]);   
34} 
35
36Console.WriteLine("---------------Another data------------");   
37code1 = new string[]{"0","4","6","9"};   
38code2 = new string[]{"1","3","6"};   
39result = this.sortData(code1,code2);   
40Assert.IsTrue(result.Count == 2);   
41for(int i=0;i&lt;result.Count;i++)   
42{   
43Console.WriteLine(result[i]);   
44} 
45
46Console.WriteLine("---------------Another data------------");   
47code1 = new string[]{"0","4","6","9"};   
48code2 = new string[]{"1","3","7"};   
49result = this.sortData(code1,code2);   
50Assert.IsTrue(result.Count == 3);   
51for(int i=0;i&lt;result.Count;i++)   
52{   
53Console.WriteLine(result[i]);   
54} 
55
56Console.WriteLine("---------------Another data------------");   
57code1 = new string[]{"0","11","13","3","3","9"};   
58code2 = new string[]{"1","11","15","16","19","3","7"};   
59result = this.sortData(code1,code2);   
60Assert.IsTrue(result.Count == 5);   
61for(int i=0;i&lt;result.Count;i++)   
62{   
63Console.WriteLine(result[i]);   
64}   
65}   
66}   
67}</orignal.length>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus