mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-11-12 13:00:47 -05:00
Added quiz 6 (and the trumpeting sounds grow louder)
This commit is contained in:
parent
5f7e9389d5
commit
0552a62896
4 changed files with 112 additions and 7 deletions
11
build.zig
11
build.zig
|
@ -252,11 +252,14 @@ const exercises = [_]Exercise{
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "48_methods2.zig",
|
.main_file = "48_methods2.zig",
|
||||||
.output = "Elephant A (U). Elephant B (U). Elephant C (U).",
|
.output = "A B C",
|
||||||
.hint = "This just needs one little fix."
|
.hint = "This just needs one little fix.",
|
||||||
|
},
|
||||||
|
.{
|
||||||
|
.main_file = "49_quiz6.zig",
|
||||||
|
.output = "A B C Cv Bv Av",
|
||||||
|
.hint = "Now you're writting Zig!",
|
||||||
},
|
},
|
||||||
// 48 use struct method for elephant tails
|
|
||||||
// 49 quiz: add elephant trunk (like tail)!
|
|
||||||
// 50 null vs undefined
|
// 50 null vs undefined
|
||||||
// 51 pass-by-value and const fn params
|
// 51 pass-by-value and const fn params
|
||||||
// 52 slices!
|
// 52 slices!
|
||||||
|
|
|
@ -23,9 +23,9 @@ const Elephant = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print(self: *Elephant) void {
|
pub fn print(self: *Elephant) void {
|
||||||
// Prints elephant letter and (V)isited or (U)nvisited.
|
// Prints elephant letter and [v]isited
|
||||||
var v: u8 = if (self.visited) 'V' else 'U';
|
var v: u8 = if (self.visited) 'v' else ' ';
|
||||||
std.debug.print("Elephant {u} ({u}). ", .{ self.letter, v});
|
std.debug.print("{u}{u} ", .{ self.letter, v });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
91
exercises/49_quiz6.zig
Normal file
91
exercises/49_quiz6.zig
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
//
|
||||||
|
// "Trunks and tails
|
||||||
|
// Are handy things"
|
||||||
|
|
||||||
|
// from Holding Hands
|
||||||
|
// by Lenore M. Link
|
||||||
|
//
|
||||||
|
// Now that we have tails all figured out, can you implement trunks?
|
||||||
|
//
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const Elephant = struct {
|
||||||
|
letter: u8,
|
||||||
|
tail: ?*Elephant = null,
|
||||||
|
trunk: ?*Elephant = null,
|
||||||
|
visited: bool = false,
|
||||||
|
|
||||||
|
// Elephant tail methods!
|
||||||
|
pub fn getTail(self: *Elephant) *Elephant {
|
||||||
|
return self.tail.?; // Remember, this is means "orelse unreachable"
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hasTail(self: *Elephant) bool {
|
||||||
|
return (self.tail != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Your Elephant trunk methods go here!
|
||||||
|
// ---------------------------------------------------
|
||||||
|
|
||||||
|
???
|
||||||
|
|
||||||
|
// ---------------------------------------------------
|
||||||
|
|
||||||
|
pub fn visit(self: *Elephant) void {
|
||||||
|
self.visited = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print(self: *Elephant) void {
|
||||||
|
// Prints elephant letter and [v]isited
|
||||||
|
var v: u8 = if (self.visited) 'v' else ' ';
|
||||||
|
std.debug.print("{u}{u} ", .{ self.letter, v });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
var elephantA = Elephant{ .letter = 'A' };
|
||||||
|
var elephantB = Elephant{ .letter = 'B' };
|
||||||
|
var elephantC = Elephant{ .letter = 'C' };
|
||||||
|
|
||||||
|
// Link the elephants so that each tail "points" to the next.
|
||||||
|
elephantA.tail = &elephantB;
|
||||||
|
elephantB.tail = &elephantC;
|
||||||
|
|
||||||
|
// And link the elephants so that each trunk "points" to the previous.
|
||||||
|
elephantB.trunk = &elephantA;
|
||||||
|
elephantC.trunk = &elephantB;
|
||||||
|
|
||||||
|
visitElephants(&elephantA);
|
||||||
|
|
||||||
|
std.debug.print("\n", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function visits all elephants twice, tails to trunks.
|
||||||
|
fn visitElephants(first_elephant: *Elephant) void {
|
||||||
|
var e = first_elephant;
|
||||||
|
|
||||||
|
// Follow the tails!
|
||||||
|
while (true) {
|
||||||
|
e.print();
|
||||||
|
e.visit();
|
||||||
|
|
||||||
|
// Get the next elephant or stop.
|
||||||
|
if (e.hasTail()) {
|
||||||
|
e = e.getTail();
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Follow the trunks!
|
||||||
|
while (true) {
|
||||||
|
e.print();
|
||||||
|
|
||||||
|
// Get the previous elephant or stop.
|
||||||
|
if (e.hasTrunk()) {
|
||||||
|
e = e.getTrunk();
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
patches/patches/49_quiz6.patch
Normal file
11
patches/patches/49_quiz6.patch
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
28a29,31
|
||||||
|
> pub fn getTrunk(self: *Elephant) *Elephant {
|
||||||
|
> return self.trunk.?;
|
||||||
|
> }
|
||||||
|
30,31c33,35
|
||||||
|
< ???
|
||||||
|
<
|
||||||
|
---
|
||||||
|
> pub fn hasTrunk(self: *Elephant) bool {
|
||||||
|
> return (self.trunk != null);
|
||||||
|
> }
|
Loading…
Reference in a new issue