Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Dop_Prog_3cem_2010.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
874.5 Кб
Скачать

1.2 Об’єднання та бітові поля

Приклад 1.5

#include <stdio.h>

void main()

{union COM{int a; char c[2];} common={24930}; //0х6162

printf("%c %c\n",common.c[0],common.c[1]);

union UN1 {int i; char ch;} un1={257}; //0x101

union UN2 {char ch; int i;} un2={258}; //0x102

printf("un1.i=%d un1.ch=%d\n",un1.i,un1.ch);

printf("un2.i=%d un2.ch=%d\n",un2.i,un2.ch);}

Приклад 1.6

struct WORDREGS{unsigned int ax, bx, cx, dx;};

struct BYTEREGS{unsigned char al, ah, bl, bh;

unsigned char сl, сh, dl, dh;};

union REGS{WORDREGS x; BYTEREGS h;};

#include <stdio.h>

#include <dos.h>

void main()

{REGS rg;

rg.x.ax=0x4300;

int86(0x2F,&rg,&rg); //прерывание 0x2F

printf("\n al=%#x",rg.h.al);}

Приклад 1.7

#include <stdio.h>

void main()

{union{char ch; struct {int a:5; int b:3; }h;

}cod;

cod.h.a=4; cod.h.b=2;

printf("cod.ch=%c - %#x\n",cod.ch,cod.ch);}

union{char ch;unsigned a:1; unsigned b:2;} y;}

y.ch=43; //0010 1011 в двоичном коде

printf("y.ch=%#x y.a=%#x y.b=%#x\n", y.ch, y.a, y.b);}

2 Функції

2.1 Передача параметрів за значенням

Приклад 2.1

int func1(int a, int b)

{return a+b;}

float sqr(float f, float g)

{ return f*g;}

float cube(float f, float e, float g)

{ return f*g*e;}

int main()

{int am=2, bm=3,res1;

res1=func1(am,bm);

float em=2.5,gm=3.0,fm=4.2,res2,res3,res4, res5;

res2= sqr(fm,gm);

res3= cube(fm,em,gm);

res4=cube(fm,gm,sqr(em,gm));

res5=cube(fm+5,sin(em),cube(fm,5.3,sqr(em,gm)));

int res6,res7,res8;

res6=(int)sqr(em,fm);

res7=(int)sqr(em,float(am));

res8=(int)sqr((float)am, float(bm));

return 0;}

Приклад 2.2

#include <stdio.h>

int sum(int, int);

int main()

{int a1,а2, b=3, g=4;

a1=sum(b, g);

а2=sum(20, g);

printf("Сумма1=%d cумма2=%d \n", a1,a2);

return 0;}

int sum(int arg1, int arg2)

{ return arg1+arg2;}

Приклад 2.3

//файл funcfile1.cpp

int func1(int x1, int x2)

{ return x1*x2;}

//файл funcfile2.cpp

int func2(int x1, int x2)

{ return x1-x2;}

//файл mainfile.cpp

#include <stdio.h>

#include "funcfile1.cpp"

#include "funcfile2.cpp"

void main()

{int a=10, b=4,res1,res2;

res1=func1(a,b);

res2=func2(a,b);

printf("res1=%d res2=%d\n",res1,res2);}

Приклад 2.4

#include <stdio.h>

void func(char simb,int intg,float fltp)

{printf("simb=%c %p\n",simb, &simb);

printf("intg=%d %p\n",intg, &intg);

printf("fltp=%f %p\n",fltp, &fltp);

simb++; intg+=10; fltp*=2;

printf("%c %d %f\n", simb,intg,fltp);

}

void main()

{ char c='A';

int i=200;

float f=100.25;

func(c,a,f);

printf("c=%c %p\n",c,&c);

printf("i=%d %p\n",i,&i);

printf("f=%f %p\n",f,&f);

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]