C#第五次上机

来源:互联网 发布:mysql高级编程 编辑:程序博客网 时间:2024/06/04 19:27

 1.引入命名空间System.Collections,使用.net提供的泛型类Stack<T>,实现字符串或数字的反序


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace app{    class Program    {        static void Main(string[] args)        {            Stack<char> stack = new Stack<char>();            string str = Console.ReadLine();            char[] SS = str.ToCharArray();            for (int i = 0; i < SS.Length; i++)            {                stack.Push(SS[i]);            }            for (int i = 0; i < SS.Length; i++)//利用栈的先进后出特性实现其功能            {                Console.Write(stack.Pop());            }            Console.ReadKey();        }    }}


2.引入命名空间System.Collections,使用.net提供的泛型类Queue<T>,实现任意数据的入队,出队操作。并将出队的数据求和


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace app{    class Program    {        static void Main(string[] args)        {            Queue<string> queue = new Queue<string>();            string str;            while (true)            {                str = Console.ReadLine();                if (str.Length == 0)                {                    break;                }                queue.Enqueue(str);            }            string SS = "";            while (queue.Count > 0)            {                SS += queue.Dequeue();            }            Console.WriteLine(SS);            Console.ReadKey();        }    }}


3.创建一个控制台应用,输入下面的代码,编译执行,观察结果。仿照此例,编写一个使用匿名方法的委托,匿名方法实现计算整数形数组各元素之和的功能。


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace APP{    delegate void MyDelegate(int[] num);    class Program    {        static void Main(string[] args)        {            MyDelegate sum = delegate(int[] num)                {                    int count = 0;                    for (int i = 0; i < num.Length; i++)                    {                        count += num[i];                    }                    Console.WriteLine(count);                };            int[] Num = new int[10];            string[] str = Console.ReadLine().Split(' ');            for (int j = 0; j < str.Length; j++)            {                Num[j] = int.Parse(str[j]);            }                sum(Num);                Console.ReadKey();        }    }}


4.创建静态类,在其中定义一个泛型方法,实现查找数组元素的功能


using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace APP{    static class MyClass    {        public static int Method<T>(int m, T n) where T : ArrayList        {            return (n.IndexOf(m)+1);        }    }    class Program    {        static void Main(string[] args)        {            ArrayList arr= new ArrayList();            string[] str = Console.ReadLine().Split(' ');            int[] num = new int[10];            for (int i = 0; i < str.Length; i++)            {                num[i] = int.Parse(str[i]);            }            arr.AddRange(num);            string ss = Console.ReadLine();            int n = int.Parse(ss);            Console.WriteLine("该元素在第{0}个位置!", MyClass.Method<ArrayList>(n, arr));            Console.ReadKey();        }    }}


5.观察下面代码中的Lambda表达式的用法,编写一个类似控制台应用,由控制台输入两个整数,输出较大的一个


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace APP{    delegate int MyDelegate(int x,int y);    class Program    {        static void Main(string[] args)        {           MyDelegate d=(int a,int b)=>Math.Max(a,b);           string[] str = Console.ReadLine().Split(' ');           int x = int.Parse(str[0]);           int y = int.Parse(str[1]);           Console.WriteLine(d(x, y));           Console.ReadKey(false);        }    }   }


0 0
原创粉丝点击