Menu
CoddyTech

Abschluss: Kontoübertragung

Lektion 48 von 48 im Kurs GDScript für C-Entwickler von Coddy.

challenge icon

Aufgabe

Einfach

Implementiere FundsAccount(initial:int) mit balance; static FundsTransfer.move(source:FundsAccount,target:FundsAccount,n:int)->bool. Weise null-Konten, negative Beträge oder unzureichendes Guthaben auf dem Quellkonto zurück, ohne etwas zu verändern. Andernfalls führe die Übertragung einmal aus. Eine Übertragung auf dasselbe Konto ist erfolgreich und verändert den Kontostand nicht, sofern der Betrag gedeckt ist. Solve nimmt [a,b,n] entgegen und gibt success:a:b zurück. Die Anfangsguthaben sind nicht negativ.

Die Eingabe ist ein JSON-Array auf stdin. Das gesperrte Hauptprogramm parst es, ruft die öffentlichen APIs auf und gibt result= gefolgt vom zurückgegebenen Text und einem Zeilenumbruch aus. Keine Hilfsfunktion sollte etwas ausgeben. Unabhängige, festgelegte Tests überprüfen das erforderliche API-Verhalten vor dem Eingabefall; sie schreiben keinen Implementierungsstil vor.

Vollständige C-Referenz. Speichere diese Dateien zusammen und kompiliere main.c mit einem C17-Compiler. Die Header stellen die Hilfs-API bereit; main liest dasselbe JSON-Array und gibt dasselbe Ergebnisprotokoll aus. Es ist keine externe Bibliothek erforderlich.

C-Datei account.h:

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <stdint.h>
struct Account { int64_t balance; };
static inline struct Account account_make(int64_t initial) {
    return (struct Account){initial};
}
#endif

C-Datei transfers.h:

#ifndef TRANSFERS_H
#define TRANSFERS_H
#include <stdbool.h>
#include "account.h"
static inline bool transfer_move(struct Account *source,
                                 struct Account *target, int64_t amount) {
    if (!source || !target || amount<0 || amount>source->balance) return false;
    if (source==target) return true;
    /* Match the reference's unchecked 64-bit arithmetic without C overflow. */
    uint64_t sum=(uint64_t)target->balance+(uint64_t)amount;
    source->balance-=amount;
    target->balance=sum <= INT64_MAX ? (int64_t)sum : -1-(int64_t)(UINT64_MAX-sum);
    return true;
}
#endif

C-Datei main.c:

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "transfers.h"
int main(int argc, char **argv) {
    if (argc>1 && strcmp(argv[1],"--probe")==0) {
        struct Account a=account_make(6);
        if (transfer_move(NULL,&a,0) || transfer_move(&a,NULL,0) ||
            !transfer_move(&a,&a,4) || a.balance!=6 ||
            transfer_move(&a,&a,7) || a.balance!=6) return 1;
        puts("contract-ok"); return 0;
    }
    int64_t a,b,n;
    if (scanf(" [ %" SCNd64 " , %" SCNd64 " , %" SCNd64 " ]",&a,&b,&n)!=3) return 1;
    struct Account source=account_make(a),target=account_make(b);
    bool moved=transfer_move(&source,&target,n);
    printf("result=%s:%" PRId64 ":%" PRId64 "\n",
           moved?"true":"false",source.balance,target.balance);
    return 0;
}

Dein Code gehört in funds_account.gd, funds_transfer.gd und exercise.gd. main.gd enthält den bereitgestellten Ein-/Ausgabecode und darf nicht bearbeitet werden. Belasse jeden class_name-Typ in der Datei, deren Name ihm entspricht.

Probier es selbst

extends Node

func contract_ok(data: Array) -> bool:
	var a: FundsAccount = FundsAccount.new(6)
	var b: FundsAccount = FundsAccount.new(2)
	if FundsTransfer.move(null,b,0) or FundsTransfer.move(a,null,0) or FundsTransfer.move(a,b,7) or FundsTransfer.move(a,b,-1): return false
	if a.balance != 6 or b.balance != 2: return false
	if not FundsTransfer.move(a,a,4) or a.balance != 6: return false
	if not FundsTransfer.move(a,b,3) or a.balance != 3 or b.balance != 5: return false
	return true

func _ready() -> void:
	var data: Array = JSON.parse_string(OS.read_string_from_stdin())
	if not contract_ok(data):
		print("API_CONTRACT_FAILED")
		return
	print("result=" + Exercise.solve(data))

Alle Lektionen in GDScript für C-Entwickler

Übe selbstständig: Online-GDScript-Compiler