图书管理系统的需求:
用结构体来实现图书管理系统,需要在cmd中运行
用MonoDevelop或者VS进行编码
用结构体写图书管理系统,此代码可以在Monodevelop上或者在VS上运行
需要实现的第一个功能是能够进行图书的添加将图书的信息保存到一个数组中,数组的类型为结构体所命名的类型
第二个功能能够进行图书的删除
第三个功能能够进行图书的修改
第四个功能能够进行图书的查询
第五个功能能够进行图书系统的退出
代码如下:
using System; using System.Threading; namespace book_Manager { struct Book { //公司变量名前面加一个m_ /// <summary> /// 书的名字 /// </summary> public string m_BookName; /// <summary> /// 书的作者 /// </summary> public string m_BookAuthor; /// <summary> /// 书的价格 /// </summary> public float m_Price; } class MainClass { public static void Main(string[] args) { Book book1 = new Book (); book1.m_BookName="平凡的世界"; book1.m_BookAuthor = "路遥"; book1.m_Price = 10; Book book2; book2.m_BookName="射雕英雄传"; book2.m_BookAuthor="金庸"; book2.m_Price=23.5f; //数组的容量 int m_Capacity=4; //数组所添加图书的个数 int m_Count=0; Book[] m_Books = new Book[4]; m_Books [0] = book1; m_Books [1] = book2; m_Count = 2; while (true) { Console.WriteLine ("**************************************************"); Console.WriteLine ("* 欢迎进入图书管理系统 *"); Console.WriteLine ("*A:添加 D:删除 C:修改 S:查看 Q:退出 P:全部显示 *"); Console.WriteLine ("**************************************************"); Console.Write ("请输入你要进行的选择(Q表示退出!):"); string input = Console.ReadLine (); //把input字符串转换成大写 然后再赋值给input input=input.ToUpper(); switch (input) { //添加 case "A": { if (m_Count < m_Capacity) { //往数组里面加一本书 Book book; Console.Write ("请输入要添加的图书的名字:"); string name = Console.ReadLine (); Console.Write ("前输入要添加的图书的作者:"); string author = Console.ReadLine (); Console.Write ("请输入要添加的图书的价格:"); float price = float.Parse (Console.ReadLine ()); book.m_BookName = name; book.m_BookAuthor = author; book.m_Price = price; //进行其中的显示的清除 Console.Clear (); m_Books [m_Count] = book; m_Count++; } else { //需要把数组的容量变大 m_Capacity += 4; //通过容量创建一个新的数组 Book[] temp = new Book[m_Capacity]; //把原有的数组里面的元素全部拷贝到新数组里面 for (int i = 0; i < m_Count; i++) { temp [i] = m_Books [i]; } //赋值以后原数组和新数组为用一个数组,都是新创建的那个数组 m_Books = temp; } } break; //删除 case "D": { Console.WriteLine ("E:按名称删除 R:按作者删除"); Console.Write ("请输入您的选择:"); string str = Console.ReadLine (); switch (str) { case "E": //判断是否找到此书 bool isFind = false; Console.Write ("请输入您要进行删除的图书的名字:"); string strName = Console.ReadLine (); for (int i = 0; i < m_Count; i++) { if (m_Books[i].m_BookName==strName) { isFind = true; } } if (isFind) { Console.WriteLine ("删除成功!!!!!"); m_Books [m_Count].m_BookName = ""; m_Books [m_Count].m_BookAuthor = ""; m_Books [m_Count].m_Price = 0; m_Count--; } else { Console.WriteLine ("删除失败!!!!!"); } break; case "R": //判断是否找到此书 bool isFind1 = false; Console.Write ("请输入您要进行删除的图书的作者:"); string strAuthor = Console.ReadLine (); for (int i = 0; i < m_Count; i++) { if (m_Books[i].m_BookAuthor==strAuthor) { isFind = true; } } if (isFind1) { Console.WriteLine ("删除成功!!!!!"); m_Books [m_Count].m_BookName = ""; m_Books [m_Count].m_BookAuthor = ""; m_Books [m_Count].m_Price = 0; m_Count--; } else { Console.WriteLine ("删除失败!!!!!"); } break; } } break; //修改 case "C": { int index = 0; bool isFind = false; Console.Write ("请输入要修改的书的名字:"); string strName = Console.ReadLine (); for (int i = 0; i < m_Count; i++) { if (m_Books[i].m_BookName==strName) { index = i; isFind = true; } } if (isFind) { Console.WriteLine ("Y:修改图书名称 H:修改图书作者 G:修改图书价格"); Console.Write ("请输入您的选择:"); string str = Console.ReadLine (); switch (str) { case "Y": Console.Write ("请输入您要修改成的图书名称:"); string strName1=Console.ReadLine(); m_Books[index].m_BookName=strName1; Console.WriteLine ("修改成功!!!!!!"); break; case "H": Console.Write ("请输入您要修改成的图书作者:"); string strAuthor=Console.ReadLine(); m_Books[index].m_BookAuthor=strAuthor; Console.WriteLine ("修改成功!!!!!!"); break; case "G": Console.Write ("请输入您要修改成的图书名称:"); int price=int.Parse(Console.ReadLine()); m_Books[index].m_Price=price; Console.WriteLine ("修改成功!!!!!!"); break; } } else { Console.WriteLine ("没有此图书无法进行修改!!!!!"); } } break; //查看 case "S": { Console.WriteLine ("N:按名称查找 Z:按作者查找"); Console.Write ("请输入您的选择:"); string str = Console.ReadLine (); str = str.ToUpper (); switch (str) { case "N": { Console.Write ("请输入要查找书的名称:"); string strName = Console.ReadLine (); //判断是否找到此书 bool isFind = false; //记录找到的书的下标 int index = 0; for (int i = 0; i < m_Count; i++) { if (m_Books [i].m_BookName == strName) { isFind = true; index = i; } } if (isFind) { Console.WriteLine ("书的名字为:{0},书的作者为:{1},书的价格为:{2}", m_Books [index].m_BookName, m_Books [index].m_BookAuthor, m_Books [index].m_Price); } else { Console.WriteLine ("没有找到这本书!!!!!!"); } } break; case "Z": { Console.Write ("请输入要查找书的作者:"); string strAuthor = Console.ReadLine (); //判断是否找到此书 bool isFind = false; //记录找到的书的下标 int index = 0; for (int i = 0; i < m_Count; i++) { if (m_Books [i].m_BookName == strAuthor) { isFind = true; index = i; } } if (isFind) { Console.WriteLine ("书的名字为:{0},书的作者为:{1},书的价格为:{2}", m_Books [index].m_BookName, m_Books [index].m_BookAuthor, m_Books [index].m_Price); } else { Console.WriteLine ("没有找到这本书!!!!!!"); } } break; } } break; case "P": for (int i = 0; i < m_Count; i++) { Console.WriteLine ("书的名字:{0},书的作者:{1},书的价格:{2}", m_Books[i].m_BookName, m_Books[i].m_BookAuthor, m_Books[i].m_Price); } break; } if (input=="Q") { break; } } Console.WriteLine ("退出程序成功!!!!!"); } } }
以上代码中应用了数组的扩容方法,随着数组的扩大数组的容量也应该跟着扩大,那样的话就需要首先将原数组的数据放到新数组中,然后赋值原数组等于新数组
//通过容量创建一个新的数组 Book[] temp = new Book[m_Capacity]; //把原有的数组里面的元素全部拷贝到新数组里面 for (int i = 0; i < m_Count; i++) { temp [i] = m_Books [i]; } //赋值以后原数组和新数组为用一个数组,都是新创建的那个数组 m_Books = temp;
明天分享用集合书写图书管理系统,有需求的可以在下方评论区写下自己的要做的cmd小程序的需求,大家可以一起分享一起讨论做???
原文地址:https://www.cnblogs.com/baosong/p/8467318.html
时间: 2024-10-11 12:09:19