using System;
using System.Collections.Generic;
using System.Text;
namespace SY3_2 {
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)//使用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); }
}
}
public class Test3_2
{
static void Main(string[] args)
{
Test3_2 T = new Test3_2();
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 = 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 (int.Parse (book[i].Total) > int.Parse (book[j].Total))
{
temp = index[n];
index[n] = index[n + 1];
index[n + 1] = temp;
}
}
}
}
}
题目就是将三个方法
void sortTitle(Card[]book,int[]index)
void sortAuthor(Card[]book,int[]index)
void sortTotle(Card[]book,int[]index)
改写成一个方法sort(Card[]book,int[]index,int method),其中增加的参数method指示按照什么字段排序,求各位高手帮帮忙啊~~~
using System.Collections.Generic;
using System.Text;
namespace SY3_2 {
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)//使用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); }
}
}
public class Test3_2
{
static void Main(string[] args)
{
Test3_2 T = new Test3_2();
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 = 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 (int.Parse (book[i].Total) > int.Parse (book[j].Total))
{
temp = index[n];
index[n] = index[n + 1];
index[n + 1] = temp;
}
}
}
}
}
题目就是将三个方法
void sortTitle(Card[]book,int[]index)
void sortAuthor(Card[]book,int[]index)
void sortTotle(Card[]book,int[]index)
改写成一个方法sort(Card[]book,int[]index,int method),其中增加的参数method指示按照什么字段排序,求各位高手帮帮忙啊~~~
{
if(method=title)
{
sortTitle(book,index)
}
if(method=sortAuthor)
{
sortsortAuthor(book,index)
}
if(method=Totle)
{
sortTotle(book,index)
}
}
sortAuthor
sortTotle
三个方法堆砌了很多重复的代码,这还不说,如果想再来个sortXXX,又得复制粘贴出很多重复代码了。这时候,该是你用委托的时候了。