偶们学的一些小程序。。。求围观。

来源:互联网 发布:索尼z2tablet 淘宝 编辑:程序博客网 时间:2024/06/05 10:46

1.把分拣奇数偶数的程序用泛型实现。(List<int>)
2.将int数组中的奇数放到一个新的int数组中返回。
int[] numbers={2,7,9,4,3,8,1,6}
List<int> listOdd=new List<int>();

for(int i=0;i<numbers.length;i++)
{
if (numbers[i]%2!=0)
listOdd.Add(numbers[i]);

}
int[] numOdd=listOdd.ToArray();


3.从整数的List<int>中找最大值,不适用Max方法。

List<int> list=new List<int>(1,2,3,4,5,6,7,8,9);

//Random radom=new Random();
//radom.next()

//list.Add

int temp=0

for(int i=0;i<list.Count;i++)
{
    if(list[i]>temp)
{

temp=list[i];
}

}
console.writeline("max is :"+temp);

4.计算字符串中每种字符出现的次数。(面试题)“welcome to china! this is a beautiful county, i think you will like it.here is The great wall”提示:Dictionary<char,int>


5.把1,2,3转换为壹 贰 叁
string str="1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";


6.简体繁体字转换。

7.编写函数进行日期转换;键输入的大写的日期转换成阿拉伯数字的形式。
二零一二年三月十三日     2012-3-13
二零一二年三月十日       2012-3-10
二零一二年三月二十三日   2012-3-23

代码如下。。。。。。。。。。。。。。。

1.把分拣奇数偶数的程序用泛型实现。(List<int>)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 分拣奇偶
{
    class Program
    {
        static void Main(string[] args)
        {
            string num = "2 3 4 5 6 7 8 9";
            string[] nums = num.Split();

            List<int > listOdd = new List<int >();
            List<int> listEven = new List<int >();
            for (int i = 0; i < nums.Length; i++)
            {
                if (Convert.ToInt32(nums[i]) % 2 != 0)
                {
                    listOdd.Add(Convert.ToInt32( nums[i]));
                }
                else
                {
                    listEven.Add(Convert.ToInt32(nums[i]));
                }
            }
            listOdd.AddRange(listEven);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < listOdd.Count; i++)
            {
                sb.Append(listOdd[i] + " ");
            }
            Console.WriteLine(sb);
            Console.ReadKey();
        }
    }
}


2.将int数组中的奇数放到一个新的int数组中返回。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 奇数放int中
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            List<int> listOdd=new List<int>();

            for(int i=0;i<numbers.Length;i++)

            {

                 if(numbers[i]%2!=0)

                 listOdd.Add(numbers[i]);
  
            }

            int[] numOdd = listOdd.ToArray();

            foreach (int tenp in numOdd)
            {
                Console.WriteLine(tenp);
            }

            Console.ReadKey();
        }
    }
}

3.从整数的List<int>中找最大值,不适用Max方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 从整数中找最大
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            int temp = 0;

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] > temp)
                {
                    temp = list[i];
                }
            }
            Console.WriteLine("max is:" + temp);
        }
    }
}


4.计算字符串中每种字符出现的次数。(面试题)“welcome to china! this is a beautiful county, i think you will like it.here is The great wall”提示:Dictionary<char,int>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 计算字符出现的次数
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Welcome to china! this is a beautiful county, i think you will like it.here is The great wall";

            Console.WriteLine("在"+str+"中");
            str = str.ToLower();
          
            Dictionary<char, int> dict2 = new Dictionary<char, int>();
           
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsLetter(str[i]))
                {
                    if (dict2.ContainsKey(str[i]))
                    {
                        dict2[str[i]]++;
                    }
                    else
                    {
                        dict2.Add(str[i], 1);
                    }
                }
            }

            foreach (KeyValuePair<char, int> kv in dict2)
            {

                Console.WriteLine("字符:{0},出现了{1}次",kv.Key,kv.Value);
            }

                Console.ReadKey();
        }
    }
    public class Person
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}

5.把1,2,3转换为壹 贰 叁    string str="1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 变简为繁
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一个数:") ;
            string number = Console.ReadLine();
            string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";

            Dictionary<char, char> dict = new Dictionary<char, char>();
            string[] parts = str.Split(' ');

            for (int i = 0; i < parts.Length; i++)
            {
                dict.Add(parts[i][0], parts[i][1]);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < number.Length; i++)
            {
                sb.Append(dict[number[i]]);
            }
            Console.WriteLine(sb);
            Console.ReadKey();
           

        }
    }
}

6.简体繁体字转换。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace 变简体为繁体
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Dictionary<char, char> dict = new Dictionary<char, char>();
        private void btnConvert_Click(object sender, EventArgs e)
        {
            //获取用户输入的简体中文
            string input = txtCHS.Text.Trim();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                //判断字典中是否包含这个键
                if (dict.ContainsKey(input[i]))
                {
                    sb.Append(dict[input[i]]);
                }
                else
                {
                    //如果dict中不包含,原样输出
                    sb.Append(input[i]);
                }

            }

            txtCHT.Text = sb.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines("ST.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                dict.Add(lines[i][0], lines[i][2]);
                //lines[0] ---"啊=啊"
            }
        }

        private void Form1_Layout(object sender, LayoutEventArgs e)
        {

        }
    }
}

7.编写函数进行日期转换;键输入的大写的日期转换成阿拉伯数字的形式。
二零一二年三月十三日     2012-3-13
二零一二年三月十日       2012-3-10
二零一二年三月二十三日   2012-3-23


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 日期转换
{
    class Program
    {
        static void Main(string[] args)
        {
           
            Console.WriteLine("请输入大写日期:");
            string inputCdate = Console.ReadLine();
            Console.WriteLine(DateConvert(inputCdate));

            Console.ReadKey();
        }


        private static string DateConvert(string date)
        {
            Dictionary<char, char> dict = new Dictionary<char, char>();
            string dictionary = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0";
            string[] parts = dictionary.Split(' ');
           
            //parts[0]="一1"
            for (int i = 0; i < parts.Length; i++)
            {
                dict.Add(parts[i][0], parts[i][1]);
            }

             

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < date.Length; i++)
            {
               
                if (dict.ContainsKey(date[i]))
                {
                    sb.Append(dict[date[i]]);
                }
                else if (!dict.ContainsKey(date[i]) && date[i] != '十')
                {
                    sb.Append('-');
                }
                else
                {
                    if(!dict.ContainsKey(date[i-1])&&!dict.ContainsKey(date[i+1]))
                    {
                        sb.Append("10");
                    }
                   else  if (!dict.ContainsKey(date[i - 1]) && dict.ContainsKey(date[i + 1]))
                    {
                        sb.Append('1');
                    }
                   else if (dict.ContainsKey(date[i - 1]) && dict.ContainsKey(date[i + 1]))
                    { }
                    else if (dict.ContainsKey(date[i - 1]) && !dict.ContainsKey(date[i + 1]))
                    {
                        sb.Append('0');
                    }
                }
            }
            return (sb.ToString().TrimEnd('-'));
        
        }
    }
}

原创粉丝点击