using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QueueTest
{
/// <summary>
/// 结构体
/// </summary>
struct Dancer
{
public string name;//姓名
public string sex;//性别
//设置吗,名字
public void SetName(string n) {
name = n;
}
//获取名字
public string GetName( )
{
return name;
}
/// <summary>
/// 重写Dancer的ToString()方法 返回name
/// </summary>
/// <returns></returns>
public override string ToString()
{
return name;
}
}
class DamcerClass {
/// <summary>
/// 创建跳舞的派对
/// </summary>
/// <param name="male"></param>
/// <param name="female"></param>
static void mewDamcer(Queue male,Queue female ) {
Dancer m, w;
m = new Dancer();//男
w = new Dancer();//女
//如果男队和女队个数都大于0 就出队
if (male.Count>0&&female.Count>0)
{
m.SetName(male.Dequeue().ToString());
w.SetName(female.Dequeue().ToString());//出队
}
//男的多
else if ((male.Count > 0) &&(female.Count==0))
{
Console.WriteLine("Waiting on a female dancer");
}
//女的多
else if ((male.Count == 0) && (female.Count > 0))
{
Console.WriteLine("Waiting on a male dancer");
}
}
/// <summary>
/// 获取队首元素
/// </summary>
/// <param name="male"></param>
/// <param name="female"></param>
static void headOfLine(Queue male, Queue female) {
Dancer m, w;
m = new Dancer();
w = new Dancer();
if (male.Count>0)
{
m.SetName(male.Peek().ToString());
}
if (female.Count>0)
{
w.SetName(female.Peek().ToString());
}
if (m.name!=""&&w.name!="")
{
Console.WriteLine("Next in line are:"+m.name+"\t" +w.name);
}
else
{
if (m.name != "")
{
Console.WriteLine("Next in line is: "+m.name);
}
else
Console.WriteLine("Next in line is: " + w.name);
}
}
/// <summary>
///
/// </summary>
/// <param name="male"></param>
/// <param name="female"></param>
static void startDaning(Queue male, Queue female) {
Dancer m, w;
m = new Dancer();
w = new Dancer();
Console.WriteLine("Dance partners are:");
Console.WriteLine();
for (int i = 0; i <=3; i++)
{
m.SetName(male.Dequeue().ToString());
w.SetName(female.Dequeue().ToString());
Console.WriteLine(w.name+"\t"+m.name);
}
}
/// <summary>
/// 读取文档组建队
/// </summary>
/// <param name="male"></param>
/// <param name="female"></param>
static void formLines(Queue male, Queue female) {
Dancer d = new Dancer();
StreamReader inFile;//流读取
inFile = File.OpenText(@"H:\工作学习unity\CSharpTest\ConsoleApplication1\QueueTest\dancser.dat");
string line;
while (inFile.Peek()!=-1)
{
line = inFile.ReadLine();//读取一行
d.sex = line.Substring(0,1);
d.name = line.Substring(2, line.Length - 2);
//如果性别为M
if (d.sex == "M")
{
male.Enqueue(d);//进男队
}
else female.Enqueue(d);//否则进女队
}
}
static void Main() {
Queue males = new Queue();
Queue females = new Queue();
formLines(males, females);//获取男女另个队
startDaning(males, females);//搭档
if (males.Count>0||females.Count>0)
{
headOfLine(males, females);
}
mewDamcer(males, females);
if (males.Count > 0 || females.Count > 0)
{
headOfLine(males, females);
}
mewDamcer(males, females);
Console.WriteLine("press enter");
Console.Read();
}
}
}
005_Queue事例
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一.200字去概括全文 200年前人们认为“完美高音”是一种与生俱来的天赋,但是放在今天不足为奇。通过研究实验,那...