Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the given example in C#. (Please also explain how to extract .dat file

ID: 3593303 • Letter: I

Question

Implement the given example in C#. (Please also explain how to extract .dat file that contain dance member.)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Collections;

using System.IO;

namespace ConsoleApplication31

{

public struct Dancer

{

public string name;

public string sex;

public void GetName(string n)

{

name = n;

}

public override string ToString()

{

return name;

}

}

class Class1

{

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.WriteLine("Waiting on a female dancer.");

else if ((female.Count > 0) && (male.Count ==0))

Console.WriteLine("Waiting on a male dancer.");

}

static void headOfLine(Queue male, Queue female)

{

Dancer w, m;

m = new Dancer();

w = new Dancer();

if (male.Count > 0)

m.GetName(male.Peek().ToString());

if (female.Count > 0)

w.GetName(female.Peek().ToString());

if (m.name != " " && w.name != "")

Console.WriteLine("Next in line are: " +m.name + " "+ w.name);

else if (m.name != "")

Console.WriteLine("Next in line is: " +m.name);

else

Console.WriteLine("Next in line is: " +w.name);

}

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 count = 0; count <= 3; count++)

{

m.GetName(male.Dequeue().ToString());

w.GetName(female.Dequeue().ToString());

Console.WriteLine(w.name + " " + m.name);

}

}

static void formLines(Queue male, Queue female)

{

Dancer d = new Dancer();

StreamReader inFile;

inFile = File.OpenText("C:\Users\Documents\\Projects\ConsoleApplication2\ConsoleApplication2\dancer.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);

}

}

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.Write("press enter");

Console.Read();

}

}

}

//--------------dancer.dat-------------------

F Jessica Rabbit

M Mark Franklin

M Jacob Bentol

M Michael Lim

F Rosy Slater

M Raymond Hernandez

F Isabel Lacy

M Bret Lee

F Lara Biddy

M Darrel Fertil

M Matthew Bogart

F Carli Maia

Explanation / Answer

Use the below code to read the data from .dat file.

StreamReader objInput = new StreamReader("C:\values.dat", System.Text.Encoding.Default)

string contents = objInput.ReadToEnd().Trim();

string [] split = System.Text.RegularExpressions.Regex.Split(contents, "\s+", RegexOptions.None);   

foreach (string s in split)

{

Console.WriteLine(s);

}