您的位置:首页 > 数据库

C# 实现一个简单的图书管理系统(无数据库)新手教程1

2013-11-13 23:21 1186 查看
源码在vs2005中测试可以运行;

源码如下:

using System;
class Card

{

private string title, author;

private int total;

public Card()

{

title = "";

author = "";

total = 0;

}

//store函数使用ref关键字用来存储图书信息
public void store(ref Card card)//使用ref关键字进行引用传递

{

title = card.title;

author = card.author;

total = card.total;

}

public void show()

{

Console.WriteLine("Title:{0},Author:{1},Total:{2}", title, author, total);

}

public string Title//Title的属性可读可写

{

get

{

return title;

}

set

{

title = value;

}

}

public string Author

{

get

{

return author;

}

set

{

author = value;

}

}

public string Total

{

get

{

return Total;

}

set

{

total = int.Parse(value);

}

}

}

class Table

{

static void Main(string[] args)

{

Console.WriteLine("**********图书管理系统*******");

Card[] books;

int[] index;

int i, k;

Card card = new Card();//构造函数,title=“”;author=“”;total=0;

Console.Write("请输入需要入库图书的总类数:");

string No = Console.ReadLine();

int num = int.Parse(No);

books = new Card[num];

index = new int[num];//先声明,再初始化,这样才符合题目要求

for (i = 0; i < num; i++)

books[i] = new Card();

for (i = 0; i < num; i++)

{

Console.Write("请输入书名:");

card.Title = Console.ReadLine();

Console.Write("请输入作者:");

card.Author = Console.ReadLine();

Console.Write("请输入入库量:");

No = Console.ReadLine();

card.Total = No;

books[i].store(ref card);//使用ref关键字进行引用传递

index[i] = i; //感觉很鸡肋的一个参数,用来传参实现了对图书数量的运用

}

Console.Write("请选择按什么关键字排序(1.按书名,2.按作者,3.按入库量)");

No = Console.ReadLine();

int choice = int.Parse(No);

switch (choice)

{

case 1:

this.sortTitle(books, index);

break;

case 2:

this.sortAuthor(books, index);

break;

case 3:

this.sortTotal(books, index);

break;

}

for (i = 0; i < num; i++)

{

k = index[i];

books[k].show();

}

// Console.Read();

}

//按存入书的书名的首字母进行排序

void sortTitle(Card[] book, int[] index)

{

int i, j, m, n, temp;

for (m = 0; m < index.Length - 1; m++)

{

for (n = 0; n < index.Length - m - 1; n++)

{

i = index
;

j = index[n + 1];

if (string.Compare(book[i].Title, book[j].Title) > 0)

{

temp = index
;

index
= index[n + 1];

index[n + 1] = temp;

}

}

}

}

//按存入书的作者的名字的首字母进行排序

void sortAuthor(Card[] book, int[] index)

{

int i, j, m, n, temp;

for (m = 0; m < index.Length - 1; m++)

for (n = 0; n < index.Length - m - 1; n++)

{

i = index
;

j = index[n + 1];

if (string.Compare(book[i].Author, book[j].Author) > 0)

{

temp = index
;

index
= index[n + 1];

index[n + 1] = temp;

}

}

}

//按存入书的数量进行排序

void sortTotal(Card[] book, int[] index)

{

int i, j, m, n, temp;

for (m = 0; m < index.Length - 1; m++)

for (n = 0; n < index.Length - m - 1; n++)

{

i = index
;

j = index[n + 1];

if (int.Parse(book[i].Total) > int.Parse(book[j].Total))//Total为属性

{

temp = index
;

index
= index[n + 1];

index[n + 1] = temp;

}

}

}

}

程序简单说明:

1、ref关键字实现引用类型传递,注意在其他函数中调用过该函数时参数也要声明ref类型;

2、(int),Int32.Parse() 和 Convert.toInt32()的区别:

int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 System.Int32。

(int)表示使用显式强制转换,是一种类型转换。当我们从 int 类型到 long、float、double 或decimal 类型,可以使用隐式转换,但是当我们从 long 类型到 int 类型转换就需要使用显式强制转换,否则会产生编译错误。

Int32.Parse()表示将数字的字符串转换为32 位有符号整数,属于内容转换[1]。

我们一种常见的方法:public static int Parse(string)。

如果 string 为空,则抛出 ArgumentNullException 异常;

如果 string 格式不正确,则抛出 FormatException 异常;

如果 string 的值小于 MinValue 或大于 MaxValue 的数字,则抛出 OverflowException 异常。

Convert.ToInt32() 则可以将多种类型(包括 object 引用类型)的值转换为 int 类型,因为它有许多重载版本[2]:

public static int ToInt32(object);

public static int ToInt32(bool);

public static int ToInt32(byte);

public static int ToInt32(char);

public static int ToInt32(decimal);

public static int ToInt32(double);

public static int ToInt32(short);

public static int ToInt32(long);

public static int ToInt32(sbyte);

public static int ToInt32(string);

(int)和Int32.Parse(),Convert.ToInt32()三者的应用举几个例子:

例子一:

long longType = 100;

int intType = longType; // 错误,需要使用显式强制转换

int intType = (int)longType; //正确,使用了显式强制转换

例子二:

string stringType = "12345";

int intType = (int)stringType; //错误,string 类型不能直接转换为 int 类型

int intType = Int32.Parse(stringType); //正确

例子三:

long longType = 100;

string stringType = "12345";

object objectType = "54321";

int intType = Convert.ToInt32(longType); //正确

int intType = Convert.ToInt32(stringType); //正确

int intType = Convert.ToInt32(objectType); //正确
3、注意Main方法中各种参数的设置,体现编程思路;

4、在面向对象的编程中,简单比较字符串大小往往用库中编写好的函数,直接调用;本例中冒泡法的实现就是用string.compare()函数就实现字符串的比较;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: