4 глава

This commit is contained in:
Alexander Zhirov 2023-02-05 19:45:16 +03:00
parent e0caa7caab
commit ec805e7f08
15 changed files with 1584 additions and 1 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,29 @@
/**
* 1. Объявите класс, который наследуется от Exception.
* 2. Создайте конструктор, который принимает, как минимум, два параметра: string file и
* size_t line, со значениями по умолчанию __FILE__ и __LINE__ соответственно.
* 3. Попросите конструктора переслать аргументы конструктору исключения (super).
* 4. Используйте свое исключение.
*/
class MyException : Exception
{
this (string message, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
{
super(message, file, line, next);
}
}
void main()
{
import std.stdio;
try
{
throw new MyException("message here");
}
catch(MyException e)
{
writeln("caught ", e);
}
}

View file

@ -0,0 +1,29 @@
/**
* 1. Объявите класс, который наследуется от Exception.
* 2. Создайте конструктор, который принимает, как минимум, два параметра: string file и
* size_t line, со значениями по умолчанию __FILE__ и __LINE__ соответственно.
* 3. Попросите конструктора переслать аргументы конструктору исключения (super).
* 4. Используйте свое исключение.
*/
class MyException : Exception
{
this (string message, string file = __FILE__, size_t line = __LINE__, Throwable next = null)
{
super(message, file, line, next);
}
}
void main()
{
import std.stdio;
try
{
throw new MyException("message here");
}
catch(MyException e)
{
writeln("caught ", e);
}
}

View file

@ -0,0 +1,9 @@
void main()
{
import std.stdio;
scope(exit) writeln("Running scope exit"); // Выполнится самая последняя
scope(success) writeln("Running scope success"); // Выполнится самая первая
return;
scope(exit) writeln("This is never run since the function returned before it was registered."); // Не выполнится
}

View file

@ -0,0 +1,34 @@
import std.stdio;
import std.conv;
enum Berries { strawberry, blackberry, blueberry }
string[] names = [
"Клубника",
"Ежевика",
"Черника"
];
void printBerry(Berries b)
{
final switch (b)
{
case b.strawberry:
writeln(names[b.strawberry]);
break;
case b.blackberry:
writeln(names[b.blackberry]);
break;
case b.blueberry:
writeln(names[b.blueberry]);
break;
}
}
void main()
{
int berry;
readf("%s\n", berry);
printBerry(to!Berries(berry));
}

View file

@ -0,0 +1,17 @@
import std.math, std.stdio;
void main()
{
foreach (float elem; 1.0 .. 100.0)
{
writeln(log(elem)); // По­лу­ча­ет ло­га­рифм с оди­нар­ной точ­но­стью
}
foreach (double elem; 1.0 .. 100.0)
{
writeln(log(elem)); // Двой­ная точ­ность
}
foreach (elem; 1.0 .. 100.0)
{
writeln(log(elem)); // То же са­мое
}
}

View file

@ -0,0 +1,23 @@
import std.stdio;
void print(double[string] map)
{
foreach (i, e; map)
{
writefln("array['%s'] = %s;", i, e);
}
}
void main()
{
float[] arr = [ 1.0, 2.5, 4.0 ];
foreach (ref float elem; arr)
{
elem *= 2; // Без про­блем
}
writeln(arr);
print(["Луна": 1.283, "Солнце": 499.307, "Проксима Центавра": 133_814_298.759]);
}

View file

@ -0,0 +1,24 @@
import std.math, std.stdio;
struct Point
{
double x, y;
double norm()
{
return sqrt(x * x + y * y);
}
}
void main()
{
Point p;
int z;
with (p)
{
x = 3; // При­сваи­ва­ет зна­че­ние по­лю p.x
p.y = 4; // Хо­ро­шо, что все еще мож­но яв­но ис­поль­зо­вать p
writeln(norm()); // Вы­во­дит зна­че­ние по­ля p.norm, то есть 5
z = 1; // По­ле z ос­та­лось ви­ди­мым
}
}