【C#】上机实验六

1. 定义Car类,练习Lambda表达式拍序
(1)Car类中包含两个字段:name和price;
(2)Car类中包含相应的属性、构造函数及ToString方法;
(3)在Main方法中定义Car数组,并实例化该数组;
(4)在Main方法中,按姓名排序输出Car数组所有元素。
(5)在Main方法中,先按姓名后按价格排序输出Car数组所有元素。
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Myproject
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Car[] cars = new Car[4];
14 
15             cars[0] = new Car("B", 80);
16             cars[1] = new Car("A", 90);
17             cars[2] = new Car("C", 70);
18             cars[3] = new Car("b", 80);
19 
20             List<Car> List_Car = new List<Car> { cars[0], cars[1], cars[2] ,cars[3] };
21             Console.WriteLine( "待排序序列 :");
22             foreach (var item in List_Car )
23             {
24                 Console.WriteLine(item);
25             }
26 
27             List_Car.Sort((u, v) =>
28             {
29                int t = u.Price - v.Price;
30                if (t == 0)
31                {
32                    return u.Name.CompareTo(v.Name);
33                }
34                else
35                {
36                    return -t;
37                }
38             });
39 
40             Console.WriteLine("已排序序列 :");
41             foreach (var item in List_Car)
42             {
43                 Console.WriteLine(item);
44             }
45 
46         }
47     }
48     class Car
49     {
50         string name;
51         int price;
52 
53         public string Name { get { return name; } set { name = value; } }
54         public int Price { get { return price; } set { price = value; } }
55 
56         public Car (string name ,int price)
57         {
58             this.name = name;
59             this.price = price; 
60         }
61         public Car() : this("", 0) { }
62 
63         public override string ToString()
64         {
65             return string.Format("{0} : {1}",name ,price );
66         }
67 
68     }
69 }
Lambda表达式排序
2. 将下方给定的字符串根据分隔符拆分成字符串数组,
数组每个元素均为一个单词。将该字符串数组按升序排序,
并输出该字符串数组的内容。
    With Microsoft Azure, you have a gallery of 
