Menu
CoddyTech

הסבה: פנקס ציונים של מדיניות

שיעור 48 מתוך 48 בקורס Swift למפתחי C של Coddy.

challenge icon

אתגר

בינוני

העבר את מימוש הייחוס ב-C. מימוש ScoreRule שדורש את accepts(_ score: Int) -> Bool, ואת PassRule(minimum:) שמקבל ציונים שגדולים או שווים ל-minimum. מימוש טיפוס ערך GradeBook() עם record(_ name: String, score: Int) -> Bool משנה־המצב ועם report(using rule: any ScoreRule) -> String. יש לקבל ציונים מ-0 עד 100; יש להחליף ציון קודם עבור אותו שם. יש לדחות ציונים אחרים ללא שינוי. יש לדווח רק על רשומות שהכלל שסופק מקבל, ממוינות לפי שם, כמחרוזת name:score המופרדת ברווחים. כללים מותאמים אישית והעתקים עצמאיים צריכים לפעול. הקלט הוא minimum בין 0 ל-100, ואחריו 0 עד 30 זוגות של שם/ציון. השמות הם מילים באותיות ASCII קטנות, באורך של עד 10 אותיות; הציונים הם בין ‎-10 ל-110. הפלט הוא מספר פעולות הרשומה שהתקבלו, ואחריו רווח ודוח רק אם הוא אינו ריק.

ממשו את ממשקי ה-API הנדרשים ואת solve(_ input: String) -> String. החזירו את טקסט הפלט ללא תו ירידת שורה בסוף; אין להדפיס בקוד שניתן לעריכה. מנהל ההתקן הנעול בודק באופן עצמאי את ממשקי ה-API גם על קלטים תקינים נוספים, ואז מדפיס את הטקסט שהוחזר עם תו ירידת שורה אחד בדיוק. כל הפעולות המספריות נשארות בגבולות Int.

ה-ScoreRule ב-C משלב callback עם מצביע הקשר והופך לפרוטוקול ב-Swift. ה-GradeBook ב-C מאחסן מערכים של רשומות בקיבולת קבועה בתוך המבנה, כך שהשמה של מבנה מעתיקה את ערכי הרשומות שלו באופן עצמאי. ה-GradeBook הנדרש ב-Swift הוא struct שהערך המועתק שלו חייב להתפתח באופן עצמאי. הקשר המדיניות ב-C נשאר תקף לכל קריאה חוזרת במהלך הדיווח. יש לקרוא לכלל שסופק עבור כל ציון כדי לתמוך גם במדיניות מותאמות אישית.

תוכנית הייחוס המלאה ב-C. יש לקמפל את main.c כשהכותרות המוצגות נמצאות לצדו; main היא נקודת הכניסה היחידה. המימוש מלא בכוונה עבור ההעברה הסופית הזו.

score_rule.h

#ifndef SCORE_RULE_H
#define SCORE_RULE_H
#include <stdbool.h>
typedef struct { const void *context; bool (*accepts)(const void *, int); } ScoreRule;
#endif

pass_rule.h

#ifndef PASS_RULE_H
#define PASS_RULE_H
#include "score_rule.h"
typedef struct { int minimum; } PassRule;
static bool pass_accepts(const void *context, int score) {
    return score >= ((const PassRule *)context)->minimum;
}
#endif

grade_book.h

#ifndef GRADE_BOOK_H
#define GRADE_BOOK_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "score_rule.h"
typedef struct { char name[11]; int score; } Grade;
typedef struct { Grade grades[30]; int size; } GradeBook;
static bool grade_book_record(GradeBook *book, const char *name, int score) {
    if (score < 0 || score > 100) return false;
    int index = 0;
    while (index < book->size && strcmp(book->grades[index].name, name) != 0) ++index;
    if (index == book->size) {
        strcpy(book->grades[index].name, name);
        ++book->size;
    }
    book->grades[index].score = score;
    return true;
}
static int grade_compare(const void *left, const void *right) {
    return strcmp(((const Grade *)left)->name, ((const Grade *)right)->name);
}
static void grade_book_report(const GradeBook *book, ScoreRule rule, char output[1024]) {
    Grade sorted[30];
    memcpy(sorted, book->grades, (size_t)book->size * sizeof(Grade));
    qsort(sorted, (size_t)book->size, sizeof(Grade), grade_compare);
    size_t used = 0;
    output[0] = '\0';
    for (int i = 0; i < book->size; ++i) {
        if (!rule.accepts(rule.context, sorted[i].score)) continue;
        used += (size_t)snprintf(output + used, 1024 - used, "%s%s:%d", used ? " " : "", sorted[i].name, sorted[i].score);
    }
}
#endif

main.c

#include "pass_rule.h"
#include "grade_book.h"
int main(void) {
    PassRule policy;
    if (scanf("%d", &policy.minimum) != 1) return 0;
    ScoreRule rule = {&policy, pass_accepts};
    GradeBook book = {0};
    char name[11], report[1024];
    int score, accepted = 0;
    while (scanf("%10s %d", name, &score) == 2)
        if (grade_book_record(&book, name, score)) ++accepted;
    grade_book_report(&book, rule, report);
    printf("%d%s%s\n", accepted, report[0] ? " " : "", report);
    return 0;
}

ממשו את ממשקי ה-API של Swift בקבצים ScoreRule.swift, PassRule.swift, GradeBook.swift, Functions.swift. השאירו את מנהל ההתקן שסופק ב-main.swift.

נסו בעצמכם

import Foundation
func apiChecks(_ input: String) -> Bool {
struct ProbeRule: ScoreRule { func accepts(_ score: Int) -> Bool { score % 2 == 0 } }
var b = GradeBook(); guard b.record("a", score: 40) && b.record("b", score: 71) && !b.record("a", score: 101) else { return false }; var copy = b; _ = copy.record("a", score: 88); return b.report(using: ProbeRule()) == "a:40" && copy.report(using: ProbeRule()) == "a:88" && b.report(using: PassRule(minimum: 40)) == "a:40 b:71" && PassRule(minimum: 50).accepts(50)
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// תחילת בדיקות ה־API הציבורי
let apiOK = apiChecks(input)
// סיום בדיקות ה־API הציבורי
if apiOK { print(solve(input)) } else { print("API mismatch") }

כל השיעורים ביחידה Swift למפתחי C

תרגלו בעצמכם: Swift אונליין