-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (88 loc) · 3.81 KB
/
Program.cs
File metadata and controls
105 lines (88 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
namespace Practise
{
//класс, описывающий дробное число
public class Fraction
{
//переменная целой части
private long wholePart;
//переменная дробной части
private ushort fractionPart;
private double fraction;
//конструктор
public Fraction(long wholePart, ushort fractionPart)
{
this.wholePart = wholePart;
this.fractionPart = fractionPart;
try
{
fraction = double.Parse(wholePart.ToString() + "," + fractionPart.ToString());
}
catch (FormatException)
{
Console.WriteLine("Неверный формат введенных данных.");
}
catch (OverflowException)
{
Console.WriteLine("Слишком большое число.");
}
}
//функция сложения
public double DAdd(Fraction other)
{
return this.fraction + other.fraction;
}
//функция вычитания
public double DSubstact(Fraction other)
{
return this.fraction - other.fraction;
}
//функция умножения
public double DMultiply(Fraction other)
{
return this.fraction * other.fraction;
}
//функция сравнения
public string DCompare(Fraction other)
{
//обработка условия сравнения
switch (this.fraction > other.fraction)
{
case true:
//вывод если первое число больше второго
return $"Число {this.fraction} больше числа {other.fraction} на {Math.Round(this.fraction - other.fraction, 4)}";
break;
case false:
if (this.fraction < other.fraction)
{
//вывод если второе число больше первого
return $"Число {other.fraction} больше числа {this.fraction} на {Math.Round(other.fraction - this.fraction, 4)}";
}
else
{
//вывод если числа равны
return "Числа равны";
}
break;
}
}
}
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Введите целую и дробную часть первого числа");
//объявление экземпляра класса
Fraction num1 = new Fraction(long.Parse(Console.ReadLine()), ushort.Parse(Console.ReadLine()));
Console.WriteLine("Введите целую и дробную часть второго числа");
//объявление экземпляра класса
Fraction num2 = new Fraction(long.Parse(Console.ReadLine()), ushort.Parse(Console.ReadLine()));
double sum = num1.DAdd(num2);
Console.WriteLine($"Сумма равна: {Math.Round(sum, 4)}");
double substraction = num1.DSubstact(num2);
Console.WriteLine($"Разность равна: {Math.Round(substraction, 4)}");
double mul = num1.DMultiply(num2);
Console.WriteLine($"Произведение равно: {Math.Round(mul, 4)}");
Console.WriteLine(num1.DCompare(num2));
}
}
}