Доброго времени суток! Как сгенерировать числа для стека. Функция Generate - ввыводит, но потом не могу отобразить ее через Display()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
interface IStack {
Boolean Empty();
object Generate(object element);
object Pop();
object Push(object elements);
object Peek();
void Display();
void DisplayAll();
}
class Carriage {
public int CarriageNumber {
get; protected set;
}
public string CarriageType {
get; protected set;
}
public Carriage(string type, int number) {
this.CarriageNumber = number;
this.CarriageType = type;
}
public override string ToString() {
return CarriageNumber.ToString() + "/" + CarriageType;
}
}
class Stack : IStack {
int StackSize;
int top;
object[] item;
public int StackSizeProperty {
get { return StackSize; }
set { StackSize = value; }
}
public Stack() {
StackSizeProperty = 10;
item = new object[StackSize];
top = 0;
}
public Stack(int capacity) {
StackSizeProperty = capacity;
item = new object[StackSizeProperty];
top = 0;
}
public bool Empty() {
if (top == 0) {
return true;
}
return false;
}
public object Generate(object element) {
Random rnd = new Random();
Console.WriteLine("Enter quantity of items: ");
int qty = int.Parse(Console.ReadLine());
int[] item = new int[qty];
for (int elements = 0; elements < qty; elements++) {
item[elements] = rnd.Next(1, 100);
Console.WriteLine("Item " + elements + ": " + item[elements]);
}
return "Items have been generated.";
}
public void Display() {
for (int elements = top; elements > 0; elements--) {
Console.WriteLine("Item " + elements + ": " + item[elements]);
}
}
public object Push(object elements) {
if (top == StackSize) {
Console.WriteLine("Stack is full.\n");
return "Stack is full.";
}
else {
item[++top] = elements;
Console.WriteLine("Item was been pushed.\n");
return "item was been pushed.";
}
}
public void Entering(string CarriageType, int CarriageNumber) {
Console.Write("\n\tEnter the type of train: ");
CarriageType = Console.ReadLine();
Console.Write("\tEnter the number of train: ");
CarriageNumber = int.Parse(Console.ReadLine());
}
public object Pop() {
if (Empty()) {
Console.WriteLine("Stack is empty.\n");
return "Nothing.";
}
else {
return item[top--];
}
}
public object Peek() {
if (Empty()) {
Console.WriteLine("Stack is empty.\n");
return "Nothing.";
}
else {
return item[top];
}
}
public void DisplayAll() {
Console.WriteLine("All items: ");
for (int elements = top; elements > 0; elements--) {
Console.WriteLine("\t" + item[elements]);
}
}
public void Exit() {
do{ } while (Console.ReadKey().Key != ConsoleKey.Escape);
}
public int Entering() {
Console.Write("\tEnter the number of train: ");
return int.Parse(Console.ReadLine());
}
}
class MainProgramStacks {
static void Main() {
Stack FullStack = new Stack();
Stack AtypeStack = new Stack();
Stack BtypeStack = new Stack();
int mm_choice = 0, type_choice = 0, peek_choice = 0,
rmv_choice = 0, display_choice = 0, qty = 0,
CarriageNumberA = 0, CarriageNumberB = 0;
while (true) {
Console.WriteLine("- -- --- - Menu - --- -- -");
Console.WriteLine("- 1 - Add an element -");
Console.WriteLine("- 2 - Peek top element -");
Console.WriteLine("- 3 - Remove top element -");
Console.WriteLine("- 4 - Display stacks -");
Console.WriteLine("- -- --- - -- - --- -- -");
mm_choice = int.Parse(Console.ReadLine());
switch (mm_choice) {
case 1:
Console.Clear();
while (type_choice != 4) {
Console.WriteLine("- 1 - Add an element to type A: ");
Console.WriteLine("- 2 - Add an element to type B: ");
Console.WriteLine("- 3 - Generate: ");
Console.WriteLine("- 4 - Exit ");
type_choice = int.Parse(Console.ReadLine());
switch (type_choice) {
case 1:
AtypeStack.Push(new Carriage("a", AtypeStack.Entering()));
break;
case 2:
BtypeStack.Push(new Carriage("b", AtypeStack.Entering()));
break;
case 3:
AtypeStack.Generate(new Carriage("a", qty));
break;
}
}
break;
case 2:
Console.Clear();
while (peek_choice != 3) {
Console.WriteLine("- 1 - Peek top element of A type: ");
Console.WriteLine("- 2 - Peek top element of B type: ");
Console.WriteLine("- 3 - Exit ");
peek_choice = int.Parse(Console.ReadLine());
switch (peek_choice) {
case 1:
AtypeStack.Peek();
break;
case 2:
BtypeStack.Peek();
break;
}
}
break;
case 3:
Console.Clear();
while (rmv_choice != 3) {
Console.WriteLine("- 1 - Remove element from type A: ");
Console.WriteLine("- 2 - Remove element from type B: ");
Console.WriteLine("- 3 - Exit -");
rmv_choice = int.Parse(Console.ReadLine());
switch (rmv_choice) {
case 1:
AtypeStack.Pop();
break;
case 2:
BtypeStack.Pop();
break;
}
}
FullStack.Pop();
break;
case 4:
Console.Clear();
while (display_choice != 4) {
Console.WriteLine("- 1 - Display elements of type A: ");
Console.WriteLine("- 2 - Display elements of type B: ");
Console.WriteLine("- 3 - Display all elements: ");
Console.WriteLine("- 4 - Exit -");
display_choice = int.Parse(Console.ReadLine());
switch (display_choice) {
case 1:
AtypeStack.Display();
break;
case 2:
BtypeStack.Display();
break;
case 3:
AtypeStack.DisplayAll();
BtypeStack.DisplayAll();
break;
}
}
break;
}
}
}
}
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Имеется ComboBox, который хранит месяца года в текстовом форматеКак можно записать номер месяца в DataTime?
ЗдравствуйтеУ меня есть вопрос, возможно ли вызвать конструктор объекта, если точно не знаешь какого он типа? От одного базового класса наследуются...