. 定义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. Hibernate对象持久化的三种状态

    1.三种状态: public static void testSel() { Session session = HibernateUtils.openSession(); Transaction t ...

  2. 权限管理(chown、chgrp、umask)

    对于文件或目录的权限的修改,只能管理员和文件的所有者拥有此权限,但是对于文件或目录的的所有者的更改,只有管理员拥有此权限(虽然普通用户创建的文件或目录,用户也不能修改文件或目录的所有者). 1.cho ...

  3. nexus 3.17.0 简单说明

    nexus 在6.24 发布了3.17.0 ,同时包含了好多新的特性 以下为一些主要变动: routing rules 可以增强repo 的安全 apt repo 格式的支持 可以方便的为ubuntu ...

  4. [golang]Go常见问题:# command-line-arguments: ***: undefined: ***

    今天遇见一个很蛋疼的问题,不知道是不是我配置的问题,IDE直接run就报错. 问题描述 在开发代码过程中,经常会因为逻辑处理而对代码进行分类,放进不同的文件里面:像这样,同一个包下的两个文件,点击id ...

  5. Django自带后台admin的使用配置

    Django自带后台使用配置参考官网地址:https://docs.djangoproject.com/en/1.11/ref/contrib/admin/ ,本文章值是介绍简单配置,如果需要详细内容 ...

  6. mysql pi() 获取pi

    mysql> select pi(); +----------+ | pi() | +----------+ | 3.141593 | +----------+ row in set (0.00 ...

  7. Koa 操作 Mongodb 数据库

    node-mongodb-native的介绍 使用基于官方的 node-mongodb-native 驱动,封装一个更小.更快.更灵活的 DB 模块, 让我们用 nodejs 操作 Mongodb 数 ...

  8. dubbo、zookeeper心跳相关参数解析与测试

    dubbo consumer和provider的心跳机制 dubbo客户端和dubbo服务端之间存在心跳,目的是维持provider和consumer之间的长连接.由dubbo客户端主动发起,可参见d ...

  9. Mysql中的Date转换

    一.背景 Mysql中有张表,表的一列为Date类型. 1. 插入日期xxx.setCreateTime(new Date())mybatis.insert(xxx) 2. 读取日期用Mybaitis ...

  10. 如何在真实串口驱动还未加载的情况下调试uboot?

    1. 先找出真实串口是什么型号 1.1 怎么找?笔者提供两种方案: 方案一: 若当前的板子支持dm,从uboot的dts找串口节点对应的compatible属性 方案二: 从linux内核的dts找串 ...