Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
#include <stdlib.h> /* malloc, exit, NULL */ #include <stdio.h> /* printf */ typedef const char* String; typedef struct Account* Account; long next_number = 1; /* nächste zu vergebende Kontonummer */ /* Konto. */ struct Account { long number; /* Kontonummer. */ String holder; /* Kontoinhaber. */ long balance; /* Kontostand in Cent. */ }; /* Gebührenpflichtiges Konto */ struct ChargedAccount { struct Account Account; /* Property-Vererbung struct Account*/ int count; }; /* Dynamisches Objekt der Größe n Byte erzeugen. */ void* new_object (int n) { void* this = malloc(n); if (this == NULL) { printf("new_object: out of memory\n"); exit(1); } return this; } /* Konto mit Inhaber h erzeugen. */ struct Account* new_account (String h) { struct Account* this = new_object(sizeof(struct Account)); this->number = next_number++; this->holder = h; this->balance = 0; return this; } /* gebührenpflichtiges Konto initialisieren */ void init_charged_Account (struct ChargedAccount* this ,String h){ this->Account.number = next_number++; this->Account.holder = h; this->Account.balance = 0; } /* Neues gebührenpflichtiges Konto */ struct ChargedAccount* new_charged_account (String h){ struct ChargedAccount* this = new_object(sizeof(struct ChargedAccount)); init_charged_Account(this, h); return this; } /* Abhebung für gebührenpflichtige Konten */ void withdraw_ChargedAccount (struct ChargedAccount* this, long amount) { withdraw(&(this->Account), amount); this->count++; } /* Einzahlung für gebührenpflichtige Konten */ void deposit_ChargedAccount (struct ChargedAccount* this, long amount) { deposit(&(this->Account), amount); this->count++; } /* Nummer von Konto this liefern. */ long number (struct Account* this) { return this->number; } /* Inhaber von Konto this liefern. */ String holder (struct Account* this) { return this->holder; } /* Kontostand von Konto this liefern. */ long balance (struct Account* this) { return this->balance; } /* Betrag amount auf Konto this einzahlen. */ void deposit (struct Account* this, long amount) { this->balance += amount; } /* Betrag amount von Konto this abheben. */ void withdraw (struct Account* this, long amount) { this->balance -= amount; } /* Betrag amount von Konto this auf Konto dest �berweisen. */ void transfer (struct Account* this, long amount, struct Account* dest) { withdraw(this, amount); deposit(dest, amount); } /* Zeiger auf Kontoobjekt. */ int main () { /* Zwei Konten erzeugen. */ Account a = new_account("Hans Maier"); Account b = new_account("Fritz Müller"); /* 1000 Cent auf Konto a einzahlen, */ /* dann 300 Cent auf Konto b �berweisen. */ deposit(a, 1000); transfer(a, 300, b); /* Kontodaten ausgeben. */ printf("Konto a: %ld, %s, %ld\n", number(a), holder(a), balance(a)); printf("Konto b: %ld, %s, %ld\n", number(b), holder(b), balance(b)); return 0; } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Affje« (19.03.2011, 13:53)
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include <stdlib.h> /* malloc, exit, NULL */ #include <stdio.h> /* printf */ typedef const char* String; //typedef struct Account* Account; WTF?! long next_number = 1; /* nächste zu vergebende Kontonummer */ /* Konto. */ struct Account { long number; /* Kontonummer. */ String holder; /* Kontoinhaber. */ long balance; /* Kontostand in Cent. */ }; /* Gebührenpflichtiges Konto */ struct ChargedAccount { struct Account Account; /* Property-Vererbung struct Account*/ int count; }; /* Dynamisches Objekt der Größe n Byte erzeugen. */ void* new_object (int n) { void* t = malloc(n); if (t == NULL) { printf("new_object: out of memory\n"); exit(1); } return t; } /* Betrag amount auf Konto this einzahlen. */ void deposit (struct Account* t, long amount) { t->balance += amount; } /* Betrag amount von Konto this abheben. */ void withdraw (struct Account* t, long amount) { t->balance -= amount; } /* Konto mit Inhaber h erzeugen. */ struct Account* new_account (String h) { struct Account* t = (Account*) new_object(sizeof(struct Account)); t->number = next_number++; t->holder = h; t->balance = 0; return t; } /* gebührenpflichtiges Konto initialisieren */ void init_charged_Account (struct ChargedAccount* t ,String h){ t->Account.number = next_number++; t->Account.holder = h; t->Account.balance = 0; } /* Neues gebührenpflichtiges Konto */ struct ChargedAccount* new_charged_account (String h){ struct ChargedAccount* t = (ChargedAccount*) new_object(sizeof(struct ChargedAccount)); init_charged_Account(t, h); return t; } /* Abhebung für gebührenpflichtige Konten */ void withdraw_ChargedAccount (struct ChargedAccount* t, long amount) { withdraw(&(t->Account), amount); t->count++; } /* Einzahlung für gebührenpflichtige Konten */ void deposit_ChargedAccount (struct ChargedAccount* t, long amount) { deposit(&(t->Account), amount); t->count++; } /* Nummer von Konto this liefern. */ long number (struct Account* t) { return t->number; } /* Inhaber von Konto this liefern. */ String holder (struct Account* t) { return t->holder; } /* Kontostand von Konto this liefern. */ long balance (struct Account* t) { return t->balance; } /* Betrag amount von Konto this auf Konto dest �berweisen. */ void transfer (struct Account* t, long amount, struct Account* dest) { withdraw(t, amount); deposit(dest, amount); } /* Zeiger auf Kontoobjekt. */ int main () { /* Zwei Konten erzeugen. */ Account* a = (Account*) new_account("Hans Maier"); Account* b = (Account*) new_account("Fritz Müller"); /* 1000 Cent auf Konto a einzahlen, */ /* dann 300 Cent auf Konto b �berweisen. */ deposit(a, 1000); transfer(a, 300, b); /* Kontodaten ausgeben. */ printf("Konto a: %ld, %s, %ld\n", number(a), holder(a), balance(a)); printf("Konto b: %ld, %s, %ld\n", number(b), holder(b), balance(b)); return 0; } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Werbeanzeige