4 глава
This commit is contained in:
parent
e0caa7caab
commit
ec805e7f08
15 changed files with 1584 additions and 1 deletions
1140
03-инструкции/README.md
Normal file
1140
03-инструкции/README.md
Normal file
File diff suppressed because it is too large
Load diff
29
03-инструкции/src/chapter-3-11/app.d
Normal file
29
03-инструкции/src/chapter-3-11/app.d
Normal 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);
|
||||
}
|
||||
}
|
29
03-инструкции/src/chapter-3-12/app.d
Normal file
29
03-инструкции/src/chapter-3-12/app.d
Normal 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);
|
||||
}
|
||||
}
|
9
03-инструкции/src/chapter-3-13/app.d
Normal file
9
03-инструкции/src/chapter-3-13/app.d
Normal 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."); // Не выполнится
|
||||
}
|
34
03-инструкции/src/chapter-3-6/app.d
Normal file
34
03-инструкции/src/chapter-3-6/app.d
Normal 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));
|
||||
}
|
17
03-инструкции/src/chapter-3-7-4/app.d
Normal file
17
03-инструкции/src/chapter-3-7-4/app.d
Normal 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)); // То же самое
|
||||
}
|
||||
}
|
23
03-инструкции/src/chapter-3-7-5/app.d
Normal file
23
03-инструкции/src/chapter-3-7-5/app.d
Normal 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]);
|
||||
}
|
24
03-инструкции/src/chapter-3-9/app.d
Normal file
24
03-инструкции/src/chapter-3-9/app.d
Normal 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 осталось видимым
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue