Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------------------------
- // P175B123. CW2 example task
- //----------------------------------------------------------
- using System;
- using System.IO;
- using System.Net.Http.Headers;
- using System.Runtime.InteropServices;
- /*
- * VIDKO:
- * Module code:
- */
- namespace DummyElements
- {
- /// <summary>
- /// Enumerator: male, female
- /// </summary>
- enum Gender
- {
- Male = 1,
- Female = 2,
- }
- /// <summary>
- /// Class to store data about single author
- /// </summary>
- class Author
- {
- // Auto-properties
- public string authorName { get; set; }
- public string bookName { get; set; }
- public Gender Gender { get; set; }
- public string publishingHouse { get; set; }
- public double Price { get; set; }
- /// <summary>
- /// Default constructor
- /// </summary>
- public Author()
- {
- }
- /// <summary>
- /// Constructor with parameters
- /// </summary>
- public Author(string author, string book,
- Gender gender, string house, double Price)
- {
- authorName = author;
- bookName = book;
- Gender = gender;
- publishingHouse = house;
- this.Price = Price;
- }
- public override string ToString()
- {
- string line;
- line = string.Format("{0, -20} {1, 8} {2, -8} {3, -10} {4, 6:f2}",
- authorName, bookName, Gender, publishingHouse, Price);
- return line;
- }
- // Overriden method Equals()
- public override bool Equals(object myObject)
- {
- Author author = myObject as Author;
- return author.authorName == authorName &&
- author.bookName == bookName;
- }
- // Overriden method GetHashCode()
- public override int GetHashCode()
- {
- return authorName.GetHashCode() ^
- bookName.GetHashCode();
- }
- // Overloaded operator >=
- public static bool operator >=(Author author1, Author author2)
- {
- int poz = String.Compare(author1.bookName, author2.bookName,
- StringComparison.CurrentCulture);
- if ((author1.Price > author2.Price) ||
- (Math.Abs(author1.Price - author2.Price) <= 0.001 && poz < 0))
- return true;
- else return false;
- }
- // Overloaded operator <=
- public static bool operator <=(Author author1, Author author2)
- {
- int poz = String.Compare(author1.bookName, author2.bookName,
- StringComparison.CurrentCulture);
- if ((author1.Price < author2.Price) ||
- (Math.Abs(author1.Price - author2.Price) <= 0.001 && poz > 0))
- return true;
- else return false;
- }
- }
- /// <summary>
- /// Container class
- /// Implemented as singly linked list of authors, without dummy elements
- /// </summary>
- sealed class LinkedListOfAuthors
- {
- // Declaration of pointers
- private NodeAuthor head; // start address
- private NodeAuthor tail; // end address
- private NodeAuthor tailE; // end address (extra)
- private NodeAuthor iP; // pointer for interface
- // TODO (0.4): implement functionality for node class (stores data of Author and reference to next element)
- // Define 2 consctructors (with and without parameters)
- private sealed class NodeAuthor
- {
- public Author Author { get; set; }
- public NodeAuthor Next { get; set; }
- public NodeAuthor() { }
- public NodeAuthor(Author Author, NodeAuthor Next)
- {
- this.Author = Author;
- this.Next = Next;
- }
- }
- // TODO (0.2): implement consturctor by the given header
- public LinkedListOfAuthors()
- {
- Author temp = new Author();
- temp.authorName = "MinName";
- Author temp1 = new Author();
- temp1.authorName = "MaxName";
- tail = new NodeAuthor(temp1, null);
- head = new NodeAuthor(temp, tail);
- tailE = head;
- iP = null;
- }
- // TODO (0.1): implement method by given header
- // Returns true if linked list is empty; false otherwise
- public bool IsEmpty()
- {
- // TODO: implement method
- return tailE == head;
- }
- // TODO (0.1): implement method by given header
- // Returns data (Author object) of first element in singly linked list
- public Author First()
- {
- // TODO: implement method
- if (head == tailE)
- return null;
- return head.Next.Author;
- }
- // TODO (0.2): implement method by given header
- // Returns number of elements in singly linked list
- public int Count()
- {
- // TODO: implement method
- int cnt = 0;
- for (NodeAuthor temp = head.Next; temp != tail; temp = temp.Next)
- cnt++;
- return cnt;
- }
- // TODO (0.3): implement method by given header
- // Adds new element (author) to the FRONT of singly linked list
- public void AddToFront(Author author)
- {
- if (head == tailE)
- {
- head.Next = new NodeAuthor(author, head.Next);
- tailE = head.Next;
- }
- else
- {
- head.Next = new NodeAuthor(author, head.Next);
- }
- }
- // TODO (0.3): implement method by given header
- // Adds new element (author) to the END of singly linked list
- public void AddToEnd(Author author)
- {
- if (head == tailE)
- {
- head.Next = new NodeAuthor(author, tail);
- tailE = head.Next;
- }
- else
- {
- tailE.Next = new NodeAuthor(author, tail);
- tailE = tailE.Next;
- }
- }
- //-------------------------------------------------------
- // Interface methods for navigation
- //-------------------------------------------------------
- // TODO (0.1): implement method by given header
- // Start of the list is assigned to the interface pointer
- public void Start()
- {
- if (head == tailE)
- iP = null;
- iP = head.Next;
- }
- // TODO (0.1): implement method by given header
- // Moves interface pointer to next element
- public void Next()
- {
- iP = iP.Next;
- }
- // TODO (0.1): implement method by given header
- // Returns true if the interface pointer is not empty;false - otherwise
- public bool Exists()
- {
- // TODO: implement method
- return iP != null && iP != tail;
- }
- // TODO (0.1): implement method by given header
- // Returns data of interface pointer in linked list
- public Author GetData()
- {
- //TODO: implement method
- if (iP == null)
- return null;
- return iP.Author;
- }
- // TODO (1): implement method by given header
- // Sorts elements by book price (in descending order) and name of book in alphabetic order
- // Adapts SelectionSort algorithm
- public void SelectionSort()
- {
- if (head == tailE)
- return;
- for (NodeAuthor n1 = head.Next; n1.Next != tail; n1 = n1.Next)
- {
- NodeAuthor max = n1;
- for (NodeAuthor n2 = n1.Next; n2 != tail; n2 = n2.Next)
- {
- if (n2.Author >= max.Author)
- max = n2;
- }
- Author temp = max.Author;
- max.Author = n1.Author;
- n1.Author = temp;
- }
- }
- // TODO (1): implement method by given header
- // Sorts elements by book price (in descending order) and name of book in alphabetic order
- // Adapts SelectionSort algorithm
- public void BubbleSort()
- {
- if (head == tailE)
- return;
- bool swap = true;
- while (swap)
- {
- swap = false;
- for (NodeAuthor temp = head.Next; temp.Next != tail; temp = temp.Next)
- {
- if (temp.Author <= temp.Next.Author)
- {
- swap = true;
- Author a = temp.Author;
- temp.Author = temp.Next.Author;
- temp.Next.Author = a;
- }
- }
- }
- }
- // TODO (1.5): implement method by given header
- // Returns the name of first most popular author and number of books published (bookCount)
- public string MostPopular(out int bookCount)
- {
- // TODO: implement method
- if (head == tailE)
- {
- bookCount = -1;
- return "Not found";
- }
- int max_cnt = 0;
- Author a = new Author();
- for (NodeAuthor n1 = head.Next; n1 != tail; n1 = n1.Next)
- {
- int cnt = 0;
- for (NodeAuthor n2 = head.Next; n2 != tail; n2 = n2.Next)
- {
- if (string.Compare(n1.Author.authorName, n2.Author.authorName) == 0)
- cnt++;
- }
- if (cnt > max_cnt)
- {
- max_cnt = cnt;
- a = n1.Author;
- }
- }
- bookCount = max_cnt;
- return a.authorName;
- }
- // TODO (1.5): implement method by the given header
- // Inserts data of author (stored in object Insertion) before element of author a
- public void InsertBefore(Author a, Author Insertion)
- {
- if (head == tailE)
- return;
- NodeAuthor temp = head.Next, prev = head;
- while (temp != tail && temp.Author != a)
- {
- prev = temp;
- temp = temp.Next;
- }
- if (temp == tail)
- return;
- else
- {
- prev.Next = new NodeAuthor(Insertion, temp);
- }
- }
- // TODO (1.5): implement method by the given header
- // Inserts data of author (stored in object Insertion) after element of author a
- public void InsertAfter(Author a, Author Insertion)
- {
- if (head == tailE)
- return;
- NodeAuthor temp = head.Next;
- while (temp != tail && temp.Author != a)
- temp = temp.Next;
- if (temp == null)
- return;
- if (temp == tailE)
- {
- tailE.Next = new NodeAuthor(Insertion, tail);
- tailE = tailE.Next;
- }
- else
- {
- temp.Next = new NodeAuthor(Insertion, temp.Next);
- }
- }
- public void InsertToSorted(Author a)
- {
- if (head == tailE)
- {
- head.Next = new NodeAuthor(a, tail);
- tailE = head.Next;
- return;
- }
- if (a >= head.Next.Author)
- {
- head.Next = new NodeAuthor(a, head.Next);
- return;
- }
- NodeAuthor temp = head.Next;
- while (temp.Next != tail && tail.Author <= a)
- temp = temp.Next;
- if (temp == tailE)
- {
- tailE.Next = new NodeAuthor(a, tail);
- tailE = tailE.Next;
- }
- else
- {
- temp.Next = new NodeAuthor(a, temp.Next);
- }
- }
- // TODO (1.5): implement method by the given header
- // Removes data of author (stored in object Insertion)
- public void Remove(Author a)
- {
- if (head == tailE)
- return;
- NodeAuthor temp = head.Next, prev = head;
- while (temp != tail && temp.Author != a)
- {
- prev = temp;
- temp = temp.Next;
- }
- if (temp == tail)
- return;
- else
- {
- if (temp == tailE)
- {
- prev.Next = temp.Next;
- tailE = prev;
- }
- else
- {
- prev.Next = temp.Next;
- }
- }
- }
- }
- class Program
- {
- const string CFd = "Authors.txt";
- static void Main(string[] args)
- {
- // TODO (1): Add required commands
- // All results are presented to CONSOLE
- // Create container A
- LinkedListOfAuthors A = new LinkedListOfAuthors();
- Read(CFd, A);
- Print("Initial container A:", A);
- Author temp = new Author();
- temp.authorName = "Inserted";
- Author temp1 = new Author();
- temp1.authorName = "Inserted1";
- //********************************************
- // INSERT BEFORE
- //********************************************
- //A.InsertBefore(A.Middle(), temp);
- //A.InsertBefore(A.First(), temp);
- //A.InsertBefore(A.First(), temp1);
- //A.InsertBefore(A.Last(), temp);
- //Print("Insert before operation testing:", A);
- //********************************************
- // INSERT AFTER
- //********************************************
- //A.InsertAfter(A.Middle(), temp);
- //A.InsertAfter(A.First(), temp);
- //A.InsertAfter(A.Last(), temp1);
- //A.InsertAfter(A.Last(), temp1);
- //Print("Insert after operation testing:", A);
- //********************************************
- // INSERT IN SORTED
- //********************************************
- //A.SelectionSort();
- //A.BubbleSort();
- //temp.Price = 0;
- //temp.Price = 100;
- //temp.Price = 5.55;
- //A.InsertToSorted(temp);
- //Print("Sorted container A:", A);
- //********************************************
- // INSERT ALL
- //********************************************
- //A.InsertAll(A.First(), temp);
- //Print("Insert all operation testing:", A);
- //********************************************
- // REMOVE
- //********************************************
- //A.InsertBefore(A.Middle(), temp);
- //A.InsertBefore(A.First(), temp);
- //A.InsertBefore(A.Last(), temp);
- //A.Remove(temp);
- //A.Remove(temp);
- //A.Remove(temp);
- //A.InsertAfter(A.Middle(), temp);
- //A.InsertAfter(A.First(), temp);
- //A.InsertAfter(A.First(), temp1);
- //A.InsertAfter(A.Last(), temp);
- //Print("Remove operation testing:", A);
- //********************************************
- // REMOVE ALL
- //********************************************
- //A.InsertBefore(A.Middle(), temp);
- //A.InsertBefore(A.First(), temp);
- //A.InsertBefore(A.Last(), temp);
- //A.RemoveAll(temp);
- //A.InsertBefore(A.Middle(), temp);
- //A.InsertBefore(A.First(), temp);
- //A.InsertBefore(A.First(), temp1);
- //A.InsertBefore(A.Last(), temp);
- //Print("Remove all operation testing:", A);
- }
- // Reads initial data from file fv to container A by creating a direct singly linked list
- // METHOD IS ALREADY IMPLEMENTED
- static void Read(string fv, LinkedListOfAuthors A)
- {
- using (var file = new StreamReader(fv))
- {
- string mLine;
- while ((mLine = file.ReadLine()) != null)
- {
- string[] parts = mLine.Split(';'); // line fragments (parts)
- string authorName = parts[0].Trim();
- string bookName = parts[1].Trim();
- Gender gender;
- Enum.TryParse(parts[2], out gender);
- string pHouse = parts[3].Trim();
- double Price = Convert.ToDouble(parts[4]);
- Author author = new Author(authorName, bookName, gender, pHouse, Price);
- A.AddToEnd(author);
- }
- }
- }
- // TODO (1): implement method by given header
- // Prints elements of container A to console using table format;
- // header – label (note) above the table
- // Method is implemented in class Program
- static void Print(string header, LinkedListOfAuthors A)
- {
- if (A.Count() == 0)
- {
- Console.WriteLine("The container is empty!");
- return;
- }
- Console.WriteLine(header);
- Console.WriteLine(new string('-', 50));
- Console.WriteLine("{0, -20} {1, -8} {2, -8} {3, -10} {4, -8}", "Name and Surname", "Book", "Gender", "House", "Price");
- Console.WriteLine(new string('-', 50));
- for (A.Start(); A.Exists(); A.Next())
- Console.WriteLine(A.GetData().ToString());
- Console.WriteLine(new string('-', 50));
- }
- // TODO (1): implement method by given header
- // Creates a new container B (reverse linked list, deep copy) of authors of specified publishing house p
- // Uppercase and lowercase letters in filter p are treated as the same (case-insensitive search)
- // Method is implemented in class Program
- static void Create(LinkedListOfAuthors A, LinkedListOfAuthors B, string p)
- {
- for (A.Start(); A.Exists(); A.Next())
- {
- if (string.Compare(A.GetData().publishingHouse, p, StringComparison.OrdinalIgnoreCase) == 0)
- B.AddToFront(A.GetData());
- }
- }
- // TODO (1): implement method by given header
- // Finds and returns last author (null if not found) for specified gender
- // Method is implemented in class Program
- static Author Find(LinkedListOfAuthors B, Gender gender)
- {
- Author temp = null;
- for (B.Start(); B.Exists(); B.Next())
- {
- if (B.GetData().Gender == gender)
- {
- temp = B.GetData();
- }
- }
- return temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement