黑马程序员-习题练习3

时间:2022-03-05 21:43:53
 //从文件路径中提取出文件名(包含后缀) 。比如从c:\a\b.txt中提取出b.txt这个文件名出来。
        //以后还会学更简单的方式:“正则表达式”,项目中我们用微软提供的:Path.GetFileName();(更简单。)
        static void Main(string[] args)
        {
            string str = "c:\a\b.txt";
            string[] s = str.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            string name = s[s.Length - 1];
            Console.WriteLine("文件名为:"+name );
            Console.ReadKey();


        }


------------------------------------------------------------------------------------------------------------------------------------------------------

//“192.168.10.5[port=21,type=ftp]”,这个字符串表示IP地址为192.168.10.5的
        //服务器的21端口提供的是ftp服务,其中如果“,type=ftp”部分被省略,则默认为http服务。
        //请用程序解析此字符串,然后打印出“IP地址为***的服务器的***端口提供的服务为***” 
        //line.Contains(“type=”)192.168.10.5[port=21]
        static void Main(string[] args)
        {
            string strIp = "192.168.10.5[port=21,type=ftp]"; 
            int Oper2 = strIp.LastIndexOf('['); 
            int Operlast = strIp.LastIndexOf(']'); 
            string Ipstr = strIp.Substring(0, Oper2); 
            string arrstr = strIp.Substring(Oper2 + 1, Operlast - Oper2 - 1); 
            string[] a = arrstr.Split(','); 
            for (int i = 0; i < a.Length; i++) 
            { 
                a[i] = a[i].Substring(a[i].IndexOf("=") + 1); 
            } 
            if (arrstr.Contains("type")) 
            { 
                Console.WriteLine("IP地址为{0}的服务器的{1}端口提供的服务为{2}", Ipstr, a[0], a[1]); 
            } 
            else 
            { 
                Console.WriteLine("IP地址为{0}的服务器的{1}端口提供的服务为{2}", Ipstr, a[0], "http"); 
            }            
            //string s = "port=21,type=ftp";            
            //string[] a = s.Split(',');            
            //for (int i = 0; i < a.Length; i++)            
            //{            
            //    a[i] = a[i].Substring(a[i].IndexOf("=")+1);            
            //}            
            //Console.WriteLine(a[0]);            
            //Console.WriteLine(a[1]);           


            Console.ReadKey();
        }


----------------------------------------------------------------------------------------------------------------------------------------------

//随机初始化员工工资,求员工工资文件中,员工的最高工资、最低工资、平均工资
        static void Main(string[] args)
        {
            string[] strFile = File.ReadAllLines(@"d:\My Documents\visual studio 2010\Projects\试题练习\No.28\工资.txt", Encoding.Default);
            int[] intarr = new int[strFile.Length];
            for(int i = 0; i < strFile.Length; i++) 
            {                
                if(!string .IsNullOrEmpty(strFile[i]))//如果文档内容中有空格 数组任然会存取为0的值 所以数组不能解决空格产生的0问题  只能使用到集合                
                {                    
                    string[] strs2 = strFile[i].Split(':' );                    
                    intarr[i] = Convert.ToInt32(strs2[1]);                
                }            
            }            
            int max = intarr[0];            
            int min = intarr[0];            
            for(int i = 1; i < intarr.Length; i++) 
            {                
                if(max < intarr[i]) 
                {                    
                    max = intarr[i];                
                }                
                if(min > intarr[i]) 
                {                    
                    min = intarr[i];                
                }            
            }            
            Console.WriteLine("最高工资:{0},最低工资:{1}" , max, min);           
            Console.ReadKey();


        }

黑马程序员-习题练习3


-----------------------------------------------------------------------------------------------------------------------------------------------

 //两个(ArrayList)集合{ “a”,“b”,“c”,“d”,“e”}和
        //{ “d”, “e”, “f”, “g”, “h” },把这两个集合去除重复项合并成一个。
        static void Main(string[] args)
        {
            ArrayList arrayList1 = new ArrayList() { "a", "b", "c", "d" };
            ArrayList arrayList2 = new ArrayList() { "c", "f", "d", "h" };
            ArrayList arrayList3 = new ArrayList();
            
            arrayList3.AddRange(arrayList1);
            for (int i = 0; i < arrayList2.Count ; i++)
            {
                
                    if (!arrayList3.Contains(arrayList2[i]))
                    {
                        arrayList3.Add(arrayList2[i]);
                    }
             }
            for (int i = 0; i < arrayList3.Count; i++)
            {
                    Console.WriteLine(arrayList3[i]);
             }
            Console.ReadKey();
         }


