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

用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]);
   }
  }
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值