P
ROGRAMOWANIE I JĘZYK
C++
/
A
NALIZA PROGRAMÓW NR
2
Temat: Zmienne typu wskaźnikowego i referencyjnego - arytmetyka wskaźników
Zadanie
Wykonać analizę poniższych programów. Nie używając komputera obliczyć wartości zmiennych określonych
jako OUTPUT przy danych wejściowych INPUT. Sprawdzić wyniki uruchamiając odpowiedni kod programu
na komputerze.
Cześć 1: Struktury.
1.
// INPUT : wspolrzedne srodka okregu o2, (x,y) = (2.5, 3.0)
// promien okregu o2, r = 6
// OUTPUT : odl, L1, S2
const
double
pi = 3.14;
struct
okrag
{
double
x,y;
int
r;
} o1 = {1.5, 0.5, 5};
okrag o2;
cout <<
"Podaj wspolrzedne srodka oraz promien drugiego okregu ..."
<< endl;
cout <<
"x = "
; cin >> o2.x;
cout <<
"y = "
; cin >> o2.y;
cout <<
"r = "
; cin >> o2.r;
double
odl = sqrt( (o1.x-o2.x)*(o1.x-o2.x) + pow(o1.y-o2.y,2) );
double
L1,S2;
L1 = 2.0*pi*o1.r;
S2 = pi*o2.r*o2.r;
2.
// INPUT : -
// OUTPUT : suma.re, suma.im, iloczyn.re, iloczyn.im
struct
liczba_zespolona
{
double
re;
double
im;
} suma, iloczyn;
liczba_zespolona z,w = {2.0,-3.0};
z.re = -5.0;
z.im = 4.0;
// Obliczamy sume liczb zespolonych : z + w
suma.re = z.re + w.re;
suma.im = z.im + w.im;
// Obliczamy iloczyn liczb zespolonych : z * w
iloczyn.re = z.re*w.re - z.im*w.im;
iloczyn.im = z.im*w.re + z.re*w.im;
Cześć 2: Wskaźniki.
1.
// INPUT : adres zmiennej A = 1000, adres zmiennej B = 2000,
// adres zmiennej c = 3000
// OUTPUT : podac wartosci, ktore wypisze program
int
A,B,C=25;
int
* pA = &A, * pB = 0, * pC = 0;
A = 5; B = 12;
cout <<
"A = "
<< A << endl;
cout <<
"B = "
<< B << endl;
cout <<
"C = "
<< C << endl << endl;
cout <<
"pA = "
<< pA << endl;
cout <<
"pB = "
<< pB << endl;
cout <<
"pC = "
<< pC << endl << endl;
B = A + 10;
pB = &B;
cout <<
"B = "
<< B << endl;
cout <<
"pB = "
<< pB << endl << endl;
C = *pA + *pB;
pC = &C;
cout <<
"C = "
<< C << endl;
cout <<
"pC = "
<< pC << endl << endl;
2.
// INPUT : adres zmiennej a = 1200, adres zmiennej b = 3000,
// adres zmiennej c = 2000
// OUTPUT : podac wartosci, ktore wypisze program
int
a,b,c;
int
* ptr1 = &a, * ptr2 = &b;
a = 3; b = 10;
c = (a++) - (--b);
cout <<
"a = "
<< a << endl;
cout <<
"b = "
<< b << endl;
cout <<
"c = "
<< c << endl << endl;
cout <<
"*ptr1 = "
<< *ptr1 << endl;
cout <<
"*ptr2 = "
<< *ptr2 << endl << endl;
*ptr1 = c + 20;
ptr2 = &c;
cout <<
"ptr1 = "
<< ptr1 << endl;
cout <<
"*ptr1 = "
<< *ptr1 << endl << endl;
cout <<
"c = "
<< c << endl;
cout <<
"*ptr2 = "
<< *ptr2 << endl;
Cześć 3: Referencje.
1.
// INPUT : -
// OUTPUT : podac wartosci, ktore wypisze program
int
x = 4, y = 7;
int
&ref_x = x;
int
&ref_y = y;
cout <<
"x = "
<< x << endl;
cout <<
"y = "
<< y << endl;
cout <<
"ref_x = "
<< ref_x << endl;
cout <<
"ref_y = "
<< ref_y << endl << endl;
ref_x = (++y) - 10;
cout <<
"x = "
<< x << endl;
cout <<
"ref_y = "
<< ref_y << endl << endl;
2.
// INPUT : -
// OUTPUT : podac wartosci, ktore wypisze program
int
x = 4, y, z = 12;
int
&ref_x = x;
int
&ref_z = z;
cout <<
"x = "
<< x << endl;
cout <<
"z = "
<< z << endl;
cout <<
"ref_x = "
<< ref_x << endl;
cout <<
"ref_z = "
<< ref_z << endl << endl;
int
&ref = y;
ref = (ref_x < z) ? 3:30;
cout <<
"y = "
<< y << endl;
cout <<
"ref = "
<< ref << endl << endl;
ref_x = (--z) - (ref++);
cout <<
"x = "
<< x << endl;
cout <<
"y = "
<< y << endl;
cout <<
"z = "
<< z << endl;
cout <<
"ref_x = "
<< ref_x << endl;
cout <<
"ref = "
<< ref << endl;
cout <<
"ref_z = "
<< ref_z << endl << endl;
Cześć 4: Arytmetyka wskaźników.
1.
// INPUT : adres pierwszego elementu tablicy jest rowny 1000
// OUTPUT : podac wartosci, ktore wypisze program
int
A[6] = {2, 4, 6, 8, 10, 12};
int
* pA0, * pA1;
int
diff;
pA0 = &A[0];
pA1 = A;
cout <<
"A[0] = "
<< A[0] << endl;
cout <<
"pA0 = "
<< pA0 << endl;
cout <<
"pA1 = "
<< pA1 << endl << endl;
pA0 = pA0++;
pA1 = pA1 + 4;
cout <<
"pA0 = "
<< pA0 << endl;
cout <<
"pA1 = "
<< pA1 << endl << endl;
diff = pA1 - pA0;
cout <<
"pA1 - pA0 = "
<< diff << endl;
2.
// INPUT : adres pierwszego elementu tablicy jest rowny 2000
// OUTPUT : podac wartosci, ktore wypisze program
double
A[6] = {2, 4, 6, 8, 10, 12};
double
* ptrA;
ptrA = &A[0];
// Tablica w wersji pierwszej
for
(
int
i=0; i<=5; i++)
cout <<
"A["
<< i <<
"] = "
<< A[i] << endl;
cout << endl;
// I - Operacje na tablicy A
for
(
int
i=0; i<=5; i++)
*(A + i) = A[i] + 5;
for
(
int
i=0; i<=5; i++)
cout <<
"A["
<< i <<
"] = "
<< A[i] << endl;
cout << endl;
// II - Operacje na tablicy A
for
(
int
i=0; i<=5; i++)
ptrA[i] = A[i] + 10;
for
(
int
i=0; i<=5; i++)
cout <<
"A["
<< i <<
"] = "
<< A[i] << endl;
cout << endl;
// III - Operacje na tablicy A
for
(
int
i=0; i<=5; i++)
*(ptrA + i) = A[i] - 1;
for
(
int
i=0; i<=5; i++)
cout <<
"A["
<< i <<
"] = "
<< A[i] << endl;
cout << endl;
3.
// INPUT : adres pierwszego elementu tablicy jest rowny 1500
// OUTPUT : podac wartosci, ktore wypisze program
double
A[6] = {2, 4, 6, 8, 10, 12};
double
* ptr1, * ptr2, * ptr3;
int
diff1,diff2;
ptr1 = A;
ptr2 = &A[2];
ptr3 = &A[5];
cout <<
"ptr1 = "
<< ptr1 << endl;
cout <<
"ptr2 = "
<< ptr2 << endl;
cout <<
"ptr3 = "
<< ptr3 << endl << endl;
cout <<
"*ptr1 = "
<< *ptr1 << endl;
cout <<
"*ptr2 = "
<< *ptr2 << endl;
cout <<
"*ptr3 = "
<< *ptr3 << endl << endl;
for
(
int
i=3; i<=5; i++)
*(ptr1 + i) = A[i] + 10;
for
(
int
i=3; i<=5; i++)
cout <<
"*(ptr1 + "
<< i <<
") = "
<< *(ptr1 + i) << endl;
cout << endl;
for
(
int
i=0; i<=5; i++)
cout <<
"A["
<< i <<
"] = "
<< A[i] << endl;
cout << endl;
ptr3 = ptr3 - 1;
diff1 = ptr3 - ptr1;
cout <<
"ptr3 = "
<< ptr3 << endl;
cout <<
"*ptr3 = "
<< *ptr3 << endl;
cout <<
"diff1 = ptr3 - ptr1 = "
<< diff1 << endl << endl;
ptr2 = ptr2 + 3;
diff2 = ptr2 - ptr1;
cout <<
"ptr2 = "
<< ptr2 << endl;
cout <<
"*ptr2 = "
<< *ptr2 << endl;
cout <<
"diff2 = ptr2 - ptr1 = "
<< diff2 << endl << endl;