程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 年度單身舞會隊列

年度單身舞會隊列

編輯:C#入門知識

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; 
        } 
    } 

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

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved