Menu
CoddyTech

Finał: Transfer konta

Lekcja 48 z 48 w kursie GDScript dla programistów C w Coddy.

challenge icon

Wyzwanie

Łatwy

Zaimplementuj FundsAccount(initial:int) z balance; statyczną funkcję FundsTransfer.move(source:FundsAccount,target:FundsAccount,n:int)->bool. Odrzucaj konta null, ujemną kwotę lub niewystarczające środki na koncie źródłowym bez modyfikowania stanu. W przeciwnym razie wykonaj przelew jeden raz. Przelew na to samo konto kończy się powodzeniem bez zmiany salda, jeśli dostępne środki są wystarczające. Funkcja solve przyjmuje [a,b,n] i zwraca success:a:b. Salda początkowe są nieujemne.

Dane wejściowe to jedna tablica JSON na stdin. Zablokowany plik main ją parsuje, wywołuje publiczne API i wypisuje result=, po którym następuje zwrócony tekst i znak nowej linii. Żadna funkcja pomocnicza nie powinna niczego wypisywać. Niezależne, stałe testy sprawdzają wymagane działanie API przed przypadkiem wejściowym; nie narzucają stylu implementacji.

Kompletna implementacja referencyjna w C. Zapisz te pliki razem i skompiluj main.c kompilatorem C17. Nagłówki udostępniają pomocnicze API; main odczytuje tę samą tablicę JSON i wypisuje wynik w tym samym formacie. Nie są wymagane żadne zewnętrzne biblioteki.

Plik C 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

Plik C 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

Plik C 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;
}

Twój kod umieść w plikach funds_account.gd, funds_transfer.gd i exercise.gd. Plik main.gd zawiera dostarczony kod wejścia/wyjścia i nie można go edytować. Umieść każdą deklarację class_name w pliku o odpowiedniej nazwie.

Spróbuj swoich sił

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))

Wszystkie lekcje w sekcji GDScript dla programistów C

Poćwicz samodzielnie: Kompilator GDScript online