You are on page 1of 16

Міністерство освіти і науки України

Тернопільський національний університет ім. Івана Пулюя

Кафедра математики та
математичного моделювання

Звіт
Про виконання лабораторної роботи №2
З дисципліни “ООП”
На тему: “Знайомство з додатками Windows Forms
та Windows Presentation Foundation. Реалізація
алгоритмів з використанням операторів if, switch,
for, while, do … while. ”

Виконав:
Студент групи СН-21
Козловський А. І.

Перевірив: Готович В. А.

Тернопіль, 2020 р.

Варіант 11
Тема: Знайомство з додатками Windows Forms та Windows Presentation Foundation.
Реалізація алгоритмів з використанням операторів if, switch, for, while, do … while.
Мета роботи: навчитися створювати додатки Windows Forms та Windows Presentation
Foundation, змінювати параметри вікна, повторити принципи побудови розгалужених
та циклічних алгоритмів.
Завдання:
1. Написати 3 програми (консольну, Windows Forms, WPF) для обрахунку значення
виразу
2. Написати 3 програми (консольну, Windows Forms, WPF) для обчислення коренів
квадратного рівняння.
3. Напишіть 3 програми у вигляді консольних додатків для розв’язання
таких завдань:

Проект «ConsoleApp4»:
Користувач з клавіатури вводить цілі числа (числа вводяться почергово,
користувач вводячи число натискає «Enter»). Послідовність чисел завершується
введенням значення «0». Порахувати кількість парних та непарних чисел, кількість
додатних та від’ємних.
Порядок виконання:
Завдання 1:
1. Складаємо та пишемо текст програм для консолі
2. Створюємо дизайн вікон WinForms та пишемо код
3. Створюємо дизайн вікон WPF та пишемо код
4. Компілюємо та отримуємо результати
Висновок: під час виконання лабораторної роботи я навчився створювати додатки
Windows Forms та Windows Presentation Foundation, змінювати параметри вікна,
повторив принципи побудови розгалужених та циклічних алгоритмів
Коди програм
Програма ConsoleApp1:
using System;
using System.Text;
namespace Lab2_ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
Console.InputEncoding = Encoding.Unicode;
System.Globalization.CultureInfo customCulture =
(System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
Console.WriteLine("Лабораторна робота №2.\nВиконав: Козловський А.І.\nВаріант
№11\nЗавдання 1.");
double x, y, z, s;
do
{
Console.Write("Введіть дробове значення x = ");
if (double.TryParse(Console.ReadLine(), out x)) break;
else
{
Console.WriteLine("Значення x введено невірно! Введіть коректне значення.");
}
}
while (true);
do
{
Console.Write("Введіть дробове значення y = ");
if (double.TryParse(Console.ReadLine(), out y)) break;
else
{
Console.WriteLine("Значення y введено невірно! Введіть коректне значення.");
}
} while (true);
do
{
Console.Write("Введіть дробове значення z = ");
if (double.TryParse(Console.ReadLine(), out z)) break;
else
{
Console.WriteLine("Помилка введення значення z. Будь-ласка повторіть введення ще
раз!!!");
}
}
while (true);
s = Math.Pow(y, Math.Pow(Math.Abs(x), 1 / 3) + Math.Pow(Math.Cos(y), 3) + (Math.Abs(x -
y) * (1 + (Math.Pow(Math.Sin(z), 2) / Math.Sqrt(x + y) / (Math.Exp(Math.Abs(x - y)) + (x / 2))))));
Console.WriteLine($"Результат обчислення: s ={s:F3}");
}
}
}
Програма WindowsFormsApp1:
using System;

using System.Windows.Forms;

namespace WinFormsApp1

public partial class Form1 : Form

public Form1()

InitializeComponent();

private void lab2_1_Load(object sender, EventArgs e)

System.Globalization.CultureInfo customCulture =

(System.Globalization.CultureInfo)

System.Threading.Thread.CurrentThread.CurrentCulture.Clone();

customCulture.NumberFormat.NumberDecimalSeparator = ".";

System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

private void button1_Click(object sender, EventArgs e)

double x, y, z, s;

if (double.TryParse(x_data.Text, out x)) { }

else

MessageBox.Show("Помилка введення значення x!", "Помилка", MessageBoxButtons.OK,


MessageBoxIcon.Error);

if (double.TryParse(y_data.Text, out y)) { }

else

MessageBox.Show("Помилка введення значення y!", "Помилка", MessageBoxButtons.OK,


MessageBoxIcon.Error);

if (double.TryParse(z_data.Text, out z)) { }

else

MessageBox.Show("Помилка введення значення z!", "Помилка", MessageBoxButtons.OK,


MessageBoxIcon.Error);
}

s = Math.Pow(y, Math.Pow(Math.Abs(x), 1 / 3) + Math.Pow(Math.Cos(y), 3) + (Math.Abs(x -


y) * (1 + (Math.Pow(Math.Sin(z), 2) / Math.Sqrt(x + y) / (Math.Exp(Math.Abs(x - y)) + (x / 2))))));
s_data.Text = s.ToString("F2");

