using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication46
{
class Program
{
static void Main(string[] args)
{
//*****************字符反转***************
string[] arrayReverse = { "A", "B", "C", "D" };
Reverse<string>(arrayReverse);
//*****************字符反转***************
//*****************返回指定元素索引***************
int[] arrayFind = { 1, 3, 5, 8, 10 };
int selectVal = 5;
int index = Find<int>(arrayFind, selectVal);
Console.WriteLine(index);
//*****************返回指定元素索引***************
//*****************返回最大的元素及索引*****************
int[] arrayInt = { 1, 3, 5, 8, 10 };
int indexFindMax;
int max = indexFindMax<int>(arrayInt, out indexFindMax);
Console.WriteLine(indexFindMax+"--"+max);
//*****************返回最大的元素及索引*****************
//*****************返回出现最多字符及数量*****************
string str = "i am amandag!";
Dictionary<char, int> dd = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
if (dd.ContainsKey(str[i]))
{
dd[str[i]]++;
}
else
{
dd[str[i]]=1;
}
}
char maxDD = str[0];
foreach(KeyValuePair<char,int> key in dd)
{
if (key.Value > dd[maxDD])
{
maxDD = key.Key;
}
}
Console.WriteLine(maxDD+"==="+dd[maxDD]);
//*****************返回出现最多字符及数量*****************
//*****************排序txt*****************
string strTxt = "1.txt,4.txt,2.txt,3.txt";
string[] arrayTxt = strTxt.Split(',');
Dictionary<int, TXT> DD = new Dictionary<int, TXT>();
for (int j = 0; j < arrayTxt.Length; j++)
{
TXT txt;
if (TXT.IsTxt(arrayTxt[j], out txt))
{
DD[txt.Id] = txt;
}
}
System.Text.StringBuilder sb = new StringBuilder();
foreach(KeyValuePair<int,TXT> key in DD.OrderBy(x=>x.Key))
{
sb.Append(key.Value + ",");
}
Console.WriteLine(sb.ToString().TrimEnd(','));
//*****************排序txt*****************
}
/// <summary>
/// 字符反转
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
static void Reverse<T>(T[] array)
{
if (array == null || array.Length == 0)
throw new ArgumentException("参数错误");
T temp = default(T);
int beg = 0;
int end = array.Length - 1;
while (beg < end)
{
temp = array[beg];
array[beg] = array[end];
array[end] = temp;
beg++;
end--;
}
foreach (T t in array) { Console.WriteLine(t); }
}
/// <summary>
/// 返回指定元素索引
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="selectVal"></param>
/// <returns></returns>
static int Find<T>(T[] array, T selectVal)
{
if (array == null || array.Length == 0)
throw new ArgumentException("参数错误");
int index=-1;
for (int i = 0; i < array.Length; i++)
{
if (array[i].Equals(selectVal))
index = i;
}
return index;
}
/// <summary>
/// 返回最大的元素及索引
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <param name="index"></param>
/// <returns></returns>
static T indexFindMax<T>(T[] array,out int index)
where T : IComparable
{
if (array == null || array.Length == 0)
throw new ArgumentException("参数错误");
T max = array[0];
index = -1;
for (int i = 0; i < array.Length; i++)
{
if (array[i].CompareTo(max)>0)
{
index = i;
max=array[i];
}
}
return max;
}
}
class TXT
{
public int Id;
public string Name;
public TXT(int id,string name)
{
this.Id = id;
this.Name = name;
}
public static bool IsTxt(string str,out TXT txt)
{
txt = null;
int index;
string[] array = str.Split('.');
if (array.Length != 2) return false;
if (!int.TryParse(array[0], out index)) return false;
if (string.Compare(array[1], "txt") != 0) return false;
txt = new TXT(index,array[1]);
return true;
}
public override string ToString()
{
return this.Id+"."+this.Name;
}
}
}