Programmeren in C/Struct
Uit Wikibooks
Het commando "struct" maakt het mogelijk om variabelen te groeperen. Zo is het mogelijk om complexe getallen voor te stellen, en er bewerkingen mee uit te voeren (zie voorbeeld).
#include <stdio.h> #include <conio.h> struct complex { int r; int i; }; struct complex lees(void); struct complex som(struct complex a, struct complex b); struct complex product(struct complex a, struct complex b); void schrijf(struct complex c); int main (void) { struct complex c1; struct complex c2; char d; c1 = lees(); c2 = lees(); printf("\nSom=\n"); schrijf(som(c1,c2)); printf("\nProduct=\n"); schrijf(product(c1,c2)); printf("\n"); d=getch(); /*Pause, zodat je zelf beslist wanneer het venster sluit*/ return 1; } struct complex lees(void) { struct complex c; printf("Geef een complex getal (vb. 3+5i)\n"); scanf("%d+%di",&c.r,&c.i); return c; } struct complex som(struct complex a, struct complex b) { struct complex c; c.r = a.r + b.r; c.i = a.i + b.i; return c; } struct complex product(struct complex a, struct complex b) { struct complex c; c.r = a.r*b.r - a.i*b.i; c.i = a.r*b.i + a.i*b.r; return c; } void schrijf(struct complex c) { printf(" %d+%di ", c.r, c.i); }
| Deze pagina is vrijgegeven onder de GNU Free Documentation License (GFDL) en nog niet onder CC-BY-SA. Klik hier voor meer informatie.
Wilt u deze tekst gebruiken onder de Creative Commons CC-BY-SA licentie? |