Last active
October 3, 2020 16:14
-
-
Save sashaqwert/cda3ed74cf67504aeb3e95e92409f81f to your computer and use it in GitHub Desktop.
БКИТ ЛР №3. Не получается создать объект SimpleStack<Figure>. В него нужно добавить несколько фигур, вывести, удалить 1 и вывести ещё раз.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
* Копия файла из проекта LAB_3 | |
*/ | |
namespace LAB_3 | |
{ | |
public interface IPrint | |
{ | |
void Print(); | |
} | |
public abstract class Figure : IComparable | |
{ | |
public int CompareTo(object obj) | |
{ | |
if (obj == null) return 1; | |
Figure f = obj as Figure; | |
double a = ploshad(); | |
double b = f.ploshad(); | |
return a.CompareTo(b); | |
} | |
public abstract double ploshad(); | |
} | |
public class Rectangle : Figure, IPrint //Прямоугольник | |
{ | |
protected double a, b; | |
public Rectangle(double A, double B) | |
{ | |
a = A; | |
b = B; | |
} | |
public override double ploshad() | |
{ | |
return a * b; | |
} | |
public override string ToString() | |
{ | |
string result = ""; | |
result += "Длинна = "; | |
result += a.ToString() + " \n"; | |
result += "Ширина = "; | |
result += b.ToString() + " \n"; | |
result += "Площадь = "; | |
result += ploshad().ToString() + " \n"; | |
return result; | |
} | |
public void Print() | |
{ | |
Console.WriteLine(ToString()); | |
} | |
} | |
public class Square : Rectangle //Квадрат | |
{ | |
public Square(double A) : base(A, A) | |
{ | |
} | |
public override string ToString() | |
{ | |
string result = ""; | |
result += "Сторона = "; | |
result += a.ToString() + " \n"; | |
result += "Площадь = "; | |
result += ploshad().ToString() + " \n"; | |
return result; | |
} | |
public new void Print() //new убирает предупреждение о скрытии | |
{ | |
Console.WriteLine(ToString()); | |
} | |
} | |
public class Round : Figure //Круг | |
{ | |
private double r; | |
public Round(double R) | |
{ | |
r = R; | |
} | |
public override double ploshad() | |
{ | |
return Math.PI * r * r; | |
} | |
public override string ToString() | |
{ | |
string result = ""; | |
result += "Радиус = "; | |
result += r.ToString() + " \n"; | |
result += "Площадь = "; | |
result += ploshad().ToString() + " \n"; | |
return result; | |
} | |
public void Print() | |
{ | |
Console.WriteLine(ToString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LAB_3; | |
using System; | |
namespace SimpleListProject | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); Console.WriteLine("Чиварзин А. Е. ИУ5Ц-52Б"); | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine("Проект SimpleListProject [3/3]"); | |
Console.ResetColor(); | |
SimpleStack<Figure> fList = new SimpleStack<Figure>(); //ОШИБКА CS0311 Тип "LAB_3.Figure" не может быть использован как параметр типа "T" в универсальном типе или методе "SimpleStack<T>". Нет преобразования неявной ссылки из "LAB_3.Figure" в "SimpleListProject.SimpleListItem<LAB_3.Figure>". | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace SimpleListProject | |
{ | |
public class SimpleListItem<T> | |
{ | |
/// <summary> | |
/// Данные | |
/// </summary> | |
public T data { get; set; } | |
/// <summary> | |
/// Следующий элемент | |
/// </summary> | |
public SimpleListItem<T> next { get; set; } | |
///конструктор | |
public SimpleListItem(T param) | |
{ | |
this.data = param; | |
} | |
} | |
public class SimpleList<T> : IEnumerable<T> where T : IComparable | |
{ | |
/// <summary> | |
/// Первый элемент списка | |
/// </summary> | |
protected SimpleListItem<T> first = null; | |
/// <summary> | |
/// Последний элемент списка | |
/// </summary> | |
protected SimpleListItem<T> last = null; | |
/// <summary> | |
/// Количество элементов | |
/// </summary> | |
public int Count | |
{ | |
get { return _count; } | |
protected set { _count = value; } | |
} | |
int _count; | |
/// <summary> | |
/// Добавление элемента | |
/// </summary> | |
public void Add(T element) | |
{ | |
SimpleListItem<T> newItem = | |
new SimpleListItem<T>(element); | |
this.Count++; | |
//Добавление первого элемента | |
if (last == null) | |
{ | |
this.first = newItem; this.last = newItem; | |
} | |
//Добавление следующих элементов | |
else | |
{ | |
//Присоединение элемента к цепочке | |
this.last.next = newItem; | |
//Присоединенный элемент считается последним | |
this.last = newItem; | |
} | |
} | |
/// <summary> | |
/// Чтение контейнера с заданным номером | |
/// </summary> | |
public SimpleListItem<T> GetItem(int number) | |
{ | |
if ((number < 0) || (number >= this.Count)) | |
{ | |
//Можно создать собственный класс исключения | |
throw new Exception("Выход за границу индекса"); | |
} | |
SimpleListItem<T> current = this.first; int i = 0; | |
//Пропускаем нужное количество элементов | |
while (i < number) | |
{ | |
//Переход к следующему элементу | |
current = current.next; | |
//Увеличение счетчика | |
i++; | |
} | |
return current; | |
} | |
/// <summary> | |
/// Чтение элемента с заданным номером | |
/// </summary> | |
public T Get(int number) | |
{ | |
return GetItem(number).data; | |
} | |
/// <summary> | |
/// Для перебора коллекции | |
/// </summary> | |
public IEnumerator<T> GetEnumerator() | |
{ | |
SimpleListItem<T> current = this.first; | |
//Перебор элементов | |
while (current != null) | |
{ | |
//Возврат текущего значения | |
yield return current.data; | |
//Переход к следующему элементу | |
current = current.next; | |
} | |
} | |
/// <summary> | |
/// Для перебора коллекции | |
/// </summary> | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
SimpleListItem<T> current = this.first; | |
//Перебор элементов | |
while (current != null) | |
{ | |
//Возврат текущего значения | |
yield return current.data; | |
//Переход к следующему элементу | |
current = current.next; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace SimpleListProject | |
{ | |
public class SimpleStack<T> : SimpleList<T> where T : SimpleListItem<T>, IComparable | |
{ | |
public SimpleStack () { } | |
public void push(T t) | |
{ | |
Add(t); | |
} | |
public T pop() | |
{ | |
return (T)last; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment