⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 C#2005 实例源代码
💻 CS
字号:
using System;

namespace Example_BookList
{
	/// <summary>
	/// 图书类
	/// </summary>
	class Book
	{
		public string strName;		//图书名
		public double dblPrice;		//图书价格
		public string strAuthor;	//图书作者

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="_strName"></param>
		/// <param name="_dblPrice"></param>
		/// <param name="_strAuthor"></param>
		public Book(string _strName,double _dblPrice,string _strAuthor)
		{
			this.strName=_strName;
			this.dblPrice=_dblPrice;
			this.strAuthor=_strAuthor;
		}
	}

	class BookList
	{
		/// <summary>
		/// 按照图书名显示一个图书数组中的多本图书
		/// </summary>
		/// <param name="arrBooks">图书数组</param>
		public static void DisplayByName(Book[] arrBooks)
		{
			//获取图书数目,用户动态建立“书名”数组
			int bookNumber=arrBooks.GetUpperBound(0)-arrBooks.GetLowerBound(0)+1;	

			//使用CreateInstance方法,动态建立“书名”数组
			int[] lengths=new int[]{bookNumber};
			int[] lowerBounds=new int[]{0};			
			Array arrNames=Array.CreateInstance(Type.GetType("System.String"),lengths,lowerBounds);

			//为“书名”数组赋值
			for(int i=arrBooks.GetLowerBound(0);i<=arrBooks.GetUpperBound(0);i++)
				arrNames.SetValue(arrBooks[i].strName,i);
			
			//利用Sort方法,以“书名”为键,将图书排序
			Array.Sort(arrNames,arrBooks);

			//显示排序后的图书列表
			foreach(Book item in arrBooks)
			{
				Console.WriteLine("{0}	{1}	{2}",item.strName,item.dblPrice,item.strAuthor);
			}
		}

		/// <summary>
		/// 按照图书价格显示一个图书数组中的多本图书
		/// </summary>
		/// <param name="arrBooks">图书数组</param>
		public static void DisplayByPrice(Book[] arrBooks)
		{
			int bookNumber=arrBooks.GetUpperBound(0)-arrBooks.GetLowerBound(0)+1;	//获取图书数目

			int[] lengths=new int[]{bookNumber};
			int[] lowerBounds=new int[]{0};

			Array arrPrice=Array.CreateInstance(Type.GetType("System.Double"),lengths,lowerBounds);
			
			for(int i=arrBooks.GetLowerBound(0);i<=arrBooks.GetUpperBound(0);i++)
				arrPrice.SetValue(arrBooks[i].dblPrice,i);

			Array.Sort(arrPrice,arrBooks);

			foreach(Book item in arrBooks)
			{
				Console.WriteLine("{0}	{1}	{2}",item.strName,item.dblPrice,item.strAuthor);
			}
		}

		/// <summary>
		/// 按照图书名显示一个图书数组中的多本图书
		/// </summary>
		/// <param name="arrBooks">图书数组</param>
		public static void DisplayByAuthor(Book[] arrBooks)
		{
			int bookNumber=arrBooks.GetUpperBound(0)-arrBooks.GetLowerBound(0)+1;	//获取图书数目

			int[] lengths=new int[]{bookNumber};
			int[] lowerBounds=new int[]{0};
			
			Array arrAuthors=Array.CreateInstance(Type.GetType("System.String"),lengths,lowerBounds);
			for(int i=arrBooks.GetLowerBound(0);i<=arrBooks.GetUpperBound(0);i++)
				arrAuthors.SetValue(arrBooks[i].strAuthor,i);

			Array.Sort(arrAuthors,arrBooks);

			foreach(Book item in arrBooks)
			{
				Console.WriteLine("{0}	{1}	{2}",item.strName,item.dblPrice,item.strAuthor);
			}
		}
	}

	class MainTest
	{

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Book b1=new Book("我的2005",20,"王");
			Book b2=new Book("家庭烹饪技术",18.23,"张");
			Book b3=new Book("西方哲学史",34.99,"周");
			Book b4=new Book("三侠五义",11.45,"吴");
			Book b5=new Book("象棋23式",122.50,"鲍");

			Book[] myBooksArr=new Book[]{b1,b2,b3,b4,b5};
			while(true)
			
			{
				Console.WriteLine("\n>>请输入排序规则:1-按书名;2-按价格;3-按作者");
				string type=Console.ReadLine();
				//string type="2";
				switch(Convert.ToInt32(type))
				{
					case 1:
						BookList.DisplayByName(myBooksArr);
						break;
					case 2:
						BookList.DisplayByPrice(myBooksArr);
						break;
					case 3:
						BookList.DisplayByAuthor(myBooksArr);
						break;
					default:
						break;
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -