您的位置:首页 > 编程语言 > PHP开发

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

2005-09-04 00:01 274 查看
用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]);
   }
  }
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