Стек, с информационными полями - C#
Формулировка задачи:
Не удаётся поместить в стек название гор и её высоту, скажите пожалуйста что я делаю не так? Выручайте, заранее спасибо.
Задание:Создать стек, информационными полями которого являются: название горы и высота. Добавить в стек сведения о новой горе. Организовать просмотр данных стека и определить среднюю высоту гор.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
struct ITm { }
class Program
{
static void Main()
{
int[] a = new int[5];
int[] b = new int[5];
var MyStack = new Stack<ITm>();
MyStack.Push(new ITm,Название, высота);
MyStack.Push('N');
MyStack.Push('X');
Random ran = new Random();
for (int i = 0; i < 5; i++)
{
a[i] = ran.Next(1000,8848);
b[i] = a[i];
// c[i] = a[i];
Console.Write("{0}\n", a[i]);
}
Console.WriteLine("Исходный стек: ");
foreach (char s in MyStack)
Console.Write(s);
Console.WriteLine("\n");
while (MyStack.Count > 0)
{
Console.WriteLine("Название горы -> {0}", MyStack.Pop());
Console.Write(" Высота горы ->{0}\n", b[i]);
}
if (MyStack.Count == 0)
Console.WriteLine("\nСтек пуст!");
Console.ReadLine();
}
}
}Решение задачи: «Стек, с информационными полями»
textual
Листинг программы
using System;
using System.Collections.Generic;
namespace Mat
{
class Hill
{
private string name;
public int height;
public Hill(string name,int height)
{
this.name = name;
this.height = height;
}
public override string ToString()
{
return name + " " + height+ " метров";
}
}
class Program
{
static void Main()
{
Stack<Hill>hills=new Stack<Hill>();
hills.Push(new Hill("Гора 1 ",1000));
hills.Push(new Hill("Гора 2 ",2000));
double s = 0;
foreach (var a in hills)
{
s += a.height;
Console.WriteLine(a);
}
Console.WriteLine(s/hills.Count);
}
}
}