Загрузка данных
Лабораторная работа №6 – Строки (Задание 3)
```csharp
using System;
class Program
{
static void Main()
{
Console.Write("Введите строку: ");
string input = Console.ReadLine();
int punctuationCount = 0;
foreach (char c in input)
{
if (c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':' ||
c == '-' || c == '(' || c == ')' || c == '"' || c == '\'' || c == ' ')
{
punctuationCount++;
}
}
Console.WriteLine($"Количество знаков препинания: {punctuationCount}");
}
}
```
---
Лабораторная работа №7 – Классы (Задание 3.1)
```csharp
using System;
class Car
{
private string brand;
private string model;
private string number;
public Car(string brand, string model, string number)
{
this.brand = brand;
this.model = model;
this.number = number;
}
public void Start()
{
Console.WriteLine($"Машина {brand} {model} завелась");
}
public void Stop()
{
Console.WriteLine($"Машина {brand} {model} остановилась");
}
public void ShowInfo()
{
Console.WriteLine($"Марка: {brand}, Модель: {model}, Номер: {number}");
}
}
class Program
{
static void Main()
{
Car myCar = new Car("Toyota", "Camry", "A123BC");
myCar.ShowInfo();
myCar.Start();
myCar.Stop();
Car secondCar = new Car("BMW", "X5", "X777XX");
secondCar.ShowInfo();
secondCar.Start();
secondCar.Stop();
}
}
```
---
Простейшие классы – Вариант 3 (Треугольник)
```csharp
using System;
class Triangle
{
private double x1, y1, x2, y2, x3, y3;
public Triangle()
{
x1 = 0; y1 = 0;
x2 = 1; y2 = 0;
x3 = 0; y3 = 1;
}
public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
SetTriangle(x1, y1, x2, y2, x3, y3);
}
public double X1 => x1;
public double Y1 => y1;
public double X2 => x2;
public double Y2 => y2;
public double X3 => x3;
public double Y3 => y3;
private double SideA()
{
return Math.Sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
}
private double SideB()
{
return Math.Sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
}
private double SideC()
{
return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
public double Perimeter()
{
return SideA() + SideB() + SideC();
}
public double Area()
{
double p = Perimeter() / 2;
return Math.Sqrt(p * (p - SideA()) * (p - SideB()) * (p - SideC()));
}
private void SetTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
{
this.x1 = x1; this.y1 = y1;
this.x2 = x2; this.y2 = y2;
this.x3 = x3; this.y3 = y3;
double a = SideA();
double b = SideB();
double c = SideC();
if (a + b <= c || a + c <= b || b + c <= a)
{
throw new Exception("Невозможно построить треугольник");
}
}
public void Move(double dx, double dy)
{
x1 += dx; y1 += dy;
x2 += dx; y2 += dy;
x3 += dx; y3 += dy;
}
public void Resize(double factor)
{
if (factor <= 0)
{
throw new Exception("Коэффициент должен быть больше нуля");
}
double cx = (x1 + x2 + x3) / 3;
double cy = (y1 + y2 + y3) / 3;
x1 = cx + (x1 - cx) * factor;
y1 = cy + (y1 - cy) * factor;
x2 = cx + (x2 - cx) * factor;
y2 = cy + (y2 - cy) * factor;
x3 = cx + (x3 - cx) * factor;
y3 = cy + (y3 - cy) * factor;
}
public void Rotate(double angle)
{
double rad = angle * Math.PI / 180;
double cos = Math.Cos(rad);
double sin = Math.Sin(rad);
double cx = (x1 + x2 + x3) / 3;
double cy = (y1 + y2 + y3) / 3;
RotatePoint(ref x1, ref y1, cx, cy, cos, sin);
RotatePoint(ref x2, ref y2, cx, cy, cos, sin);
RotatePoint(ref x3, ref y3, cx, cy, cos, sin);
}
private void RotatePoint(ref double x, ref double y, double cx, double cy, double cos, double sin)
{
double dx = x - cx;
double dy = y - cy;
x = cx + dx * cos - dy * sin;
y = cy + dx * sin + dy * cos;
}
public void Print()
{
Console.WriteLine($"Вершины: ({x1:F2},{y1:F2}) ({x2:F2},{y2:F2}) ({x3:F2},{y3:F2})");
Console.WriteLine($"Периметр: {Perimeter():F2}");
Console.WriteLine($"Площадь: {Area():F2}");
}
}
class Program
{
static void Main()
{
try
{
Console.WriteLine("Создание треугольника");
Triangle t1 = new Triangle(0, 0, 4, 0, 1, 3);
t1.Print();
Console.WriteLine("\nПеремещение на (2,1)");
t1.Move(2, 1);
t1.Print();
Console.WriteLine("\nУвеличение в 2 раза");
t1.Resize(2);
t1.Print();
Console.WriteLine("\nПоворот на 45 градусов");
t1.Rotate(45);
t1.Print();
Console.WriteLine("\nПопытка создать неправильный треугольник");
Triangle t2 = new Triangle(0, 0, 1, 0, 2, 0);
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
}
}
```