< Programmeren in C

Programmeren in C

Inleiding
  1. Inleiding
  2. De compiler

Bewerkingen

  1. Basis
  2. Stijl en structuur
  3. Datatypes
  4. Berekeningen
  5. If en loops
  6. Arrays en pointers
  7. Functies
  8. File handling

Overige

  1. Bestanden
  2. C-Preprocessor (CPP)
  3. Struct
  4. Expressies

Het commando "struct" maakt het mogelijk om variabelen te groeperen. Zo is het mogelijk om bijvoorbeeld complexe getallen voor te stellen en er bewerkingen mee uit te voeren (zie voorbeeld hieronder).

#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);
}
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.