【C#】上机实验六
. 定义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#】上机实验六的更多相关文章
- 实验六 CC2530平台上P2P通信的TinyOS编程
实验六 CC2530平台上P2P通信的TinyOS编程 实验目的: 加深和巩固学生对于TinyOS编程方法的理解和掌握 让学生初步的掌握射频通信TinyOS编程方法 学生通过本实验应理解TinyOS中 ...
- oracle上机实验内容
这是oracle实验的部分代码,我花了一中午做的. 第一次上机内容 实验目的:熟悉ORACLE11G的环境 实验内容: 第二次上机内容 实验目标:掌握oracle体系结构,掌握sqlplus的运行环境 ...
- lingo运筹学上机实验指导
<运筹学上机实验指导>分为两个部分,第一部分12学时,是与运筹学理论课上机同步配套的4个实验(线性规划.灵敏度分析.运输问题与指派问题.最短路问题和背包问题)的Excel.LONGO和LI ...
- VMware vSphere服务器虚拟化实验六 vCenter Server 添加储存
VMware vSphere服务器虚拟化实验六 vCente ...
- 算法课上机实验(一个简单的GUI排序算法比较程序)
(在家里的电脑上Linux Deepin截的图,屏幕大一点的话,deepin用着还挺不错的说) 这个应该是大二的算法课程上机实验时做的一个小程序,也是我的第一个GUI小程序,实现什么的都记不清了,只记 ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验六:数码管模块
实验六:数码管模块 有关数码管的驱动,想必读者已经学烂了 ... 不过,作为学习的新仪式,再烂的东西也要温故知新,不然学习就会不健全.黑金开发板上的数码管资源,由始至终都没有改变过,笔者因此由身怀念. ...
- Java第一次上机实验源代码
小学生计算题: package 第一次上机实验_; import java.util.*; public class 小学计算题 { public static void main(String[] ...
- 实验 六:分析linux内核创建一个新进程的过程
实验六:分析Linux内核创建一个新进程的过程 作者:王朝宪 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029 ...
- Linux内核分析实验六
Linux内核分析实验六 进程控制块PCB——task_struct(进程描述符) 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. struct task_s ...
随机推荐
- facl
file access control lists 文件的额外赋权机制,针对性的对某用户对文件的权限进行处理 setfacl 指定空权限
- P4899 【[IOI2018] werewolf 狼人】
感觉已经几次碰到这种类型的题目了,写篇\(Blog\)总结一下 题意: 是否存在一条\((s_i, t_i)\)的路径,满足先只走编号不超过\(L_i\)的点,再走编号不超过\(R_i\)的点 \(S ...
- 【loj3123】【CTS2019】重复
题目 给出一个长度为\(n\)的串\(s\),询问有多少个长度为\(m\)的串\(t\) 满足 \(t\) 的无限循环串存在一个长度为\(n\)且比\(s\)字典序严格小的子串 $ n , m \le ...
- Kafka 消费者到底是什么 以及消费者位移主题到底是什么(Python 客户端 1.01 broker)
Kafka 中有这样一个概念消费者组,所有我们去订阅 topic 和 topic 交互的一些操作我们都是通过消费者组去交互的. 在 consumer 端设置了消费者的名字之后,该客户端可以对多个 to ...
- Tkinter 之使用PAGE工具开发GUI界面
一.安装 1.官网下载 PAGE http://page.sourceforge.net/ Tcl(8.6+) https://www.activestate.com/activetcl/downlo ...
- Dubbo+zookeeper实现单表的增删改查
1.数据库准备 建表语句 CREATE TABLE `tb_brand` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) DEFAULT NULL COMMENT ...
- 刷题记录:[CISCN2019 华北赛区 Day1 Web2]ikun
目录 刷题记录:[CISCN2019 华北赛区 Day1 Web2]ikun 一.涉及知识点 1.薅羊毛逻辑漏洞 2.jwt-cookies伪造 Python反序列化 二.解题方法 刷题记录:[CIS ...
- openSTack备份恢复
- k8s记录-kube-dns(core-dns)配置(七)
docker search corednsdocker pull xxx 拉取镜像(根据实际情况选择)docker tag xxx coredns/coredns:latestdocker tag c ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...