. 定义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-汽车

【C#】上机实验六的更多相关文章

  1. 实验六 CC2530平台上P2P通信的TinyOS编程

    实验六 CC2530平台上P2P通信的TinyOS编程 实验目的: 加深和巩固学生对于TinyOS编程方法的理解和掌握 让学生初步的掌握射频通信TinyOS编程方法 学生通过本实验应理解TinyOS中 ...

  2. oracle上机实验内容

    这是oracle实验的部分代码,我花了一中午做的. 第一次上机内容 实验目的:熟悉ORACLE11G的环境 实验内容: 第二次上机内容 实验目标:掌握oracle体系结构,掌握sqlplus的运行环境 ...

  3. lingo运筹学上机实验指导

    <运筹学上机实验指导>分为两个部分,第一部分12学时,是与运筹学理论课上机同步配套的4个实验(线性规划.灵敏度分析.运输问题与指派问题.最短路问题和背包问题)的Excel.LONGO和LI ...

  4. VMware vSphere服务器虚拟化实验六 vCenter Server 添加储存

                                                                          VMware vSphere服务器虚拟化实验六 vCente ...

  5. 算法课上机实验(一个简单的GUI排序算法比较程序)

    (在家里的电脑上Linux Deepin截的图,屏幕大一点的话,deepin用着还挺不错的说) 这个应该是大二的算法课程上机实验时做的一个小程序,也是我的第一个GUI小程序,实现什么的都记不清了,只记 ...

  6. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验六:数码管模块

    实验六:数码管模块 有关数码管的驱动,想必读者已经学烂了 ... 不过,作为学习的新仪式,再烂的东西也要温故知新,不然学习就会不健全.黑金开发板上的数码管资源,由始至终都没有改变过,笔者因此由身怀念. ...

  7. Java第一次上机实验源代码

    小学生计算题: package 第一次上机实验_; import java.util.*; public class 小学计算题 { public static void main(String[] ...

  8. 实验 六:分析linux内核创建一个新进程的过程

    实验六:分析Linux内核创建一个新进程的过程 作者:王朝宪  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029 ...

  9. Linux内核分析实验六

    Linux内核分析实验六 进程控制块PCB——task_struct(进程描述符) 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struct task_s ...

随机推荐

  1. 16-ESP8266 SDK开发基础入门篇--TCP 服务器 非RTOS运行版,串口透传(串口回调函数处理版)

    https://www.cnblogs.com/yangfengwu/p/11105466.html 其实官方给的RTOS的版本就是在原先非RTOS版本上增加的 https://www.cnblogs ...

  2. IDEA 重新 build Project

  3. mysql 获取数学成绩最高以及最低的同学

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  4. [开源] FreeSql.AdminLTE.Tools 根据实体类生成后台管理代码

    前言 FreeSql 发布至今已经有9个月,功能渐渐完善,自身的生态也逐步形成,早在几个月前写过一篇文章<ORM 开发环境之利器:MVC 中间件 FreeSql.AdminLTE>,您可以 ...

  5. 面试问我 Java 逃逸分析,瞬间被秒杀了。。

    记得几年前有一次栈长去面试,问到了这么一个问题: Java中的对象都是在堆中分配吗?说明为什么! 当时我被问得一脸蒙逼,瞬间被秒杀得体无完肤,当时我压根就不知道他在考什么知识点,难道对象不是在堆中分配 ...

  6. Spring Cloud Ribbon---微服务调用和客户端负载均衡

    前面分析了Eureka的使用,作为服务注册中心,Eureka 分为 Server 端和 Client 端,Client 端作为服务的提供者,将自己注册到 Server 端,Client端高可用的方式是 ...

  7. 范仁义html+css课程---8、新元素布局

    范仁义html+css课程---8.新元素布局 一.总结 一句话总结: 推荐用新标签(语义化的标签)来布局:header(头部),footer(尾部),aside(侧边栏),nav(导航),artic ...

  8. [E2E_L7 51CTO]初步接触OpenVINO提供的例子(win+vs)

    一.例子编译 1.运行 C:\Program Files (x86)\IntelSWTools\openvino_2019.1.148\bin\setupvars.bat 这步需要win平台下安装py ...

  9. 源码包的三个参数make-configure-make install解释

    1.configure 这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码: ./configure --prefix= ...

  10. python初级(302) 4 函数

    一.函数 1.函数定义: 可以完成某个工作的代码块.这是可以用来构建更大程序的一个小部分. 2.创建或定义函数要使用def关键字 3.创建一个函数 1) def 关键字 2)函数名及后面跟随的括号 3 ...