I am writing a term paper and am having trouble checking it. The point of this work is to create a circular two-way array of Client class objects. I sent it to the manager, and he said that some aspects of the program are not correct. He said:
Why does the Сlient class use an array of references private Operation[] operations; instead of an array of objects of the Operation class?
Why is the Bank class using an address list private DoublyLinkedList<Client> arrayClient; instead of an array-queue?
// Operation class
using System;
namespace DepositLibrary
{
// Класс операции
public class Operation
{
// Дата операции
public DateTime date;
// Депозит операции
public decimal deposit;
public Operation(DateTime dateIn, decimal depositIn)
{
date = dateIn;
deposit = depositIn;
}
// Получение даты операции
public DateTime GetDateTime { get => date; }
// Получение депозита операции
public decimal GetDeposit { get => deposit; }
}
}
// Client class
using System;
using System.Collections.Generic;
namespace DepositLibrary
{
//Класс клиент
public class Client
{
//Фамилия клиента
public string surname;
//Текущий депозит лицевого счета
public decimal currentDeposit;
// Совершенные операции клиента
private Operation[] operations;
public Client(string surnameIn)
{
surname = surnameIn;
operations = new Operation[0];
}
//Получить фамилию
public string GetSurname { get => surname; }
//Получить все совершенные операции
public Operation[] Operations { get => operations; }
//Получить текущий депозит лицевого счета
public decimal GetDeposit { get => currentDeposit; }
}
}
// Bank class
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DepositLibrary
{
// Класс банк
public class Bank
{
// Наименование банка
private string nameBank;
// Клиентские банковские книги
private DoublyLinkedList<Client> arrayClient;
public Bank(string _nameBank)
{
nameBank = _nameBank;
arrayClient = new DoublyLinkedList<Client>();
}
//Получить наименование банка
public string GetBankName { get => nameBank; }
//Получить клиентские банковские книги
public DoublyLinkedList<Client> GetListClients { get => arrayClient; }
}
public class DoublyNode<T>
{
public DoublyNode(T data)
{
Data = data;
}
public T Data { get; set; }
public DoublyNode<T> Previous { get; set; }
public DoublyNode<T> Next { get; set; }
}
public class DoublyLinkedList<T> : IEnumerable<T> // двусвязный список
{
public DoublyNode<T> head; // головной/первый элемент
public DoublyNode<T> tail; // последний/хвостовой элемент
int count; // количество элементов в списке
// добавление элемента
public void Add(T data)
{
DoublyNode<T> node = new DoublyNode<T>(data);
if (head == null)
head = node;
else
{
tail.Next = node;
node.Previous = tail;
// for looped array
node.Next = head;
head.Previous = node;
}
tail = node;
count++;
}
public void AddFirst(T data)
{
DoublyNode<T> node = new DoublyNode<T>(data);
DoublyNode<T> temp = head;
node.Next = temp;
head = node;
if (count == 0)
tail = head;
else
{
temp.Previous = node;
// for looped
tail.Next = node;
node.Previous = tail;
}
count++;
}
// удаление
public bool Remove(T data)
{
DoublyNode<T> current = head;
// поиск удаляемого узла
do
{
if (current.Data.Equals(data))
{
break;
}
current = current.Next;
} while (!current.Data.Equals(head.Data));
if (current != null)
{
// если узел не последний
if (!current.Next.Data.Equals(head.Data))
{
current.Next.Previous = current.Previous;
}
else
{
// если последний, переустанавливаем tail
tail = current.Previous;
// for looped
tail.Next = head;
}
// если узел не первый
if (!current.Previous.Data.Equals(tail.Data))
{
current.Previous.Next = current.Next;
}
else
{
// если первый, переустанавливаем head
head = current.Next;
// for looped
head.Previous = tail;
}
count--;
return true;
}
return false;
}
public int Count { get { return count; } }
public bool IsEmpty { get { return count == 0; } }
public void Clear()
{
head = null;
tail = null;
count = 0;
}
public bool Contains(T data)
{
DoublyNode<T> current = head;
do
{
if (current.Data.Equals(data))
return true;
current = current.Next;
} while (!current.Data.Equals(head.Data));
return false;
}
IEnumerator GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
DoublyNode<T> current = head;
do
{
yield return current.Data;
current = current.Next;
} while (!current.Data.Equals(head.Data));
}
public IEnumerable<T> BackEnumerator()
{
DoublyNode<T> current = tail;
do
{
yield return current.Data;
current = current.Previous;
} while (!current.Data.Equals(tail.Data));
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}
I tried for a very long time to understand what he wants from me, but didn't come to a normal result. I would be grateful for any help and advice. Thank you!
Related
I have the next class:
using System.Drawing;
using System.Windows.Forms;
namespace course_project
{
internal class Pike<T> : Fish
{
Brush brush = new SolidBrush(Color.Green);
public PictureBox aquarium = new AquariumForm().aquarium_picturebox;
public Pike(T data) {
Data = data;
Draw((int[])(object)data);
}
public T Data { get; set; }
public Pike<T> Next { get; set; }
protected override void Draw(int[] coordinates)
{
Graphics graphics = Graphics.FromImage(aquarium.Image);
graphics.FillEllipse(brush, coordinates[0], coordinates[1], 40, 40);
aquarium.Refresh();
}
}
}
Linked List:
using System.Collections;
using System.Collections.Generic;
namespace course_project
{
internal class PikeFlock<T> : IEnumerable<T>
{
Pike<T> head;
Pike<T> tail;
int count;
public void Add(T data)
{
Pike<T> pike = new Pike<T>(data);
if (head == null)
head = pike;
else
tail.Next = pike;
tail = pike;
count++;
}
public bool Remove(T data)
{
Pike<T> current = head;
Pike<T> previous = null;
while (current != null)
{
if (current.Data.Equals(data))
{
if (previous != null)
{
previous.Next = current.Next;
if (current.Next == null)
{
tail = previous;
}
}
else
{
head = head.Next;
if (head == null)
tail = null;
}
count--;
return true;
}
previous = current;
current = current.Next;
}
return false;
}
public int Count { get { return count; } }
public bool isEmpty { get { return count == 0; } }
public void Clear()
{
head = null;
tail = null;
count = 0;
}
public bool Contains(T data)
{
Pike<T> current = head;
while (current != null)
{
if (current.Data.Equals(data))
return true;
current = current.Next;
}
return false;
}
public void AppendFirst(T data)
{
Pike<T> pike = new Pike<T>(data);
pike.Next = head;
head = pike;
if (count == 0)
{
tail = head;
}
count++;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
Pike<T> current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
}
}
Button click event:
private void add_pike_button_Click(object sender, EventArgs e)
{
PikeFlock<int[]> pikeFlock = new PikeFlock<int[]>();
int[] coords = { 1, 2 };
pikeFlock.Add(coords);
}
Form1_Load:
private void Form1_Load(object sender, EventArgs e)
{
Bitmap bitmap = new Bitmap(aquarium_picturebox.Width, aquarium_picturebox.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Blue);
aquarium_picturebox.Image = bitmap;
aquarium_picturebox.Refresh();
}
But when you click the button an ArgumentNullException (System.ArgumentNullException: "The value cannot be undefined.
Parameter name: image".) is thrown because the Image property of the PictureBox element is empty for some reason.
How can it be solved? Please don't throw tomatoes, I just started to learn C# :)
I have my own template class, how to make the foreach loop work for it. I understand how to do this for a template class with one template type, but for two - I do not understand. I understand that you need to inherit from IEnumerable and IEnumerator, but I can't find a suitable example and don't understand what to do.
using System;
using System.Collections;
using System.Collections.Generic;
namespace CCS8
{
public class Node<KEY, VALUE>
{
public Node(KEY k, VALUE v)
{
this.k = k;
this.v = v;
}
public KEY k { get; set; }
public VALUE v { get; set; }
public Node<KEY, VALUE> Next { get; set; }
}
public class MyList<K, V>
{
Node<K, V> head;
Node<K, V> tail;
int count;
int possition = -1;
public bool MoveNext()
{
if (possition < count - 1)
{
possition++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
possition = -1;
}
public void Add(K k, V v)
{
Node<K, V> node = new Node<K, V>(k, v);
if (head == null)
head = node;
else
tail.Next = node;
tail = node;
count++;
}
public int Count { get { return count; } }
public V ValueByIndex(int i)
{
int j = 0;
Node<K, V> current = head;
while (j != i)
{
current = current.Next;
j++;
}
return current.v;
}
}
class Program
{
static void Main(string[] args)
{
MyList<int, String> l = new MyList<int, String>();
// добавление элементов
l.Add(1, "Line1");
l.Add(2, "Line2");
l.Add(3, "Line3");
l.Add(4, "Line4");
Console.WriteLine("3th elemnt: " + l.ValueByIndex(2));
Console.WriteLine("Elements count: " + l.Count);
foreach (var i in l)
{
Console.WriteLine(i);
}
}
}
}
I am looking for a job. Periodically I come across the requirement to perform some kind of test task. Recently, I received a reject (which in itself is not critical for me) based on a test solution. Unfortunately, they did not provide feedback to me, so I decided to write here in an attempt to figure out the reason for the rejection and the correctness of the formulation of the tasks themselves. Personally, I have suspicions regarding part 3. Task text:
Original task text
Написать функцию, которая преобразует строку с римским числом в целое (иными словами, написать тело функции public int RomanToInt(string s)). Римское число не больше 3000.
Проверить сбалансированность скобочной структуры в произвольном выражении ((1+3)()(4+(3-5)))
Реализовать двусвязный список и написать функцию, переворачивающую его, т.е. изменяющую порядок элементов на обратный.
public interface DoubleLinkedListNode<T>
{
T Value { get; set; }
DoubleLinkedNode<T> Next { get; set; }
DoubleLinkedNode<T> Prev { get; set; }
}
public interface DoubleLinkedList<T>
{
DoubleLinkedNode<T> First { get; set; }
DoubleLinkedNode<T> Last { get; set; }
void Reverse();
//insert new DoubleLinkedListNode with given value at the start of the list
void AddFirst(T value);
//insert new DoubleLinkedListNode with given value at the end of the list
void AddLast(T value);
}
Все решения должны быть сопровождены юнит-тестами.
Task translation
Write a function that converts a string with a Roman number to an integer (in other words, write the body of a function public int RomanToInt (string s)). Roman number no more than 3000.
Check the balance of the bracket structure in an arbitrary expression ((1 + 3) () (4+ (3-5)))
Implement a doubly linked list and write a function that reverses it, i.e. reverse the order of elements.
The third part caused me dissonance, first of all, by non-compliance with the convention. In addition, in the text of the assignment there are no points that would regulate the use of these interfaces in the required solution. Just a piece of code and that’s it.
I have a question for you how these interfaces can be used in the implementation of doubly linked lists and what advantages this can give (in comparison with more common solutions). Maybe I'm not so strong in OOD, but I personally only think of circumventing the limitations of lack of multiple inheritance in C#.
My solution
public class DoublyNode<T>
{
public DoublyNode()
{}
public DoublyNode(T data)
{
Data = data;
}
public T Data { get; set; }
public DoublyNode<T> Previous { get; set; }
public DoublyNode<T> Next { get; set; }
}
public class DoublyLinkedList<T> : IEnumerable<T>
{
private DoublyNode<T> head;
private DoublyNode<T> tail;
private int count;
public void AddLast(T data)
{
DoublyNode<T> node = new DoublyNode<T>(data);
if (head == null)
{
head = node;
}
else
{
tail.Next = node;
node.Previous = tail;
}
tail = node;
count++;
}
public void AddFirst(T data)
{
DoublyNode<T> node = new DoublyNode<T>(data);
DoublyNode<T> temp = head;
node.Next = temp;
head = node;
if (count == 0)
{
tail = head;
}
else
{
temp.Previous = node;
}
count++;
}
public bool Remove(T data)
{
DoublyNode<T> current = head;
while (current != null)
{
if (current.Data.Equals(data))
{
break;
}
current = current.Next;
}
if (current != null)
{
if (current.Next != null)
{
current.Next.Previous = current.Previous;
}
else
{
tail = current.Previous;
}
if (current.Previous != null)
{
current.Previous.Next = current.Next;
}
else
{
head = current.Next;
}
count--;
return true;
}
return false;
}
public int Count { get { return count; } }
public bool IsEmpty { get { return count == 0; } }
public void Clear()
{
head = null;
tail = null;
count = 0;
}
public bool Contains(T data)
{
DoublyNode<T> current = head;
while (current != null)
{
if (current.Data.Equals(data))
{
return true;
}
current = current.Next;
}
return false;
}
public void Reverse()
{
var buffer = head;
head = tail;
tail = buffer;
DoublyNode<T> current;
current = head;
while (current != null)
{
current.Next = buffer;
current.Next = current.Previous;
current.Previous = buffer;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
DoublyNode<T> current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
public IEnumerable<T> BackEnumerator()
{
DoublyNode<T> current = tail;
while (current != null)
{
yield return current.Data;
current = current.Previous;
}
}
}
Hi guys help me please.
I wrote the code for a binary search tree, but for some reason I'm wildly stupid in post-traverse and searching. Can someone help with writing a postorder and search.
Thank you and sorry for bad english. Thank you very much again.
I don't show you my Remove and Contains method because it does not important. If you want to see them , i can show it.
My BinaryTreeNode class
public class BinaryTreeNode<T> : IComparable<T>
where T : IComparable
{
public BinaryTreeNode(T value)
{
Value = value;
}
public BinaryTreeNode<T> Left { get; set; }
public BinaryTreeNode<T> Right { get; set; }
public T Value { get; }
public int CompareTo(T other)
{
return Value.CompareTo(other);
}
public IEnumerator<T> GetEnumerator()
{
var leftEnumerable = (IEnumerable<T>) Left ?? new T[0];
var rightEnumerable = (IEnumerable<T>) Right ?? new T[0];
return leftEnumerable.Concat(new[] {Value})
.Concat(rightEnumerable)
.GetEnumerator();
}
}
My BinaryTree class :
public class BinaryTree<T>
where T : IComparable
{
private BinaryTreeNode<T> _head;
public int Count { get; private set; }
public void Add(T value)
{
if (_head == null)
_head = new BinaryTreeNode<T>(value);
else
AddTo(_head, value);
Count++;
}
private void AddTo(BinaryTreeNode<T> node, T value)
{
if (value.CompareTo(node.Value) < 0)
{
if (node.Left == null)
node.Left = new BinaryTreeNode<T>(value);
else
AddTo(node.Left, value);
}
else
{
if (node.Right == null)
node.Right = new BinaryTreeNode<T>(value);
else
AddTo(node.Right, value);
}
}
public IEnumerable<T> Preorder(BinaryTreeNode<T> root)
{
var stack = new Stack<BinaryTreeNode<T>>();
stack.Push(root);
while (stack.Count > 0)
{
var node = stack.Pop();
yield return node.Value;
if (node.Right != null)
stack.Push(node.Right);
if (node.Left != null)
stack.Push(node.Left);
}
}
public IEnumerable<T> InOrder(BinaryTreeNode<T> root)
{
var stack = new Stack<BinaryTreeNode<T>>();
while (root != null)
{
while (root.Left != null)
{
stack.Push(root);
root = root.Left;
}
yield return root.Value;
while (root.Right == null && stack.Count > 0)
{
root = stack.Pop();
yield return root.Value;
}
root = root.Right;
}
}
public IEnumerator<T> GetEnumerator()
{
return _head.GetEnumerator();
}
public void Clear()
{
_head = null;
Count = 0;
}
}
I have a custom queue class that is implemented via linked list, but I can't figure out how ot implement IEnumerable for something that is not array.
It's easy to implement it using Dequeue(), but I don't want enumeration to mutate my collection.
Here is my code for DQueue class:
class DQueue<Item> : IEnumerable<Item>
{
private Node<Item> startNode;
private Node<Item> lastNode;
private int _size;
public DQueue()
{
_size = 0;
}
public void Enqueue(Item item)
{
_size++;
if (startNode == null) {
startNode = new Node<Item>();
startNode.data = item;
lastNode = startNode;
} else {
Node<Item> temp = new Node<Item>();
temp.data = item;
lastNode.next = temp;
lastNode = temp;
}
}
public Item Dequeue()
{
Item temp = startNode.data;
startNode = startNode.next;
_size--;
return temp;
}
public bool IsEmpty()
{
return startNode == null;
}
public int Size()
{
return _size;
}
private class Node<InnerItem>
{
public InnerItem data;
public Node<InnerItem> next;
public Node()
{
next = null;
}
}
// IEnumerable
public IEnumerator<Item> GetEnumerator()
{
Node<Item> current;
for (int i = 0; i < _size; i++) {
//yield return values[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Suggest next solve, iteration from start node:
public IEnumerator<Item> GetEnumerator()
{
for (Node<Item> item = startNode; item != null; item = item.next)
{
yield return item.data;
}
}