using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace TestOfQueueDancers { class Program { static void Main(string[] args) { Queue males = new Queue(); Queue females = new Queue(); FormLines(males,females); StartDancing(males,females); if (males.Count > 0 || females.Count > 0) { HeadOfLine(males,females); } NewDancers(males,females); if (males.Count > 0 || females.Count > 0) { HeadOfLine(males,females); } NewDancers(males,females); Console.WriteLine("Press Enter"); } static void NewDancers(Queue male, Queue female) { Dancer m, w; m = new Dancer(); w = new Dancer(); if (male.Count > 0 && female.Count > 0) { m.GetName(male.Dequeue().ToString()); w.GetName(female.Dequeue().ToString()); } else if ((male.Count > 0) && (female.Count == 0)) { Console.Write("Wating for female dancer."); } else if ((female.Count > 0) && (male.Count == 0)) { Console.Write("Wating for male dancer."); } } public static void HeadOfLine(Queue male, Queue female) { Dancer w, m; w = new Dancer(); m = new Dancer(); if (male.Count > 0) { m.GetName(male.Peek().ToString()); } if (female.Count > 0) { w.GetName(female.Peek().ToString()); } if (!string.IsNullOrEmpty(m.name) && !string.IsNullOrEmpty(w.name)) { Console.WriteLine("Next in line are:" + m.name + "\t" + w.name); } else { if (!string.IsNullOrEmpty(m.name)) { Console.WriteLine("Next in line is:" + m.name); } else { Console.WriteLine("Next in line is:" + w.name); } } } public static void StartDancing(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 < 4; i++) { m.GetName(male.Dequeue().ToString()); w.GetName(female.Dequeue().ToString()); Console.WriteLine(w.name + "\t" + m.name); } } public static void FormLines(Queue male, Queue female) { Dancer d = new Dancer(); StreamReader inFile; inFile = File.OpenText("dancers.dat"); string line; while (inFile.Peek() != -1) { line = inFile.ReadLine(); d.sex = line.Substring(0, 1); d.name = line.Substring(2, line.Length - 2); if (d.sex == "M") { male.Enqueue(d); } else { female.Enqueue(d); } } } } public struct Dancer { public string name; public string sex; public override string ToString() { return name; } public void GetName(string n) { name = n; } } }
dancers.dat文件类容:
F Jennifer Ingram
M Frank Optiz
M Terrill Beckerman
M Mike Dahly
F Beata Lovelace
M Raymond Williams
F Beth Munson
M Don Gundolf
F Bernica Tackett
M David Durr
M Mike McMillan
F Nikki Feldman