---------------------------------------------------------------------------------------------------------------------------------------------

//随机生成10个1-100之间的数放到ArrayList中,要求这10个数不能重复,
        //并且都是偶数(添加10次,可能循环很多次。
        static void Main(string[] args)
        {
            Random random = new Random();
            ArrayList arrayRandom = new ArrayList();
            while (arrayRandom.Count < 10)
            {
                int ra = random.Next(1, 101);
                if (ra % 2 == 0)
                {
                    if (!arrayRandom.Contains(ra))
                    {
                        arrayRandom.Add(ra);
                    }
                }
            }
            Console.ReadKey();


        }


--------------------------------------------------------------------------------------------------------------------------------------------

//有一个字符串是用空格分隔的一系列整数,写一个程序把其中的整数做如下重新排列打印出来:
        //奇数显示在左侧、偶数显示在右侧。比如”2 7 8 3 22 9 5 11”显示成”7 3 9 2 8 22….”。
        static void Main(string[] args)
        {
            string msg = "2 7 9 3 6 5 8 4";
            string[] nums = msg.Split(new char []{' '}, StringSplitOptions.RemoveEmptyEntries );
            //7 9 3 5 2 6 8 4


            //存放奇数
            ArrayList listOdd = new ArrayList();
            //存放偶数
            ArrayList listEven = new ArrayList();
            for (int i = 0; i < nums.Length; i++)
            {
                if (Convert.ToInt32(nums[i]) % 2 != 0)
                {
                    listOdd.Add(nums[i]);
                }
                else
                {
                    listEven.Add(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();
        }


------------------------------------------------------------------------------------------------------------------------------------------------

//将int数组中的奇数放到一个新的int数组中返回
        static void Main(string[] args)
        {
            int[] numbers = { 2, 7, 9, 4, 3, 8, 1, 6 };
            List <int >listOld = new List<int >();
            for (int i = 0; i < numbers.Length; i++)
            {
                if (numbers[i] % 2 != 0)
                {
                    listOld.Add(numbers[i]);
                }
            }
            int[] allNum = listOld.ToArray();
            for (int i = 0; i < allNum.Length; i++)
            {
                Console.WriteLine(allNum[i]);
            }
            Console.ReadKey();
        }


---------------------------------------------------------------------------------------------------------------------------------------------

 //从一个整数的List<int>中取出最大数(找最大值)。别用max方法
        static void Main(string[] args)
        {
            List <int >listMax = new List<int >() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int temp = listMax[0];
            for (int i = 1; i < listMax.Count; i++)
            {
                if (temp < listMax[i])
                {
                    temp = listMax[i];
                }
            }
            Console.WriteLine("Max is:" + temp);
            Console.ReadKey();
        }


------------------------------------------------------------------------------------------------------------------------------------------------

 //把123转换为:壹贰叁。Dictionary<char,char>
        static void Main(string[] args)
        {
            Dictionary<char, char> dir = new Dictionary<char, char>(); 
            dir.Add('1', '壹'); 
            dir.Add('2', '贰'); 
            dir.Add('3', '叁'); 
            string str = "123"; 
            StringBuilder sb = new StringBuilder(); 
            for (int i = 0; i < str.Length ; i++) 
            { 
                char temp = str[i];
                Console.WriteLine(dir[temp]);
                sb.AppendFormat("{0}", dir[temp]);
                
            } 
            Console.WriteLine(sb); 
            Console.ReadKey(); 
        }


----------------------------------------------------------------------------------------------------------------------------------------------

 //计算字符串中每种字符出现的次数(面试题)。 “Welcome to Chinaworld”,
        //不区分大小写,打印“W2”“e 2”“o 3”……
        //提示:Dictionary<char,int>,char的很多静态方法。char.IsLetter()
        static void Main(string[] args)
        {
            string strtemp = "Welcome to Chinaworld"; 
            string str = strtemp.ToLower(); 
            Dictionary<char, int> dir = new Dictionary<char, int>(); 
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsLetter(str[i])) 
                { 
                    if (dir.ContainsKey(str[i])) 
                    { 
                        dir[str[i]]++; 
                    } 
                    else 
                    { 
                        dir.Add(str[i], 1); 
                    } 
                }
            } 
            StringBuilder sb = new StringBuilder(); 
            foreach (KeyValuePair<char, int> item in dir) 
            { 
                sb.AppendFormat("{0}\t{1}\n", item.Key, item.Value); 
            } 
            Console.WriteLine(sb); 
            Console.ReadKey(); 


        }


---------------------------------------------------------------------------------------------------------------------------------------------

 //编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:
        //二零一二年十二月月二十一日要转换为2012-12-2 1。(处理“十”的问题:
        //1.*月十日;2.*月十三日;3.*月二十三日;4.*月三十日。4种情况对“十”的不同翻译。
        //1→10;2→1;3→不翻译;4→0【年部分不可能出现’十’,都出现在了月与日部分。】
        //测试数据:二零一二年十二月二十一日、二零零九年七月九日、二零一零年十月二十四日、
        //二零一零年十月二十日
        static void Main(string[] args)
        {
            string date = "二零一二年二月二一日";
            string strNumb = "一1 二2 三3 四4 五5 六6 七7 八8 九9 零0";
            string[] strNumbs = strNumb.Split(' ');
            string nullYear = "";
            Dictionary<char, char> years = new Dictionary<char, char>(); 
            for (int i = 0; i < strNumbs.Length; i++) 
            {
                years.Add(strNumbs[i][0], strNumbs[i][1]); 
            }
            //years.Add('年', '-');
            //years.Add('月', '-');
            for (int i = 0; i < date.Length; i++) 
            { 
                if (years.ContainsKey(date[i])) 
                {
                    nullYear += years[date[i]];
                } 
                else if (date[i] == '年' || date[i] == '月' ) 
                {
                    nullYear += '-';
                } 
                else if (date[i] == '十' && years.ContainsKey(date[i + 1]) && !years.ContainsKey(date[i - 1])) 
                {
                    nullYear += '1'; 
                } 
                else if (date[i] == '十' && !years.ContainsKey(date[i + 1]) && years.ContainsKey(date[i - 1])) 
                {
                    nullYear += '0'; 
                } 
                else if (date[i] == '十' && !years.ContainsKey(date[i + 1]) && !years.ContainsKey(date[i - 1])) 
                {
                    nullYear += "10"; 
                } 
            }
            Console.WriteLine(nullYear);
            Console.ReadKey(); 




        }


---------------------------------------------------------------------------------------------------------------------------------------------//有500个老太太玩游戏,从1开始数,1,2,3,数到3的老太太退出,下一个还是1,
        //剩下的老太太只要到3就退出,然后从1 开始,最后剩下几个,分别是原500人当中的第几个


        static void Main(string[] args)
        {
            //int[] renshu=new int[500];
            ArrayList renshu = new ArrayList(500);
            ArrayList result = new ArrayList ();
            //int a=0;
            //int b = 0;
            for (int i = 1; i <= 500 ; i++)
            {
                renshu.Add(i);
            }
            result = renshu;
            do
            {
                //int a = 0;
                
                //while (!(a==renshu.Count) )
                for (int a = 0; a < renshu.Count;a++ )
                {
                    //if (!((a+1) % 3 == 0))
                    //{
                    //    result.Add(renshu[a]);
                    //}
                    if ((a + 1) % 3 == 0)
                    {
                        //result.RemoveAt(a);
                        result[a] = " ";
                    }
                    //a++;


                }
                //int b = 0;
                
                while (result.Contains (" ") )
                {
                    result.Remove(" ");


                }
                    if (result.Count == 3)
                    {
                        break;
                    }
                renshu = result;
                //result.Clear();
                


                
            } while (true );
            
            Console.WriteLine(result[2].ToString() );
            Console.ReadKey();
        }