码迷,mamicode.com
首页 > 其他好文 > 详细

基础加强第三天 练习总结

时间:2015-03-22 06:56:51      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

5、数字转大写案例
#region 练习:把123转换为:壹贰叁。Dictionary<char,char>
            //提示用户输入
            Console.WriteLine("请输入一个数");
            //接受用户的输入
            string number = Console.ReadLine();
            string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";
            Dictionary<int, char> dic = new Dictionary<int, char>();
            //去掉字符串中的空格
            string[] parts = str.Split(new char[]{‘ ‘},StringSplitOptions.RemoveEmptyEntries);
            //加入键值对集合中
            for (int i = 0; i < parts.Length; i++)
            {
                dic.Add(parts[i][0],parts[i][1]);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < number.Length; i++)
            {
                sb.Append(dic[number[i]]);
            }
            Console.WriteLine(sb.ToString());
            Console.ReadKey();
            #endregion
#region  第二种方法
            string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";
            Dictionary<char, char> dic = new Dictionary<char, char>();
            //去除空格
            string[] number = str.Split(new []{‘ ‘},StringSplitOptions.RemoveEmptyEntries);
            //将数组中的这十项都拿出来
            for (int i = 0; i < number.Length; i++)
            {
                //将这十项添加到键值对字典中
                //因为键不能重复,所以添加判断
                if (!dic.ContainsKey(number[i][0]))
                {
                    dic.Add(number[i][0],number[i][1]);
                }
            }
            //提示用户输入
            Console.WriteLine("请输入数字");
            string txt=Console.ReadLine();
            for (int i = 0; i < txt.Length; i++)
            {
                //判断当前字典中是否包含用户输入的数字
                if (dic.ContainsKey(txt[i]))
                {
                    //如果有这个值,那么久替换成大写    dic[txt[i]]这个代表当前这个键的值,打印输出
                    Console.Write(dic[txt[i]]);
                }
                else
                {
                    //否则原样输出
                    Console.Write(txt[i]);
                }
            }
            Console.ReadKey();
            #endregion

#region 练习四 计算字符串中每种字母出现的次数(面试题)。 “Welcome ,to Chinaworld”,不区分大小写,打印“W2”“e 2”“o 3”……
            string str = "Welcome ,to Chinaworld";
            str = str.ToLower();
            Dictionary<char, int> dic = new Dictionary<char, int>();
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsLetter(str[i]))
                {
                    if (!dic.ContainsKey(str[i]))   //如果字典中不包含这个字符
                    {
                        //则添加这个字符,并且该字符出现的次数是1次
                        dic.Add(str[i], 1);
                    }
                    else
                    {
                        //键对应的值次数加1
                        dic[str[i]]++;
                    }
                }
            }
            foreach (KeyValuePair<char,int> item in dic)
            {
                Console.WriteLine("字母{0}出现{1}次",item.Key,item.Value);
            }
            Console.ReadKey();
            #endregion


6、文件操作
            //写入文件
            //using (FileStream fs=new FileStream("5.txt",FileMode.OpenOrCreate,FileAccess.Write))
            //{
            //    string str = "今天天气好晴朗";
            //    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
            //    fs.Write(buffer,0,buffer.Length);
            //}
            //Console.WriteLine("OK");
            //Console.ReadKey();
            //读取文件
            using (FileStream fs=new FileStream("5.txt",FileMode.OpenOrCreate,FileAccess.Read))
            {
                byte[] buffer=new byte[fs.Length];
                fs.Read(buffer,0,buffer.Length);
                string msg = System.Text.Encoding.UTF8.GetString(buffer);
                Console.WriteLine(msg);
            }
            Console.ReadKey();

 

7、工资翻倍
 #region 练习3:通过StreamReader和StreamWrite对工资文件进行操作
            //将文件中的工资翻倍再写入到文件中
            using (StreamReader sr=new StreamReader("salary.txt",Encoding.Default))
            {
                using (StreamWriter sw=new StreamWriter("a.txt",false,Encoding.Default))
                {
                    string line;
                    while ((line=sr.ReadLine())!=null)
                    {
                        string[] msg = line.Split(new char[]{‘|‘},StringSplitOptions.RemoveEmptyEntries);
                        string str = string.Format("{0}|{1}",msg[0],Convert.ToInt32(msg[1])*2);
                        sw.Write(str);
                    }
                }
            }
            Console.WriteLine("ok");
            Console.ReadKey();
            #endregion

8、Path类
            //1.
            //string lujing = @"c:\1.txt";
            ////主要是更该后缀名
            //string msg = Path.ChangeExtension(lujing,".rar");
            //Console.WriteLine(msg);
            //Console.ReadKey();

            //2.合并路径
            //string str1 = @"C:\Program Files (x86)\";
            //string str2 = @"Microsoft SQL Server\100\SDK\Assemblies";
            //string msg = Path.Combine(str1,str2);
            //Console.WriteLine(msg);
            //Console.ReadKey();

            //3.查找某个文件所在的路径
            string str = @"C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies";
            string msg = Path.GetDirectoryName(str);
            Console.WriteLine(str);
            Console.ReadKey();

            //Path.GetExtension();//返回扩展名
            //Path.GetFileName();//获得文件名
            //Path.GetFullPath();//返回指定字符串的绝对路径

基础加强第三天 练习总结

标签:

原文地址:http://blog.csdn.net/qizhichao110/article/details/44531837

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!