open source options. Code in Ruby, Python, Java, PHP, 
and Node.js. Build on Windows, iOS, Linux, and more.
 
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Myproject2
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string str = "With Microsoft Azure, you have a gallery of open source options. Code in Ruby, Python, Java, PHP, and Node.js. Build on Windows, iOS, Linux, and more.";
13             string[] s = str.Split(new char[] { ',', ' ', '.' }, StringSplitOptions.RemoveEmptyEntries);
14             
15             int len = s.Length ;
16             List<string> s_List = new List<string>(s);
17 
18             Console.WriteLine("待排序的字符串");
19             foreach (var item in s_List)
20             {
21                 Console.WriteLine(item);
22             }
23 
24             s_List.Sort((u, v) => {
25                 return u.CompareTo(v);
26             });
27             Console.WriteLine("已排序好的字符串");
28             foreach (var item in s_List)
29             {
30                 Console.WriteLine(item);
31             }
32         }
33     }
34 }
String排序
3、设计一个控制台应用程序,使用泛型类集合List<T>存储一周七天,并实现添加、排序,插入、删除、输出集合元素。具体要求如下:
(1)创建一个空的List<string>,并使用Add方法添加一些元素。
(2)测试List<string>的Count属性和Capacity属性。
(3)使用Contains方法测试元素是否存在。
(4)测试Insert的功能,将元素插入到List<string>中的指定索引处。
(5)利用索引检索List<string>中的元素。
(6)测试Remove的功能,删除List<string>中的元素。
(7)遍历List<string>中的所有元素。
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Myproject3
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //创造一个空的List
13             List<string> list = new List<string> ();
14             
15             //测试Add
16             list.Add("Monday");
17             list.Add("Tuesday");
18             list.Add("Wednesday");
19             list.Add("Thursday");
20             list.Add("Friday");
21             list.Add("Saturday");
22             list.Add("Sunday");
23 
24             //测试Count,Capacity属性 
25             
26             Console.WriteLine("Count : {0} , Capacity :{1}",list.Count ,list.Capacity);
27             Console.WriteLine("\n---------\n");
28 
29 
30             //测试Contain
31             if( list.Contains("Monday") ){
32                 Console.WriteLine("# # Monday # #");
33             }
34             if( !list.Contains("D") ){
35                 Console.WriteLine("Not exists D");
36             }
37             Console.WriteLine("\n---------\n");
38 
39 
40             //测试Insert(Index , "string" )
41             list.Insert(1,"{M} ### {T}");
42             foreach (var item in list)
43             {
44                 Console.Write("{0} ",item);
45             }
46             Console.WriteLine();
47             Console.WriteLine("\n---------\n");
48 
49 
50             //测试list[index]
51             Console.WriteLine("Index {0}: {1}",list.IndexOf(list[4]),list[4]);
52             Console.WriteLine("\n---------\n");
53 
54 
55             //测试Remove
56             list.Remove("{M} ### {T}");
57 
58 
59             //遍历所有元素
60             foreach (var item in list)
61             {
62                 Console.Write("{0} ", item);
63             }
64             Console.WriteLine();
65             Console.WriteLine("\n---------\n");
66 
67         }
68     }
69 }
集合List
4、设计一个控制台应用程序,模拟管理车牌相关信息,
例如姓名和车牌号码,能够添加、修改、查找、删除、输出车牌信息。
(使用Dictionary<>类完成)
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace Myproject
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             Dictionary<string, string> dict = new Dictionary<string, string>();
 14 
 15             while (true)
 16             {
 17                 Show_Mean();
 18                 int opt = int.Parse(Console.ReadLine());
 19                 switch (opt)
 20                 {
 21                     case 1: Add(dict); break;
 22                     case 2: Modify(dict); break;
 23                     case 3: Find(dict); break;
 24                     case 4: Delete(dict); break;
 25                     case 5: Show_Car(dict); break;
 26                     default: break;
 27                 }
 28                 Console.WriteLine("---请按回车键继续---");
 29                 string t = Console.ReadLine();
 30                 Console.Clear();
 31             }
 32         }
 33         
 34         public static void Add(Dictionary<string, string> dict)
 35         {
 36             Console.WriteLine("请输入车牌号");
 37             string key = Console.ReadLine();
 38 
 39             Console.WriteLine("请输入车主姓名");
 40             string value = Console.ReadLine();
 41 
 42             dict.Add(key, value);
 43             Console.WriteLine("Succsee add :({0} , {1})",key,value);
 44         }
 45         public static void Modify(Dictionary<string, string> dict)
 46         {
 47             Console.WriteLine("请输入车牌号码");
 48             string key = Console.ReadLine();
 49             
 50             if( dict.ContainsKey(key) )
 51             {
 52                 Console.WriteLine("请输入车主信息");
 53                 string value = Console.ReadLine();
 54                 dict[key] = value;
 55                 Console.WriteLine("Succsee Modify :({0} , {1})", key, value);
 56             }
 57             else
 58             {
 59                 Console.WriteLine("无法查询到对应的车牌号.");
 60             }
 61             
 62         }
 63         public static void Find(Dictionary<string, string> dict)
 64         {
 65             Console.WriteLine("请输入车牌号");
 66             string key = Console.ReadLine();
 67             if( dict.ContainsKey(key))
 68             {
 69                 Console.WriteLine(dict[key]);
 70             }
 71             else
 72             {
 73                 Console.WriteLine("无法查询到对应的车牌号.");
 74             }
 75         }
 76         public static void Delete(Dictionary<string, string> dict)
 77         {
 78             Console.WriteLine("请输入车牌号");
 79             string key = Console.ReadLine();
 80             if (dict.ContainsKey(key))
 81             {
 82                 dict.Remove(key);
 83                 Console.WriteLine("Key {0} Delete",key);
 84             }
 85             else
 86             {
 87                 Console.WriteLine("无法查询到对应的车牌号.");
 88             }
 89         }
 90         public static void Show_Car(Dictionary<string, string> dict)
 91         {
 92             foreach (KeyValuePair<string,string>item in dict)
 93             {
 94                 Console.WriteLine("车牌号 :{0} , 车主信息 :{1}",item.Key,item.Value);
 95             }
 96         }
 97         public static void Show_Mean()
 98         {
 99             //Console.Clear();
100             Console.WriteLine("##### 车牌管理系统 #####");
101             Console.WriteLine("1.添加车牌号");
102             Console.WriteLine("2.修改车牌号");
103             Console.WriteLine("3.查找车牌号");
104             Console.WriteLine("4.删除车牌号");
105             Console.WriteLine("5.输出车牌信息");
106 
107         }
108     }
109     
110 }
Dictionary-汽车

猜你喜欢

转载自www.cnblogs.com/Osea/p/11765493.html
今日推荐