1 Star 0 Fork 4

Lucifer / Programming-in-D

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
struct.d 28.24 KB
一键复制 编辑 原始数据 按行查看 历史
meatatt 提交于 2016-06-11 20:36 . Preparation
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
Ddoc
$(DERS_BOLUMU $(IX struct) Structs)
$(P
As has been mentioned several times earlier in the book, fundamental types are not suitable to represent higher-level concepts. For example, although a value of type $(C int) is suitable to represent the hour of day, two $(C int) variables would be more suitable together to represent a point in time: one for the hour and the other for the minute.
)
$(P
Structs are the feature that allow defining new types by combining already existing other types. The new type is defined by the $(C struct) keyword. Most of the content of this chapter is directly applicable to classes as well. Especially the concept of $(I combining existing types to define a new type) is exactly the same for them.
)
$(P
This chapter covers only the basic features of structs. We will see more of structs in the following chapters:
)
$(UL
$(LI $(LINK2 /ders/d.en/member_functions.html, Member Functions))
$(LI $(LINK2 /ders/d.en/const_member_functions.html, const ref Parameters and const Member Functions))
$(LI $(LINK2 /ders/d.en/special_functions.html, Constructor and Other Special Functions))
$(LI $(LINK2 /ders/d.en/operator_overloading.html, Operator Overloading))
$(LI $(LINK2 /ders/d.en/encapsulation.html, Encapsulation and Protection Attributes))
$(LI $(LINK2 /ders/d.en/property.html, Properties))
$(LI $(LINK2 /ders/d.en/invariant.html, Contract Programming for Structs and Classes))
$(LI $(LINK2 /ders/d.en/foreach_opapply.html, foreach with Structs and Classes))
)
$(P
To understand how useful structs are, let's take a look at the $(C addDuration()) function that we had defined earlier in the $(LINK2 /ders/d.en/assert.html, $(C assert) and $(C enforce) chapter). The following definition is from the exercise solution of that chapter:
)
---
void addDuration(in int startHour, in int startMinute,
in int durationHour, in int durationMinute,
out int resultHour, out int resultMinute) {
resultHour = startHour + durationHour;
resultMinute = startMinute + durationMinute;
resultHour += resultMinute / 60;
resultMinute %= 60;
resultHour %= 24;
}
---
$(P $(I $(B Note:) I will ignore the $(C in), $(C out), and $(C unittest) blocks in this chapter to keep the code samples short.)
)
$(P
Although the function above clearly takes six parameters, when the three pairs of parameters are considered, it is conceptually taking only three bits of information for the starting time, the duration, and the result.
)
$(H5 Definition)
$(P
The $(C struct) keyword defines a new type by combining variables that are related in some way:
)
---
$(CODE_NAME TimeOfDay)struct TimeOfDay {
int hour;
int minute;
}
---
$(P
The code above defines a new type named $(C TimeOfDay), which consists of two variables named $(C hour) and $(C minute). That definition allows the new $(C TimeOfDay) type to be used in the program just like any other type. The following code demonstrates how similar its use is to an $(C int)'s:
)
---
int number; // a variable
number = otherNumber; // taking the value of otherNumber
TimeOfDay time; // an object
time = otherTime; // taking the value of otherTime
---
$(P
The syntax of $(C struct) definition is the following:
)
---
struct $(I TypeName) {
// ... member variables and functions ...
}
---
$(P
We will see member functions in later chapters.
)
$(P
The variables that a struct combines are called its $(I members). According to this definition, $(C TimeOfDay) has two members: $(C hour) and $(C minute).
)
$(H6 $(C struct) defines a type, not a variable)
$(P
There is an important distinction here: Especially after the $(LINK2 /ders/d.en/name_space.html, Name Scope) and $(LINK2 /ders/d.en/lifetimes.html, Lifetimes and Fundamental Operations) chapters, the curly brackets of $(C struct) definitions may give the wrong impression that the struct members start and end their lives inside that scope. This is not true.
)
$(P
Member definitions are not variable definitions:
)
---
struct TimeOfDay {
int hour; // ← Not a variable; will become a part of
// a struct variable used in the program.
int minute; // ← Not a variable; will become a part of
// a struct variable used in the program.
}
---
$(P
The definition of a $(C struct) determines the types and the names of the members that the objects of that $(C struct) will have. Those member variables will be constructed as parts of $(C TimeOfDay) objects that take part in the program:
)
---
TimeOfDay bedTime; // This object contains its own hour
// and minute member variables.
TimeOfDay wakeUpTime; // This object contains its own hour
// and minute member variables as
// well. The member variables of
// this object are not related to
// the member variables of the
// previous object.
---
$(P
$(IX object, struct) Variables of $(C struct) and $(C class) types are called $(I objects).
)
$(H6 Coding convenience)
$(P
Being able to combine the concepts of hour and minute together as a new type is a great convenience. For example, the function above can be rewritten in a more meaningful way by taking three $(C TimeOfDay) parameters instead of the existing six $(C int) parameters:
)
---
void addDuration(in TimeOfDay start,
in TimeOfDay duration,
out TimeOfDay result) {
// ...
}
---
$(P $(I $(B Note:) It is not normal to add two variables that represent two points in time. For example, it is meaningless to add the lunch time 12:00 to the breakfast time 7:30. It would make more sense to define another type, appropriately called $(C Duration), and to add objects of that type to $(C TimeOfDay) objects. Despite this design flaw, I will continue using only $(C TimeOfDay) objects in this chapter and introduce $(C Duration) in a later chapter.)
)
$(P
As you remember, functions return up-to a single value. That has precisely been the reason why the earlier definition of $(C addDuration()) was taking two $(C out) parameters: It could not return the hour and minute information as a single value.
)
$(P
Structs remove this limitation as well: Since multiple values can be combined as a single $(C struct) type, functions can return an object of such a $(C struct), effectively returning multiple values at once. $(C addDuration()) can now be defined as returning its result:
)
---
TimeOfDay addDuration(in TimeOfDay start,
in TimeOfDay duration) {
// ...
}
---
$(P
As a consequence, $(C addDuration()) now becomes a function that produces a value, as opposed to being a function that has side effects. As you would remember from the $(LINK2 /ders/d.en/functions.html, Functions chapter), producing results is preferred over having side effects.
)
$(P
Structs can be members of other structs. For example, the following $(C struct) has two $(C TimeOfDay) members:
)
---
struct Meeting {
string topic;
size_t attendanceCount;
TimeOfDay start;
TimeOfDay end;
}
---
$(P
$(C Meeting) can in turn be a member of another $(C struct). Assuming that there is also the $(C Meal) struct:
)
---
struct DailyPlan {
Meeting projectMeeting;
Meal lunch;
Meeting budgetMeeting;
}
---
$(H5 $(IX ., member) Accessing the members)
$(P
Struct members are used like any other variable. The only difference is that the actual struct variable and a $(I dot) must be specified before the name of the member:
)
---
start.hour = 10;
---
$(P
The line above assigns the value 10 to the $(C hour) member of the $(C start) object.
)
$(P
Let's rewrite the $(C addDuration()) function with what we have seen so far:
)
---
$(CODE_NAME addDuration)TimeOfDay addDuration(in TimeOfDay start,
in TimeOfDay duration) {
TimeOfDay result;
result.minute = start.minute + duration.minute;
result.hour = start.hour + duration.hour;
result.hour += result.minute / 60;
result.minute %= 60;
result.hour %= 24;
return result;
}
---
$(P
Notice that the names of the variables are now much shorter in this version of the function: $(C start), $(C duration), and $(C result). Additionally, instead of using complex names like $(C startHour), it is possible to access struct members through their respective struct variables as in $(C start.hour).
)
$(P
Here is a code that uses the new $(C addDuration()) function. Given the start time and the duration, the following code calculates when a class period at a school would end:
)
---
$(CODE_XREF TimeOfDay)$(CODE_XREF addDuration)void main() {
TimeOfDay periodStart;
periodStart.hour = 8;
periodStart.minute = 30;
TimeOfDay periodDuration;
periodDuration.hour = 1;
periodDuration.minute = 15;
immutable periodEnd = addDuration(periodStart,
periodDuration);
writefln("Period end: %s:%s",
periodEnd.hour, periodEnd.minute);
}
---
$(P
The output:
)
$(SHELL
Period end: 9:45
)
$(P
The $(C main()) above has been written only by what we have seen so far. We will make this code even shorter and cleaner soon.
)
$(H5 $(IX construction) Construction)
$(P
The first three lines of $(C main()) are about constructing the $(C periodStart) object and the next three lines are about constructing the $(C periodDuration) object. In each three lines of code first an object is being defined and then its hour and minute values are being set.
)
$(P
In order for a variable to be used in a safe way, that variable must first be constructed in a consistent state. Because construction is so common, there is a special construction syntax for struct objects:
)
---
TimeOfDay periodStart = TimeOfDay(8, 30);
TimeOfDay periodDuration = TimeOfDay(1, 15);
---
$(P
The values are automatically assigned to the members in the order that they are specified: Because $(C hour) is defined first in the $(C struct), the value 8 is assigned to $(C periodStart.hour) and 30 is assigned to $(C periodStart.minute).
)
$(P
As we have seen in $(LINK2 /ders/d.en/cast.html, the Type Conversions chapter), the construction syntax can be used for other types as well:
)
---
auto u = ubyte(42); // u is a ubyte
auto i = int(u); // i is an int
---
$(H6 Constructing objects as $(C immutable))
$(P
Being able to construct the object by specifying the values of its members at once makes it possible to define objects as $(C immutable):
)
---
immutable periodStart = TimeOfDay(8, 30);
immutable periodDuration = TimeOfDay(1, 15);
---
$(P
Otherwise it would not be possible to mark an object first as $(C immutable) and then modify its members:
)
---
$(HILITE immutable) TimeOfDay periodStart;
periodStart.hour = 8; $(DERLEME_HATASI)
periodStart.minute = 30; $(DERLEME_HATASI)
---
$(H6 Trailing members need not be specified)
$(P
There may be fewer values specified than the number of members. In that case, the remaining members are initialized by the $(C .init) values of their respective types.
)
$(P
The following program constructs $(C Test) objects each time with one less constructor parameter. The $(C assert) checks indicate that the unspecified members are initialized automatically by their $(C .init) values. (The reason for needing to call $(C isNaN()) is explained after the program):
)
---
import std.math;
struct Test {
char c;
int i;
double d;
}
void main() {
// The initial values of all of the members are specified
auto t1 = Test('a', 1, 2.3);
assert(t1.c == 'a');
assert(t1.i == 1);
assert(t1.d == 2.3);
// Last one is missing
auto t2 = Test('a', 1);
assert(t2.c == 'a');
assert(t2.i == 1);
assert($(HILITE isNaN(t2.d)));
// Last two are missing
auto t3 = Test('a');
assert(t3.c == 'a');
assert($(HILITE t3.i == int.init));
assert($(HILITE isNaN(t3.d)));
// No initial value specified
auto t4 = Test();
assert($(HILITE t4.c == char.init));
assert($(HILITE t4.i == int.init));
assert($(HILITE isNaN(t4.d)));
// The same as above
Test t5;
assert(t5.c == char.init);
assert(t5.i == int.init);
assert(isNaN(t5.d));
}
---
$(P
As you would remember from the $(LINK2 /ders/d.en/floating_point.html, Floating Point Types chapter), the initial value of $(C double) is $(C double.nan). Since the $(C .nan) value is $(I unordered), it is meaningless to use it in equality comparisons. That is why calling $(C std.math.isNaN) is the correct way of determining whether a value equals to $(C .nan).
)
$(H6 $(IX default value, member) Specifying default values for members)
$(P
It is important that member variables are automatically initialized with known initial values. This prevents the program from continuing with indeterminate values. However, the $(C .init) value of their respective types may not be suitable for every type. For example, $(C char.init) is not even a valid value.
)
$(P
The initial values of the members of a struct can be specified when the struct is defined. This is useful for example to initialize floating point members by $(C 0.0), instead of the mostly-unusable $(C .nan).
)
$(P
The default values are specified by the assignment syntax as the members are defined:
)
---
struct Test {
char c $(HILITE = 'A');
int i $(HILITE = 11);
double d $(HILITE = 0.25);
}
---
$(P
Please note that the syntax above is not really assignment. The code above merely determines the default values that will be used when objects of that struct are actually constructed later in the program.
)
$(P
For example, the following $(C Test) object is being constructed without any specific values:
)
---
Test t; // no value is specified for the members
writefln("%s,%s,%s", t.c, t.i, t.d);
---
$(P
All of the members are initialized by their default values:
)
$(SHELL
A,11,0.25
)
$(H6 $(IX {}, construction) $(IX C-style struct initialization) Constructing by the $(C {}) syntax)
$(P
Struct objects can also be constructed by the following syntax:
)
---
TimeOfDay periodStart = { 8, 30 };
---
$(P
Similar to the earlier syntax, the specified values are assigned to the members in the order that they are specified. The trailing members get their default values.
)
$(P
This syntax is inherited from the C programming language:
)
---
auto periodStart = TimeOfDay(8, 30); // ← regular
TimeOfDay periodEnd = { 9, 30 }; // ← C-style
---
$(P
$(IX designated initializer) This syntax allows $(I designated initializers). Designated initializers are for specifying the member that an initialization value is associated with. It is even possible to initialize members in a different order than they are defined in the $(C struct):
)
---
TimeOfDay t = { $(HILITE minute:) 42, $(HILITE hour:) 7 };
---
$(H5 $(IX copy, struct) $(IX assignment, struct) Copying and assignment)
$(P
Structs are value types. As has been described in the $(LINK2 /ders/d.en/value_vs_reference.html, Value Types and Reference Types chapter), this means that every $(C struct) object has its own value. Objects get their own values when constructed, and their values change when they are assigned new values.
)
---
auto yourLunchTime = TimeOfDay(12, 0);
auto myLunchTime = yourLunchTime;
// Only my lunch time becomes 12:05:
myLunchTime.minute += 5;
// ... your lunch time is still the same:
assert(yourLunchTime.minute == 0);
---
$(P
During a copy, all of the members of the source object are automatically copied to the corresponding members of the destination object. Similarly, assignment involves assigning each member of the source to the corresponding member of the destination.
)
$(P
Struct members that are of reference types need extra attention.
)
$(H6 $(IX reference type, member) Careful with members that are of reference types!)
$(P
As you remember, copying or assigning variables of reference types does not change any value, it changes what object is being referenced. As a result, copying or assigning generates one more reference to the right-hand side object. The relevance of this for struct members is that, the members of two separate struct objects would start providing access to the same value.
)
$(P
To see an example of this, let's have a look at a struct where one of the members is a reference type. This struct is used for keeping the student number and the grades of a student:
)
---
struct Student {
int number;
int[] grades;
}
---
$(P
The following code constructs a second $(C Student) object by copying an existing one:
)
---
// Constructing the first object:
auto student1 = Student(1, [ 70, 90, 85 ]);
// Constructing the second student as a copy of the first
// one and then changing its number:
auto student2 = student1;
student2.number = 2;
// WARNING: The grades are now being shared by the two objects!
// Changing the grades of the first student ...
student1.grades[0] += 5;
// ... affects the second student as well:
writeln(student2.grades[0]);
---
$(P
When $(C student2) is constructed, its members get the values of the members of $(C student1). Since $(C int) is a value type, the second object gets its own $(C number) value.
)
$(P
The two $(C Student) objects also have individual $(C grades) members as well. However, since slices are reference types, the actual elements that the two slices share are the same. Consequently, a change made through one of the slices is seen through the other slice.
)
$(P
The output of the code indicates that the grade of the second student has been increased as well:
)
$(SHELL
75
)
$(P
For that reason, a better approach might be to construct the second object by the copies of the grades of the first one:
)
---
// The second Student is being constructed by the copies
// of the grades of the first one:
auto student2 = Student(2, student1.grades$(HILITE .dup));
// Changing the grades of the first student ...
student1.grades[0] += 5;
// ... does not affect the grades of the second student:
writeln(student2.grades[0]);
---
$(P
Since the grades have been copied by $(C .dup), this time the grades of the second student are not affected:
)
$(SHELL
70
)
$(P
$(I Note: It is possible to have even the reference members copied automatically. We will see how this is done later when covering struct member functions.)
)
$(H5 $(IX literal, struct) Struct literals)
$(P
Similar to being able to use integer literal values like 10 in expressions without needing to define a variable, struct objects can be used as literals as well.
)
$(P
Struct literals are constructed by the object construction syntax.
)
---
TimeOfDay(8, 30) // ← struct literal value
---
$(P
Let's first rewrite the $(C main()) function above with what we have learned since its last version. The variables are constructed by the construction syntax and are $(C immutable) this time:
)
---
$(CODE_XREF TimeOfDay)$(CODE_XREF addDuration)void main() {
immutable periodStart = TimeOfDay(8, 30);
immutable periodDuration = TimeOfDay(1, 15);
immutable periodEnd = addDuration(periodStart,
periodDuration);
writefln("Period end: %s:%s",
periodEnd.hour, periodEnd.minute);
}
---
$(P
Note that $(C periodStart) and $(C periodDuration) need not be defined as named variables in the code above. Those are in fact temporary variables in this simple program, which are used only for calculating the $(C periodEnd) variable. They could be passed to $(C addDuration()) as literal values instead:
)
---
$(CODE_XREF TimeOfDay)$(CODE_XREF addDuration)void main() {
immutable periodEnd = addDuration(TimeOfDay(8, 30),
TimeOfDay(1, 15));
writefln("Period end: %s:%s",
periodEnd.hour, periodEnd.minute);
}
---
$(H5 $(IX static, member) $(C static) members)
$(P
Although objects mostly need individual copies of the struct's members, sometimes it makes sense for the objects of a particular struct type to share some variables. This may be necessary to maintain e.g. a general information about that struct type.
)
$(P
As an example, let's imagine a type that assigns a separate identifier for every object that is constructed of that type:
)
---
struct Point {
// The identifier of each object
size_t id;
int line;
int column;
}
---
$(P
In order to be able to assign different ids to each object, a separate variable is needed to keep the next available id. It would be incremented every time a new object is created. Assume that $(C nextId) is to be defined elsewhere and to be available in the following function:
)
---
Point makePoint(int line, int column) {
size_t id = nextId;
++nextId;
return Point(id, line, column);
}
---
$(P
A decision must be made regarding where the common $(C nextId) variable is to be defined. $(C static) members are useful in such cases.
)
$(P
Such common information is defined as a $(C static) member of the struct. Contrary to the regular members, there is a single variable of each $(C static) member for each thread. (Note that most programs consist of a single thread that starts executing the $(C main()) function.) That single variable is shared by all of the objects of that struct in that thread:
)
---
import std.stdio;
struct Point {
// The identifier of each object
size_t id;
int line;
int column;
// The id of the next object to construct
$(HILITE static size_t nextId;)
}
Point makePoint(int line, int column) {
size_t id = $(HILITE Point.)nextId;
++$(HILITE Point.)nextId;
return Point(id, line, column);
}
void main() {
auto top = makePoint(7, 0);
auto middle = makePoint(8, 0);
auto bottom = makePoint(9, 0);
writeln(top.id);
writeln(middle.id);
writeln(bottom.id);
}
---
$(P
As $(C nextId) is incremented at each object construction, each object gets a unique id:
)
$(SHELL
0
1
2
)
$(P
Since $(C static) members are owned by the entire type, there need not be an object to access them. As we have seen above, such objects can be accessed by the name of the type, as well as by the name of any object of that struct:
)
---
++Point.nextId;
++$(HILITE bottom).nextId; // would be the same as above
---
$(P
When a variable is needed not $(I one per thread) but $(I one per program), then those variables must be defined as $(C shared static). We will see the $(C shared) keyword in a later chapter.
)
$(H6 $(IX static this, struct) $(IX static ~this, struct) $(IX this, static, struct) $(IX ~this, static, struct) $(C static this()) for initialization and $(C static ~this()) for finalization)
$(P
Instead of explicitly assigning an initial value to $(C nextId) above, we relied on its default initial value, zero. We could have used any other value:
)
---
static size_t nextId $(HILITE = 1000);
---
$(P
However, such initialization is possible only when the initial value is known at compile time. Further, some special code may have to be executed before a struct can be used in a thread. Such code is written in $(C static this()) scopes.
)
$(P
For example, the following code reads the initial value from a file if that file exists:
)
---
import std.file;
struct Point {
// ...
enum nextIdFile = "Point_next_id_file";
$(HILITE static this()) {
if (exists(nextIdFile)) {
auto file = File(nextIdFile, "r");
file.readf(" %s", &nextId);
}
}
}
---
$(P
The contents of $(C static this()) blocks are automatically executed once per thread before the $(C struct) type is ever used in that thread. Code that should be executed only once for the entire program (e.g. initializing $(C shared) and $(C immutable) variables) must be defined in $(C shared static this()) and $(C shared static ~this()) blocks, which will be covered in $(LINK2 /ders/d.en/concurrency_shared.html, the Data Sharing Concurrency chapter).
)
$(P
Similarly, $(C static ~this()) is for the final operations of a thread and $(C shared static ~this()) is for the final operations of the entire program.
)
$(P
The following example complements the previous $(C static this()) by writing the value of $(C nextId) to the same file, effectively persisting the object ids over consecutive executions of the program:
)
---
struct Point {
// ...
$(HILITE static ~this()) {
auto file = File(nextIdFile, "w");
file.writeln(nextId);
}
}
---
$(P
The program would now initialize $(C nextId) from where it was left off. For example, the following would be the output of the program's second execution:
)
$(SHELL
3
4
5
)
$(PROBLEM_COK
$(PROBLEM
Design a struct named $(C Card) to represent a playing card.
$(P
This struct can have two members for the suit and the value. It may make sense to use an $(C enum) to represent the suit, or you can simply use the characters , , , and .
)
$(P
An $(C int) or a $(C dchar) value can be used for the card value. If you decide to use an $(C int), the values 1, 11, 12, and 13 may represent the cards that do not have numbers (ace, jack, queen, and king).
)
$(P
There are other design choices to make. For example, the card values can be represented by an $(C enum) type as well.
)
$(P
The way objects of this struct will be constructed will depend on the choice of the types of its members. For example, if both members are $(C dchar), then $(C Card) objects can be constructed like this:
)
---
auto card = Card('♣', '2');
---
)
$(PROBLEM
Define a function named $(C printCard()), which takes a $(C Card) object as a parameter and simply prints it:
---
struct Card {
// ... please define the struct ...
}
void printCard(in Card card) {
// ... please define the function body ...
}
void main() {
auto card = Card(/* ... */);
printCard(card);
}
---
$(P
For example, the function can print the 2 of clubs as:
)
$(SHELL
2
)
$(P
The implementation of that function may depend on the choice of the types of the members.
)
)
$(PROBLEM
Define a function named $(C newDeck()) and have it return 52 cards of a deck as a $(C Card) slice:
---
Card[] newDeck()
out (result) {
assert(result.length == 52);
} body {
// ... please define the function body ...
}
---
$(P
It should be possible to call $(C newDeck()) as in the following code:
)
---
Card[] deck = newDeck();
foreach (card; deck) {
printCard(card);
write(' ');
}
writeln();
---
$(P
The output should be similar to the following with 52 distinct cards:
)
$(SHELL
2 3 4 5 6 7 8 9 0 J Q K A 2 3 4
5 6 7 8 9 0 J Q K A 2 3 4 5 6 7
8 9 0 J Q K A 2 3 4 5 6 7 8 9 0
J Q K A
)
)
$(PROBLEM
Write a function that shuffles the deck. One way is to pick two cards at random by $(C std.random.uniform), to swap those two cards, and to repeat this process a sufficient number of times. The function should take the number of repetition as a parameter:
---
void shuffle(Card[] deck, in int repetition) {
// ... please define the function body ...
}
---
$(P
Here is how it should be used:
)
---
Card[] deck = newDeck();
shuffle(deck, 1);
foreach (card; deck) {
printCard(card);
write(' ');
}
writeln();
---
$(P
The function should swap cards $(C repetition) number of times. For example, when called by 1, the output should be similar to the following:
)
$(SHELL
2 3 4 5 6 7 8 9 0 J Q K A 2 3 4
5 6 7 8 $(HILITE 4) 0 J Q K A 2 3 4 5 6 7
8 9 0 J Q K A 2 3 $(HILITE 9) 5 6 7 8 9 0
J Q K A
)
$(P
A higher value for $(C repetition) should result in a more shuffled deck:
)
---
shuffled(deck, $(HILITE 100));
---
$(P
The output:
)
$(SHELL
4 7 9 6 2 6 6 A 5 8 3 Q J K 8 4
J Q Q 9 0 A A 9 7 3 K 2 0 J 7 7
8 4 J 4 0 6 5 5 K 3 Q 2 5 2 8 A
K 9 0 3
)
$(P
$(I $(B Note:) A much better way of shuffling the deck is explained in the solutions.)
)
)
)
Macros:
SUBTITLE=Structs
DESCRIPTION=The 'struct' feature of the D programming language, which is used for defining user types.
KEYWORDS=d programming book tutorial struct
1
https://gitee.com/lucifer2031/Programming-in-D.git
git@gitee.com:lucifer2031/Programming-in-D.git
lucifer2031
Programming-in-D
Programming-in-D
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891