C#练习

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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值