其实这篇博客早就该写了,这是我来北京找工作的时候,收到的一个公司给我笔试题。要求是这样的:
大家也可以试着做做,我想对于那些大牛来说,太小儿科了。但是,对于我们这些刚刚进入职场的小鸟来说,还是要花一些时间的。我自己做了一个下午才做出来。
不过,今天在公司的时候,想写一个总结,可是在电脑却怎么也运行不出来了,报一个非常不也的错误。
后来,我找到了报错的原因,可是并不是代码的问题,是因为我开始是在我的笔记本上做的,因为我笔记本上装的是vs2012,然后生成代码时,怕看代码的人没有高版本的VS,由于生成的是.netframework4.0。然后在公司的时候,打开就运行不出来,然后回家之后在VS2012上还原回.netframework4.5就好了。
这也让我引发了一个思考,我个人觉得,新版本的编程工具会好些,一个显著的原因就是它的智能提示啊什么的,当然还有其它的一些。但是,新旧版本的交替需要一个过程。
下面我来show一下自己的实现方法吧。
首先是获取远程服务器上的数据,这个挺简单的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace WriteTest
{
class GetData
{
/// <summary>
/// 获取远程数据,返回一个字符串
/// </summary>
/// <returns></returns>
public string GetUrlData()
{
WebClient client = new WebClient();
string data = client.DownloadString("http://kj.edu24ol.com/update/qa/qa1.txt");
return data;
}
}
}
哦,对了,忘记说了,为了简单,我用的是winfrom实现的。
下面是对数据进行处理的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WriteTest
{
public class Data:IComparable<Data>
{
protected string _first;
protected string _second;
public Data(char[] charData)
{
if (charData.Length==2)
{
this._first = charData[0].ToString();
this._second = charData[1].ToString();
}
else
{
this._first = charData[0].ToString();
}
}
public Data(string first)
{
this._first = first;
this._second = null;
}
/// <summary>返回整个字符串
///
/// </summary>
/// <returns></returns>
private string GetWholeString()
{
return this._first + this._second;
}
/// <summary>获取data是由多少个数组成的。
///
/// </summary>
/// <returns></returns>
private int GetLength()
{
if (this._first!=null && this._second!=null)
{
return 2;
}
else
{
if (this._first==null && this._second==null)
{
return 0;
}
return 1;
}
}
/// <summary>只有两个字符都在特殊字符里,而且不相等,才返回true
///
/// </summary>
/// <param name="one"></param>
/// <param name="two"></param>
/// <returns></returns>
private bool SpecialSort(string one, string two)
{
string containString = "jaGg8rDfUbW";
if (containString.Contains(one) && containString.Contains(two))
{
int thisIndex = containString.IndexOf(one);
int otherIndex = containString.IndexOf(two);
if (thisIndex!=otherIndex)
{
return true;
}
}
return false;
}
/// <summary>比较特殊字符
///
/// </summary>
/// <param name="one"></param>
/// <param name="two"></param>
/// <returns></returns>
private int GetResult(string one, string two)
{
string containString = "jaGg8rDfUbW";
int thisIndex = containString.IndexOf(one);
int otherIndex = containString.IndexOf(two);
return thisIndex - otherIndex;
}
public int CompareTo(Data other)
{
if (this == null)
{
if (other == null)
{
//如果两个都为空,那么返回0
return 0;
}
else
{
//如果other不为空的话,那么other更大
return -1;
}
}
else
{
if (other == null)
// this不为空,other为空,那么this更大
{
return 1;
}
else
{
//都不为空,那么比较对应的属性
//数字或者字母多的,更大
int retval = this.GetLength().CompareTo(other.GetLength());
if (retval != 0)
{
return retval;
}
else
{
if (SpecialSort(this._first,other._first))
{
return GetResult(this._first, other._first);
}
else
{
if (this._first.CompareTo(other._first)!=0)
{
return this._first.CompareTo(other._first);
}
else
{
if (SpecialSort(this._second,other._second))
{
return GetResult(this._second, other._second);
}
return this._second.CompareTo(other._second);
}
}
}
}
}
}
//重写toString方法
public override string ToString()
{
return this._first + this._second;
}
}
}
界面的设计如图所示:
界面下的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
namespace WriteTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//获取远程的字符串
string wholeString = new GetData().GetUrlData();
#region 测试
//Data d1 = new Data("a", "c");
//Data d2 = new Data("d", "e");
//List<Data> list = new List<Data>();
//list.Add(d1);
//list.Add(d2);
//list.Sort();
//foreach (Data d in list)
//{
// txtData.Text = txtData.Text+d.ToString()+"\r\n";
//}
#endregion
//获取每行的字符,放在数组里。
string[] rowString= wholeString.Split(new string[1]{"\r\n"}, StringSplitOptions.None);
foreach (string str in rowString)
{
//txtData.Text = txtData.Text+ str + "\r\n";
//把每行中的两个挨着的字符,放在数组里。
string[] singleString = str.Split(new string[1] { " " }, StringSplitOptions.None);
//实例化Data数组。
Data[] data = new Data[singleString.Length];
int i = 0;
foreach (string strData in singleString)
{
data[i] = new Data(strData.ToCharArray());
i = i + 1;
}
List<Data> list = new List<Data>();
list.AddRange(data);
//排序,调用data里的compareto方法
list.Sort();
foreach (Data val in list)
{
txtData.Text = txtData.Text + val.ToString()+" ";
}
txtData.Text = txtData.Text + "\r\n";
}
}
}
}
提供一个下载吧,如果有兴趣的朋友,可以下载下来看看:
http://download.csdn.net/detail/yjjm1990/5076225
不过,我最后把我做的代码发给HR了,可是没有收到回信,不知道是我写的代码不行还是怎么回事。希望与大家进行交流。