Finished assignment. Added function to calculate testat length, generate random order and print plan.

main
2wenty1ne 2024-10-27 13:11:03 +01:00
parent 8b75a185a2
commit 6739b986ab
1 changed files with 61 additions and 41 deletions

View File

@ -1,5 +1,4 @@
const std = @import("std"); const std = @import("std");
const writer = std.io.getStdOut().writer();
const Timestamp = struct { const Timestamp = struct {
@ -8,50 +7,49 @@ const Timestamp = struct {
}; };
const testees:[]const []const u8 = &[_][] const u8 { const testees:[]const []const u8 = &[_][] const u8 {
"1", "Gruppe A",
"2", "Gruppe B",
"3", "Gruppe C",
"4", "Gruppe D",
"5", "Gruppe E",
"6", "Gruppe F",
"7", "Gruppe G",
"8" "Gruppe H",
"Gruppe I",
"Gruppe J",
}; };
const number_of_testees: u64 = testees.len; // len von testees const number_of_testees: u64 = testees.len; // len von testees
const total_minutes: u64 = 60; // Gesamte Dauer in min const total_minutes: u64 = 190; // Gesamte Dauer in min
const pause_minutes: u64 = 2; // Länge der Pause zwischen 2 Testaten const pause_minutes: u64 = 5; // Länge der Pause zwischen 2 Testaten
pub fn main () !void { pub fn main () !void {
var testStamp = Timestamp { var time = Timestamp {
.hours = 13, .hours = 12,
.minutes = 5, .minutes = 0,
}; };
try writer.print("Amount tests: {d} \n\n", .{number_of_testees}); try printPlan(&time);
try printTimestamp(&testStamp);
try writer.print("\n", .{});
try printPlan();
try addMinutesToTimestamp(&testStamp, 175);
try printTimestamp(&testStamp);
} }
// Printet timestamp im Format HH:MM
//? Printet timestamp im Format HH:MM
pub fn printTimestamp(ts: *Timestamp) !void { pub fn printTimestamp(ts: *Timestamp) !void {
const writer = std.io.getStdOut().writer();
try writer.print("{0d:0>2}:{1d:0>2}", .{ ts.hours, ts.minutes }); try writer.print("{0d:0>2}:{1d:0>2}", .{ ts.hours, ts.minutes });
} }
pub fn addMinutesToTimestamp(ts: *Timestamp, min: u16) !void{
//? Add a given minute amount to the given timestamp
pub fn addMinutesToTimestamp(ts: *Timestamp, min: u16) void{
var addHours: u64 = min / 60; var addHours: u64 = min / 60;
var addMinutes: u64 = min%60; var addMinutes: u64 = min%60;
if ((addMinutes + ts.minutes) >= 60){ if ((addMinutes + ts.minutes) >= 60){
addMinutes = (addMinutes + ts.minutes)%60; addMinutes = (addMinutes + ts.minutes)%60;
addHours += 1; addHours += 1;
@ -60,27 +58,34 @@ pub fn addMinutesToTimestamp(ts: *Timestamp, min: u16) !void{
ts.hours += addHours; ts.hours += addHours;
ts.minutes += addMinutes; ts.minutes += addMinutes;
} }
pub fn printPlan() !void {
//? Print the whole test-plan
pub fn printPlan(ts: *Timestamp) !void {
const rand = std.crypto.random; const rand = std.crypto.random;
const writer = std.io.getStdOut().writer();
//? BLOCK CALCULATION
const testTime: u64 = total_minutes - ((number_of_testees - 1) * pause_minutes);
const timePerTest: u64 = testTime / number_of_testees;
//? CREATE RANDOM ORDER ARRAY
const noneNumber:u64 = number_of_testees+1; const noneNumber:u64 = number_of_testees+1;
var testees_order: [number_of_testees]u64 = undefined;
var testees_order: [number_of_testees]u8 = undefined; //? Fill order array with noneNumber
// Fill order array with noneNumber
for (testees_order, 0..) |_, index| { for (testees_order, 0..) |_, index| {
testees_order[index] = noneNumber; testees_order[index] = noneNumber;
} }
//? Fill order array with order until full
// Fill order array with a random order
for (testees, 0..) |_, blockIndex| { for (testees, 0..) |_, blockIndex| {
while(true) { while(true) {
const randIndex:u8 = rand.intRangeLessThan(u8, 0, number_of_testees); const randIndex:u64 = rand.intRangeLessThan(u64, 0, number_of_testees);
if (arrayContains(&testees_order, randIndex)) { if (arrayContains(&testees_order, randIndex)) {
continue; continue;
@ -90,12 +95,27 @@ pub fn printPlan() !void {
} }
} }
for (testees_order, 0..) |elem, index| {
try writer.print("Nr.{d}: {s} \n", .{index, testees[elem]}); //? PLAN OUTPUT
try writer.print("{d} Gruppen x {d} Minuten ({d} Minuten Pause dazwischen) \n", .{number_of_testees, timePerTest, pause_minutes});
try writer.print("Beginn des Testat-Termins: ", .{});
try printTimestamp(ts);
try writer.print("\n", .{});
try writer.print("Testat-Bloecke: \n", .{});
for (testees_order, 0..) |testee, index| {
try writer.print("{d}.) ", .{index+1});
try printTimestamp(ts);
try writer.print("-", .{});
addMinutesToTimestamp(ts, timePerTest);
try printTimestamp(ts);
addMinutesToTimestamp(ts, pause_minutes);
try writer.print(": {s} \n", .{testees[testee]});
} }
} }
pub fn arrayContains(arr: []u8, q: i64) bool { // Check if a given array contains a given element
pub fn arrayContains(arr: []u64, q: u64) bool {
for (arr) |element| { for (arr) |element| {
if (element == q) { if (element == q) {
return true; return true;