-
Notifications
You must be signed in to change notification settings - Fork 5
/
Homework6.cs
188 lines (183 loc) · 6.66 KB
/
Homework6.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
namespace Homework6
{
internal class Homework6
{
// Task 1
public static int Div(int a, int b)
{
return a / b;
}
// Task 2
public static double Div(double a, double b)
{
return (double)a / b;
}
// Task 3
public static List<int> ReadNumber(int start, int end)
{
List<int> numbers = new List<int>();
const int LENGHT = 10;
while (true)
{
try
{
// Adding the first integer to the list if it is in our range.
Console.Write("Enter number [1]: ");
int num = Convert.ToInt32(Console.ReadLine());
if(num >= end || num <= start)
{
throw new ApplicationException($"The number must be in range of ({start};{end}).");
}
numbers.Add(num);
break;
}
catch(ApplicationException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch (FormatException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
for (int i = 1; i < LENGHT; i++)
{
while (true)
{
try
{
// Adding other elements to the list if they're in our range and the current integer is bigger than the previous one.
Console.Write($"Enter number [{i+1}]: ");
int num = Convert.ToInt32(Console.ReadLine());
if (num >= end || num <= start)
{
throw new ApplicationException($"The number must be in range of ({start};{end}).");
}
if (numbers[i - 1] >= num)
{
throw new ApplicationException($"The number must be > than the previous one.");
}
numbers.Add(num);
break;
}
catch (ApplicationException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch (FormatException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
return numbers;
}
static void Main(string[] args)
{
// Task 1
while (true)
{
try
{
Console.Write("Enter the first integer value: ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second integer value: ");
int b = Convert.ToInt32(Console.ReadLine());
if (a < b)
{
throw new ApplicationException("The first value must be >= than the second value");
}
Console.WriteLine($"The result of division first and second integer is {Div(a, b)}");
break;
}
catch(ApplicationException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch(DivideByZeroException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch(FormatException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
Console.WriteLine("Success!");
}
}
// Task 2
while (true)
{
try
{
Console.Write("Enter the first double value: ");
double a = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter the second double value: ");
double b = Convert.ToDouble(Console.ReadLine());
if (b == 0)
{
throw new DivideByZeroException("The second double can't equal 0");
}
Console.WriteLine($"The result of division the first and the second double is {Div(a, b)}");
break;
}
catch (ApplicationException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch (DivideByZeroException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch (FormatException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
Console.WriteLine("Success!");
}
}
// Task 3
while (true)
{
try
{
int start, end;
Console.Write("Enter the left limit of range: ");
start = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the right limit of range: ");
end = Convert.ToInt32(Console.ReadLine());
if (start >= end)
{
throw new ApplicationException("The left limit can't be bigger than the right one!");
}
if (end - start <= 10)
{
throw new ApplicationException("The difference of end and start must be at least 10!");
}
List<int> numbers = new List<int>();
numbers = ReadNumber(start, end);
Console.Write("Result: ");
foreach (var number in numbers)
{
Console.Write($"{number} ");
}
break;
}
catch (ApplicationException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
catch (FormatException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
}
}