Програма WpfApp:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Globalization.CultureInfo customCulture =
(System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
}

private void button1_Click(object sender, RoutedEventArgs e)


{
double x, y, z, s;
if (double.TryParse(x_data.Text, out x)) { }
else
{
MessageBox.Show("Помилка введення значення x!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
if (double.TryParse(TextBoxy.Text, out y)) { }
else
{
MessageBox.Show("Помилка введення значення y!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
if (double.TryParse(TextBoxz.Text, out z)) { }
else
{
MessageBox.Show("Помилка введення значення z!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
s = Math.Pow(y, Math.Pow(Math.Abs(x), 1 / 3) + Math.Pow(Math.Cos(y), 3) + (Math.Abs(x -
y) * (1 + (Math.Pow(Math.Sin(z), 2) / Math.Sqrt(x + y) / (Math.Exp(Math.Abs(x - y)) + (x / 2))))));
s_data.Text = s.ToString("F2");
}
}
}

Програма ConsoleApp_2:
using System;
using System.Text;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
Console.InputEncoding = Encoding.Unicode;
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

Console.WriteLine("Лабораторна робота №2.\nВиконав: Козловський А.І.\nВаріант


№11\nЗавдання 2.");
double a, b, c, D, x1, x2;
do
{
Console.Write("Введіть значення a = ");
if (double.TryParse(Console.ReadLine(), out a)) break;
else
{
Console.WriteLine(" Помилка введення значення a. Будь-ласка повторіть введення
ще раз!");
}
}
while (true);

do
{
Console.Write("Введіть значення b = ");
if (double.TryParse(Console.ReadLine(), out b)) break;
else
{
Console.WriteLine(" Помилка введення значення b. Будь-ласка повторіть введення
ще раз!");
}
}

while (true);
do
{
Console.Write("Введіть значення c = ");
if (double.TryParse(Console.ReadLine(), out c)) break;
else
{
Console.WriteLine(" Помилка введення значення c. Будь-ласка повторіть введення
ще раз!");
}
}

while (true);
D = Math.Pow(b, 2) - 4 * a * c;
if (D > 0)
{
x1 = (-b + Math.Sqrt(D)) / (2 * a);
x2 = (-b - Math.Sqrt(D)) / (2 * a);
if (a == 0)
{
Console.WriteLine("Помилка ділення на нуль!");
}
else
{
Console.WriteLine($"Дискримінант більше нуля: D = {D}\nРівняння має два корені:
x1 = {x1}, x2 = {x2}");
}
}

if (D == 0)
{
x1 = -b / (2 * a);
if (a == 0)
{
Console.WriteLine("Помилка ділення на нуль!");
}

else
{
Console.WriteLine($"Дискримінант дорівнює нулю: D = {D}\nРівняння має один
корінь: x1 = {x1:F0}");
}
}

if (D < 0)
{
Console.WriteLine($"Дискримінант менше нуля: D = {D}\nРівняння не має коренів!");
}
}
}
}

Програма WindowsFormsApp2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
System.Globalization.CultureInfo customCulture =
(System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
}

private void button1_Click(object sender, EventArgs e)


{
double a, b, c, D, x1, x2;
if (double.TryParse(a_data.Text, out a)) { }
else
{
MessageBox.Show("Помилка введення значення a!", "Помилка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
if (double.TryParse(b_data.Text, out b)) { }
else
{
MessageBox.Show("Помилка введення значення b!", "Помилка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
if (double.TryParse(c_data.Text, out c)) { }
else
{
MessageBox.Show("Помилка введення значення c!", "Помилка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
D = Math.Pow(b, 2) - 4 * a * c;
if (D > 0)
{
x1 = (-b + Math.Sqrt(D)) / (2 * a);
x2 = (-b - Math.Sqrt(D)) / (2 * a);
if (a == 0)
{
MessageBox.Show("Помилка ділення на нуль!", "Помилка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
d_data.Text = D.ToString("F");
label5.Visible = true;
x1_data.Visible = true;
x1_data.Text = x1.ToString("F");
label6.Visible = true;
x2_data.Visible = true;
x2_data.Text = x2.ToString("F");
}
}
if (D == 0)
{
x1 = -b / (2 * a);
if (a == 0)
{
MessageBox.Show("Помилка ділення на нуль!", "Помилка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
d_data.Text = D.ToString("F");
label5.Visible = true;
x1_data.Visible = true;
x1_data.Text = x1.ToString("F");
label6.Visible = false;
x2_data.Visible = false;
}
}
if (D < 0)
{
d_data.Text = D.ToString("F");
label5.Visible = false;
x1_data.Visible = false;
label6.Visible = false;
x2_data.Visible = false;
MessageBox.Show("Рівняння не має коренів!", "Результат", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}

Програма WpfApp2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
}

private void button1_Click(object sender, RoutedEventArgs e)


{
double a, b, c, D, x1, x2;
if (double.TryParse(TextBoxA.Text, out a)) { }
else
{
MessageBox.Show("Помилка введення значення a!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
if (double.TryParse(TextBoxB.Text, out b)) { }
else
{
MessageBox.Show("Помилка введення значення b!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
if (double.TryParse(TextBoxC.Text, out c)) { }
else
{
MessageBox.Show("Помилка введення значення c!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
D = Math.Pow(b, 2) - 4 * a * c;
if (D > 0)
{
x1 = (-b + Math.Sqrt(D)) / (2 * a);
x2 = (-b - Math.Sqrt(D)) / (2 * a);
if (a == 0)
{
MessageBox.Show("Помилка ділення на нуль!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
else
{
TextBoxD.Text = D.ToString("F");
label5.Visibility = Visibility.Visible;
TextBoxX1.Visibility = Visibility.Visible;
TextBoxX1.Text = x1.ToString("F");
label6.Visibility = Visibility.Visible;
TextBoxX2.Visibility = Visibility.Visible;
TextBoxX2.Text = x2.ToString("F");
}
}
if (D == 0)
{
x1 = -b / (2 * a);
if (a == 0)
{
MessageBox.Show("Помилка ділення на нуль!!!", "Помилка", MessageBoxButton.OK,
MessageBoxImage.Error);
}
else
{
TextBoxD.Text = D.ToString("F");
label5.Visibility = Visibility.Visible;
TextBoxX1.Visibility = Visibility.Visible;
TextBoxX1.Text = x1.ToString("F");
label6.Visibility = Visibility.Hidden;
TextBoxX2.Visibility = Visibility.Hidden;
}
}
if (D < 0)
{
TextBoxD.Text = D.ToString("F");
label5.Visibility = Visibility.Hidden;
TextBoxX1.Visibility = Visibility.Hidden;
label6.Visibility = Visibility.Hidden;
TextBoxX2.Visibility = Visibility.Hidden;
MessageBox.Show("Рівняння не має коренів!!!", "Результат", MessageBoxButton.OK,
MessageBoxImage.Information);

}
}
}
}

Програма ConsoleApp_2
using System;
using System.Text;

namespace ConsoleApp_2
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
Console.InputEncoding = Encoding.Unicode;
System.Globalization.CultureInfo customCulture =
(System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
Console.WriteLine("Лабораторна робота №2.\nВиконав: Козловський А.І.\nВаріант
№11\nЗавдання 3.");
double N, res = 0;
do
{
Console.Write("Введіть значення N = ");
if (double.TryParse(Console.ReadLine(), out N)) break;
else
{
Console.WriteLine(" Помилка введення значення N. Будь-ласка, повторіть введення
ще раз!");
}
}
while (true);

for (int i = 0; i < N; i++)


{
double j = N + 1;
res = res + Math.Pow(i, --j);

Console.WriteLine($"Result = {res}\n");
}
}
}
}

Програма ConsoleApp_3
using System;
using System.Text;

namespace ConsoleApp_3
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
Console.InputEncoding = Encoding.Unicode;
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

Console.WriteLine("Лабораторна робота №2.\nВиконав: Козловський А.І.\nВаріант


№11\nЗавдання 3.");
double N, res = 0, K;
do
{
Console.Write("Введіть значення N = ");
if (double.TryParse(Console.ReadLine(), out N)) break;
else
{
Console.WriteLine(" Помилка введення значення N. Будь-ласка повторіть введення
ще раз!");
}
}
while (true);
do
{
Console.Write("Введіть значення K = ");
if (double.TryParse(Console.ReadLine(), out K)) break;
else
{
Console.WriteLine(" Помилка введення значення K. Будь-ласка повторіть введення
ще раз!");
}
}
while (true);
for (int i = 0; i < N; i++)
{
res += Math.Pow(i, K);
Console.WriteLine($"Result = {res}\n");
}
}
}
}

Програма ConsoleApp_4
using System;
using System.Text;

namespace ConsoleApp_4
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.Unicode;
Console.InputEncoding = Encoding.Unicode;
System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)
System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
customCulture.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;

Console.WriteLine("Лабораторна робота №2.\nВиконав: Козловський А.І.\nВаріант


№11\nЗавдання 3.");
double N;
bool K = false;
int countPar = 0, countNePar = 0, countPos = 0, countNeg = 0;
do
{
do
{
Console.Write("Введіть значення N = ");
if (double.TryParse(Console.ReadLine(), out N)) break;
else
{
Console.WriteLine(" Помилка введення значення N. Будь-ласка по-вторіть
введення ще раз!");
}
}
while (true);
if (N % 2 == 0)
{
countPar = countPar + 1;
}
if (N % 2 == 1)
{
countNePar = countNePar + 1;
}
if (N > 0)
{
countPos = countPos + 1;
}
if (N < 0)
{
countNeg = countNeg + 1;
}
else if (N == 0)
{
K = true;
}

}
while (!K);
Console.WriteLine($"Парні = {countPar}\n");
Console.WriteLine($"Не парні = {countNePar}\n");
Console.WriteLine($"Додатні = {countPos}\n");
Console.WriteLine($"Від'ємні = {countNeg}\n");
}
}
}

You might also like