. 定义Car类,练习Lambda表达式拍序
()Car类中包含两个字段:name和price;
()Car类中包含相应的属性、构造函数及ToString方法;
()在Main方法中定义Car数组,并实例化该数组;
()在Main方法中,按姓名排序输出Car数组所有元素。
()在Main方法中,先按姓名后按价格排序输出Car数组所有元素。
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Myproject
{
class Program
{
static void Main(string[] args)
{
Car[] cars = new Car[]; cars[] = new Car("B", );
cars[] = new Car("A", );
cars[] = new Car("C", );
cars[] = new Car("b", ); List<Car> List_Car = new List<Car> { cars[], cars[], cars[] ,cars[] };
Console.WriteLine( "待排序序列 :");
foreach (var item in List_Car )
{
Console.WriteLine(item);
} List_Car.Sort((u, v) =>
{
int t = u.Price - v.Price;
if (t == )
{
return u.Name.CompareTo(v.Name);
}
else
{
return -t;
}
}); Console.WriteLine("已排序序列 :");
foreach (var item in List_Car)
{
Console.WriteLine(item);
} }
}
class Car
{
string name;
int price; public string Name { get { return name; } set { name = value; } }
public int Price { get { return price; } set { price = value; } } public Car (string name ,int price)
{
this.name = name;
this.price = price;
}
public Car() : this("", ) { } public override string ToString()
{
return string.Format("{0} : {1}",name ,price );
} }
}

Lambda表达式排序

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CarCode
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!"); Car[] car = new Car[];
car[] = new Car("A", );
car[] = new Car("S", );
car[] = new Car("B", );
car[] = new Car("c", );
car[] = new Car("b", ); Console.WriteLine("++++++++++++++");
foreach (var item in car)
{
Console.WriteLine(item);
} Console.WriteLine("++++++++++++++");
car = car.OrderBy(rhs => rhs.Name).ToArray<Car>();
foreach (var item in car)
{
Console.WriteLine(item);
} Console.WriteLine("++++++++++++++");
car = car.OrderBy(rhs => rhs.Price).ThenBy(rhs => rhs.Name).ToArray<Car>();
foreach (var item in car)
{
Console.WriteLine(item);
} Console.WriteLine("++++++++++++++");
Array.Sort(car, , );
foreach (var item in car)
{
Console.WriteLine(item);
}
}
}
public class Car : IComparable
{
string name;
int price; public string Name { get => name; set => name = value; }
public int Price { get { return price; } set { price = value; } } public Car(){
name = "";
price = ;
} public Car(string N, int P){
name = N;
price = P;
} public override string ToString()
{
return string.Format("{0} : {1}", name, price);
} public int CompareTo( object obj)
{
int r = ;
if( obj == null)
{
return r;
} Car rhs = obj as Car;
if (rhs == null)
{
throw new ArgumentException("Object is not a Car");
}
else
{
r = this.price.CompareTo(rhs.price);
if (r == )
{
return this.name.CompareTo(rhs.name);
}
else
{
return r;
}
}
}
}
}

Lambda表达式

. 将下方给定的字符串根据分隔符拆分成字符串数组,
数组每个元素均为一个单词。将该字符串数组按升序排序,
并输出该字符串数组的内容。
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.
 
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Myproject2
{
class Program
{
static void Main(string[] args)
{
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.";
string[] s = str.Split(new char[] { ',', ' ', '.' }, StringSplitOptions.RemoveEmptyEntries); int len = s.Length ;
List<string> s_List = new List<string>(s); Console.WriteLine("待排序的字符串");
foreach (var item in s_List)
{
Console.WriteLine(item);
} s_List.Sort((u, v) => {
return u.CompareTo(v);
});
Console.WriteLine("已排序好的字符串");
foreach (var item in s_List)
{
Console.WriteLine(item);
}
}
}
}

String排序

、设计一个控制台应用程序,使用泛型类集合List<T>存储一周七天,并实现添加、排序,插入、删除、输出集合元素。具体要求如下:
()创建一个空的List<string>,并使用Add方法添加一些元素。
()测试List<string>的Count属性和Capacity属性。
()使用Contains方法测试元素是否存在。
()测试Insert的功能,将元素插入到List<string>中的指定索引处。
()利用索引检索List<string>中的元素。
()测试Remove的功能,删除List<string>中的元素。
()遍历List<string>中的所有元素。
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Myproject3
{
class Program
{
static void Main(string[] args)
{
//创造一个空的List
List<string> list = new List<string> (); //测试Add
list.Add("Monday");
list.Add("Tuesday");
list.Add("Wednesday");
list.Add("Thursday");
list.Add("Friday");
list.Add("Saturday");
list.Add("Sunday"); //测试Count,Capacity属性 Console.WriteLine("Count : {0} , Capacity :{1}",list.Count ,list.Capacity);
Console.WriteLine("\n---------\n"); //测试Contain
if( list.Contains("Monday") ){
Console.WriteLine("# # Monday # #");
}
if( !list.Contains("D") ){
Console.WriteLine("Not exists D");
}
Console.WriteLine("\n---------\n"); //测试Insert(Index , "string" )
list.Insert(,"{M} ### {T}");
foreach (var item in list)
{
Console.Write("{0} ",item);
}
Console.WriteLine();
Console.WriteLine("\n---------\n"); //测试list[index]
Console.WriteLine("Index {0}: {1}",list.IndexOf(list[]),list[]);
Console.WriteLine("\n---------\n"); //测试Remove
list.Remove("{M} ### {T}"); //遍历所有元素
foreach (var item in list)
{
Console.Write("{0} ", item);
}
Console.WriteLine();
Console.WriteLine("\n---------\n"); }
}
}

