代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.Text;
namespace c_sharp_experiment03
{
class Card {
private string title, author;
private int total;
public Card() {
title = "";
author = "";
total = 0;
}
public Card(string title, string author, int total) {
this.title = title;
this.author = author;
this.total = total;
}
//书的入库处理
public void store(ref Card card) {
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 {
get {
return title;
}
set {
author = value;
}
}
public string Author {
get {
return author;
}
set {
author = value;
}
}
public int Total {
get {
return total;
}
set {
total = value;
}
}
}
class Test3_1 {
static void Main(string[] args) {
Test3_1 T = new Test3_1();
Card[] books;
int[] index;
int i,k;
Card card = new Card();
Console.Write("请输入需要入库图书的种类总数:");
string sline = Console.ReadLine();
int num = int.Parse(sline);
books = new Card[num];
for (i = 0; i < num; i++) {
books[i] = new Card();
}
index = new int[num];
for (i = 0; i < num; i++) {
Console.Write("请输入书名:");
card.Title = Console.ReadLine();
Console.Write("请输入作者:");
card.Author = Console.ReadLine();
Console.Write("请输入库存量");
sline = Console.ReadLine();
card.Total = int.Parse(sline);
books[i].store(ref card);
index[i] = i;
}
Console.Write("请选择按什么关键字排序(1.按书名,2.按作者,3.按入库量)");
sline = Console.ReadLine();
int choice = int.Parse(sline);
switch (choice) {
case 1:
T.sortTitle(books, index);
break;
case 2:
T.sortAuthor(books, index);
break;
case 3:
T.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[n];
j = index[n + 1];
if (string.Compare(book[i].Title, book[j].Title) > 0)
{
temp = index[n];
index[n] = 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[n];
j = index[n + 1];
if (string.Compare(book[i].Author, book[j].Author) > 0)
{
temp = index[n];
index[n] = 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[n];
j = index[n + 1];
if (book[i].Total > book[i].Total)
{
temp = index[n];
index[n] = index[n + 1];
index[n + 1] = temp;
}
}
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。