Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

5 - Strukturované datové typy

Struktura - komplexní čísla

#include <stdio.h>
 
struct komplex
{
	float re, im;
};
 
typedef struct
{
	float re, im;
} complex;
 
struct komplex soucet (struct komplex x, struct komplex y)
{
	struct komplex ret;
	ret.re = x.re + y.re;
	ret.im = x.im + y.im;
	return ret;	
}
 
struct komplex rozdil (struct komplex x, struct komplex y)
{
	return (struct komplex){.re = x.re - y.re, .im = x.im - y.im};
}
 
int main()
{
	struct komplex a;	// promenne datoveho typu struct komplex
	struct komplex b = {1.0, 2.0};
	struct komplex c = { .im = 6.2, .re = 4.3};
 
	a.re = 2.9;
	a.im = 3.14;
 
	printf("a = %.2f + j%.2f\n", a.re, a.im);
	printf("b = %.2f + j%.2f\n", b.re, b.im);
	printf("c = %.2f + j%.2f\n", c.re, c.im);
 
	c = (struct komplex){.re = 1.1, .im = 2.2}; // C99
	printf("c = %.2f + j%.2f\n", c.re, c.im);
 
	c = soucet (a, b);
	printf("c = %.2f + j%.2f\n", c.re, c.im);
 
	a = rozdil (c, b);
	printf("a = %.2f + j%.2f\n", a.re, a.im);
 
	return 0;
}

Union - rozklad float

#include <stdio.h>
 
union integer
{
	unsigned int i; 
	unsigned char c[4];
};
 
union flo
{
	float f;
 
	struct
	{
		unsigned int mantisa:23;
		unsigned int exponent:8;
		unsigned int znamenko:1;
	} s;
};
 
int main()
{
	union integer x;
 
	x.i = 325;
 
	for (int i = 0; i < 4; i++)
		printf("%i ", x.c[i]);
 
	printf("\n");
 
	union flo y;
	y.f = 0.15625;
	printf("mantisa: %x\n", y.s.mantisa);
	printf("exponent: %x\n", y.s.exponent);
	printf("znamenko: %x\n", y.s.znamenko);
 
	return 0;
}

Pole - inicializace (1)

#include <stdio.h>
 
void tisk (int x[], int velikost, char nazev)
{
	printf(" -- vypis pole %c -- \n", nazev);
 
	for (int i = 0; i < velikost; i++)
	{
		printf("[%i] = %i\n", i, x[i]);
	}
}
 
int main()
{
	// neinicializovane pole velikosti 3
	int a[3];
	int b[3] = {0};
	int c[] = {[7] = 110};
	int d[4] = {[2] = 4};
 
	for (int i = 0; i < 3; i++)
	{
		printf("a[%i] = %i\n", i, a[i]);
	}
 
	for (int i = 0; i < sizeof(b)/sizeof(int); i++)
	{
		printf("b[%i] = %i\n", i, b[i]);
	}
 
	tisk(b, sizeof(b)/sizeof(int), 'b');
	tisk(c, sizeof(c)/sizeof(int), 'c');
	tisk(d, sizeof(d)/sizeof(int), 'd');
 
	return 0;
}

Pole - inicializace (2)

#include <stdio.h>
 
void tisk (int x[], int velikost, char nazev)
{
	printf(" -- vypis pole %c -- \n", nazev);
 
	for (int i = 0; i < velikost; i++)
	{
		printf("[%i] = %i\n", i, x[i]);
	}
}
 
int main()
{
	int n, a;
	// velikost pole
	scanf ("%i", &n);
 
	int pole[n];
 
	for (int i = 0; i < n; i++)
	{
		scanf("%i", &a);
		pole[i] = a;
	}
 
	tisk (pole, n, 'p');
 
	return 0;
}

courses/b0b99prpa/solutions/lab05.txt · Last modified: 2019/11/05 20:31 by viteks