集合List

、设计一个控制台应用程序,模拟管理车牌相关信息,
例如姓名和车牌号码,能够添加、修改、查找、删除、输出车牌信息。
(使用Dictionary<>类完成)
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Myproject
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>(); while (true)
{
Show_Mean();
int opt = int.Parse(Console.ReadLine());
switch (opt)
{
case : Add(dict); break;
case : Modify(dict); break;
case : Find(dict); break;
case : Delete(dict); break;
case : Show_Car(dict); break;
default: break;
}
Console.WriteLine("---请按回车键继续---");
string t = Console.ReadLine();
Console.Clear();
}
} public static void Add(Dictionary<string, string> dict)
{
Console.WriteLine("请输入车牌号");
string key = Console.ReadLine(); Console.WriteLine("请输入车主姓名");
string value = Console.ReadLine(); dict.Add(key, value);
Console.WriteLine("Succsee add :({0} , {1})",key,value);
}
public static void Modify(Dictionary<string, string> dict)
{
Console.WriteLine("请输入车牌号码");
string key = Console.ReadLine(); if( dict.ContainsKey(key) )
{
Console.WriteLine("请输入车主信息");
string value = Console.ReadLine();
dict[key] = value;
Console.WriteLine("Succsee Modify :({0} , {1})", key, value);
}
else
{
Console.WriteLine("无法查询到对应的车牌号.");
} }
public static void Find(Dictionary<string, string> dict)
{
Console.WriteLine("请输入车牌号");
string key = Console.ReadLine();
if( dict.ContainsKey(key))
{
Console.WriteLine(dict[key]);
}
else
{
Console.WriteLine("无法查询到对应的车牌号.");
}
}
public static void Delete(Dictionary<string, string> dict)
{
Console.WriteLine("请输入车牌号");
string key = Console.ReadLine();
if (dict.ContainsKey(key))
{
dict.Remove(key);
Console.WriteLine("Key {0} Delete",key);
}
else
{
Console.WriteLine("无法查询到对应的车牌号.");
}
}
public static void Show_Car(Dictionary<string, string> dict)
{
foreach (KeyValuePair<string,string>item in dict)
{
Console.WriteLine("车牌号 :{0} , 车主信息 :{1}",item.Key,item.Value);
}
}
public static void Show_Mean()
{
//Console.Clear();
Console.WriteLine("##### 车牌管理系统 #####");
Console.WriteLine("1.添加车牌号");
Console.WriteLine("2.修改车牌号");
Console.WriteLine("3.查找车牌号");
Console.WriteLine("4.删除车牌号");
Console.WriteLine("5.输出车牌信息"); }
} }

Dictionary-汽车

最新文章

  1. TPLink 备份文件bin文件解析[续]
  2. I2C Verilog的实现(一)
  3. flex lineChart中自定义datatip
  4. 一种基于FSIM对视频编码图像质量客观评价的方法
  5. 【Linux】windows-linux、linux-linux文件互传
  6. TreeView简单的动态加载数据
  7. eclipse导入工程时,出现Some projects cannot be imported because they already exist in the workspace
  8. java1.8--OptionalInt,OptionalDouble,OptionalLong类
  9. CMake简介
  10. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十三 || DTOs 对象映射使用,项目部署Windows+Linux完整版
  11. XAMPP支持多PHP版本
  12. python----数据驱动ddt的使用
  13. nginx——优化 Nginx 连接超时时间
  14. How to RAMDISK on macOS
  15. 【JAVA面试】java面试题整理(4)
  16. 仿B站项目(4)webpack打包第三方库jQuery
  17. iOS开发微信支付
  18. unity shader base pass and additional pass
  19. [LeetCode_98]Validate Binary Search Tree
  20. MyBatis中的缓存1

热门文章

  1. CSS链接伪类:超链接的状态
  2. java如何判断溢出
  3. 如果要对img里面的值做特殊处理,可以直接写方法
  4. ICEM-两管相贯
  5. vue on-change 如果有循环的timer 则无限自动执行
  6. Cloudera-Manager(一) —— 基本概念及使用
  7. hdu1237 简单计算器[STL 栈]
  8. hadoop 综合大作业
  9. [Beta]第二次 Scrum Meeting
  10. class文件格式版本号