- •Введение
- •Список вопросов, рассматриваемых в модуле 1.
- •Продемонстрированные в приложении особенности
- •Пример Mod1Ex4 (Инкапсуляция, обработка ошибок, наследование) Постановка задачи
- •Продемонстрированные в приложении особенности
- •Пример Mod1Ex5 (Полиморфизм)
- •Продемонстрированные в приложении особенности
- •Список вопросов, рассматриваемых в модуле 2.
- •Описание модельных примеров модуля 2. Пример Mod2Ex1 Постановка
- •Листинги примеров Пример Mod1Ex1
- •Пример Mod1Ex2
- •Пример Mod1Ex3
- •Пример Mod1Ex4
- •Пример Mod2Ex1
- •Пример Mod2Ex2
- •Пример Mod2Ex3
Пример Mod1Ex2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Module1Example02
{
class Program
{
static int[] x1, y1, x2, y2;
static void Main(string[] args)
{
InitGeneration(5);
ShowAllRectangles();
Console.ReadLine();
}
static void InitGeneration(int rectangleCount)
{
x1=new int[rectangleCount];
x2=new int[rectangleCount];
y1=new int[rectangleCount];
y2=new int[rectangleCount];
int maxX = Console.WindowWidth;
int maxY = Console.WindowHeight;
Random rnd = new Random();
for(int i=0;i<rectangleCount;i++)
{
x1[i] = rnd.Next(0, maxX);
x2[i] = rnd.Next(0, maxX);
y1[i] = rnd.Next(0, maxY);
y2[i] = rnd.Next(0, maxY);
}
}
static void ShowRectangle(int x1, int y1, int x2, int y2, char ch)
{
int _x1 = Math.Min(x1, x2);
int _x2 = Math.Max(x1, x2);
int _y1 = Math.Min(y1, y2);
int _y2 = Math.Max(y1, y2);
Console.SetCursorPosition(_x1, _y1);
for (int i = _x1; i <= _x2; i++) Console.Write(ch);
Console.SetCursorPosition(_x1, _y2);
for (int i = _x1; i <= _x2; i++) Console.Write(ch);
for (int i = _y1; i <= _y2; i++)
{
Console.SetCursorPosition(_x1, i);
Console.Write(ch);
Console.SetCursorPosition(_x2, i);
Console.Write(ch);
}
}
static void ShowAllRectangles()
{
for (int i = 0; i < x1.Length; i++)
ShowRectangle(x1[i], y1[i], x2[i], y2[i], '*');
}
}
}
Пример Mod1Ex3
using System;
using System.Collections.Generic;
using System.Text;
namespace Classes
{
public class Cafe
{
public CafeClient[] places;
public Cafe(int nPlaces) { places = new CafeClient[nPlaces]; }
public void TakeOrder(int clientPlace)
{
Random r = new Random();
if (!places[clientPlace].MakeOrder(r.Next(10, 100)))
places[clientPlace] = null;
}
public bool TakeClient(CafeClient cl)
{
bool accepted = false;
for (int i = 0; i < places.Length; i++)
{
if (places[i] == null)
{ places[i] = cl; accepted = true; break; }
}
return accepted;
}
public bool HasEmptyPlaces()
{
bool found = false;
for (int i = 0; i < places.Length; i++)
{ if (places[i] == null) found = true; break; }
return found;
}
public string ToString()
{
string res = "=========================\n";
for (int i = 0; i < places.Length; i++)
{
if (places[i] != null)
res += String.Format("Стол {0}: ", i) + places[i].ToString() + "\n";
}
return res;
}
}
public class CafeClient
{
public string name;
public int money;
public CafeClient(string n, int m) { name = n; money = m; }
public bool MakeOrder(int price)
{
if (price <= money)
{
money -= price;
return true;
}
else
{ return false; }
}
public string ToString()
{
return String.Format("У клиента {0} осталось {1} денег.", name, money);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Classes;
namespace KR2_CafeAndClients
{
class Program
{
static void Main(string[] args)
{
Random r=new Random();
CafeClient[] clients = new CafeClient[10]; //массив из 10 клиентов с именами client1, client2,...,client10
int i;
for(i=0; i<clients.Length; i++)
clients[i]=new CafeClient("client"+i, r.Next(20,200));
Cafe cafe = new Cafe(5);
for (i = 0; i< clients.Length; i++)
{
if (!cafe.TakeClient(clients[i]))
break;
}
Console.WriteLine(cafe.ToString());
while (!cafe.HasEmptyPlaces())
{
cafe.TakeOrder(0);
Console.WriteLine(cafe.ToString());
}
cafe.TakeClient(clients[clients.Length-1]);
Console.WriteLine(cafe.ToString());
}
}
}
