Examination_IT / Programming bases on With C++
.pdf181
@ false, Z ;. ~ F > 0, ;, ; -
x1 x2, d, , -
; x1 x2, ; :
// ‚DJ@F=>?@> <>{>FR<>??DE PQB?>K. *x1 = (-b+sqrt(D))/2.0/a;
*x2 = (-b-sqrt(D))/2.0/a;
5 =,
. s x1 x2,
, , ; x1 x2, . . = ; x1 x2. ~ - d , , , -
, ;@ @ ,
@…
x1[0] = (-b+sqrt(D))/2.0/a; x2[0] = (-b-sqrt(D))/2.0/a;
: Z , . . ;
, @ Z. /,
;.
3 = _tmain. B
= Quadratic:
MaterialRoots = Quadratic(a, b, c, &x1, &x2);
3 – Z==
a, b, c, – x1 x2. 3
, = Quadratic
x1 x2 = _tmain.
6.4.
@ ( )
n. :
182
a, -
, b. : -
n, a b
, @ ; -
. ? =:
; FormkVect –
; Determination – d .
? d ( 6.5) -
= _tmain
Vector. s Vector -
= FormkVect, ;
pVect, ;@ Vector, -
;@ . D -
. 6.6.
B = ; WriteVect, -
;
pText. !, Z =-
WriteVect = _tmain:
WriteVect("„FEQ^?DK <>PRQB:", Vector, n);
„FEQ^?DK <>PRQB:. / =- ; WriteVect Z , ….
8 58 9 6.5. !6:; Ex06_05.cpp
/////////////////////////////////////////////////////////
///
// jHAQ=?;>R CHFF@< <>{>FR<>??DC@ J@F=HC@. void FormkVect(double *pVect, int n)
{
int i;
for(i=0; i<n; i++)
pVect[i] = (rand()%1000-500)/100.0;
}
183
/////////////////////////////////////////////////////////
///
// |>wH>R IH^HJb.
void Determination(double *pVect, int n, double a, double b)
{
int i;
for(i=0; i<n; i++)
{
if(pVect[i]>0.0) pVect[i] -= a; if(pVect[i]<0.0) pVect[i] = b*i;
}
}
/////////////////////////////////////////////////////////
///
//‚D<Q^@R < QP?Q <>PRQB (Q^?QC>B?DK CHFF@<). void WriteVect(char *pText, double *pVect, int n)
{
int i;
cout << pText << endl; for(i=0; i<n; i++)
cout << setw(6) << pVect[i]; cout << endl;
}
/////////////////////////////////////////////////////////
///
//t=H<?H; ABQdBHCCH.
void main()
{
setlocale(LC_CTYPE, "Russian_Russia.1251"); double *Vector;
// „FEQ^?D> ^H??D>. double a = 4.76, b = 1.2; int n; // BHIC>B <>PRQBH
cout << "|HIC>B <>PRQBH> "; cin >> n;
//‚D^>=>?@> AHC;R@. Vector = new double[n];
//jHAQ=?>?@> CHFF@<H <>{>FR<>??DC@ J@F=HC@. FormkVect(Vector, n);
//‚D<Q^ < QP?Q @FEQ^?QdQ CHFF@<H. WriteVect("„FEQ^?DK <>PRQB:", Vector, n);
184
//|>w>?@> IH^HJ@. Determination(Vector, n, a, b);
//‚D<Q^ < QP?Q B>w>?@; IH^HJ@. WriteVect("|>w>?@>:", Vector, n); cout << endl;
//8F<Q9Qk^>?@> AHC;R@.
delete Vector;
// †^>C ?HkHR@; =m9QK P=H<@w@ P=H<@HRbBD. system("pause");
}
D. 6.6. D Ex06_05
6.5
n×m, @ . B
… Z -
( ). 5 . D d 6.6. / =
= FindMaxAndPos. 5 =,
; Matrix
MaxElem Position,
Z -
( ). ? n m ;
.
?, r pRovs,
d Z . :-
, -
… .
185
. ? -
Z i-
; pRovs:
pRovs = Matrix[i];
3 Z
, . F ;@ ;
; Z i-
MaxElem[i] Position[i] .
MaxElem[i] = pRovs[0];
Position[i] = 0;
Z pRovs, -
Z .
/, = FormkVect -
. ? Z Z = …
i- , -
; -
. F = FormkVect
;@ :
FormkVect(Matrix[i], m);
5 = :
FormkVect(&Matrix[i][0], m);
3 _tmain.
r ; -
int **Matrix, *MaxElem, *Position;
B Z . -
n m -
. ? -
186
, -
,
Z .
? =
= FindMaxAndPos. ? = MaxElem Position
@ d . / -
Z . D -
. 6.7.
8 58 9 6.6. !6:; Ex06_06.cpp
#include "stdafx.h" |
// AQRQPQ<DK <<Q^-<D<Q^. |
#include <iostream> |
|
#include <locale.h> |
// ^=; setlocale. |
#include <iomanip> |
|
using namespace std; |
|
/////////////////////////////////////////////////////////
///////
// jHAQ=?;>R CHFF@< c>=DC@ F=bJHK?DC@ J@F=HC@. void FormkVect(int *pVect, int n)
{
int i;
for(i=0; i<n; i++)
pVect[i] = rand()%500-250; }// ]Q?>c ab?Pc@@ FormkVect.
/////////////////////////////////////////////////////////
///////
// ‚D<Q^@R < QP?Q <>PRQB (Q^?QC>B?DK CHFF@<). void WriteVect(char *pText, int *pVect, int n)
{
int i;
cout << pText; for(i=0; i<n; i++)
cout << setw(6) << pVect[i]; cout << endl;
}// ]Q?>c ab?Pc@@ WriteVect.
///////////////////////////////////////////////////////
187
//eHEQ^@R < PHk^QK FRBQP> ^<bEC>B?QK CHRB@cD n*m
//CHPF@CH=f?DK Œ=>C>?R, BHFAQ=Qk>??DK F=><H. void FindMaxAndPos(int **Matrix, int *MaxElem,
int *Position, int n, int m)
{
//pPHIHR>=f ^=; EBH?>?@; H^B>FQ< FRBQP ^<bEC>B?QK
//CHRB@cD.
int *pRovs;
for(int i=0; i<n; i++) // c@P= AQ FRBQPHC CHRB@cD.
{
//jHAQC@?H>C bPHIHR>=f ?H i-m FRBQPb CHPRB@cD. pRovs = Matrix[i];
//GB@?@CH>C, JRQ CHPF@CH=f?DK Œ=>C>?R
//?HEQ^@RRF; < < ?b=><QK ;J>KP> CHFF@<H pRovs. MaxElem[i] = pRovs[0];
//jHAQC@?H>C AQ=Qk>?@> CHPF@CH=f?QdQ Œ=>C>?RH. Position[i] = 0;
for(j=1; j<m; j++) // c@P= AQ Œ=>C>?RHC <>PRQBH.
{
if(pRovs[j]>MaxElem[i])
{
//jHAQC@?H>C ?Q<Q> I?HJ>?@>
//CHPF@CH=f?QdQŒ=>C>?RH.
MaxElem[i] = pRovs[j];
//jHAQC@?H>C ?Q<Q> AQ=Qk>?@>
//CHPF@CH=f?QdQ Œ=>C>?RH. Position[i] = j;
}// ]Q?>c bF=Q<@; if.
}// ]Q?>c c@P=H for AQ j.
}// ]Q?>c c@P=H for AQ i.
}// ]Q?>c ab?Pc@@ FindMaxAndPos.
/////////////////////////////////////////////////////////
///////
// t=H<?H; ABQdBHCCH. void main()
{
setlocale(LC_CTYPE, "Russian_Russia.1251"); int **Matrix, *MaxElem, *Position;
int n, m;
cout << "]Q=@J>FR<Q FRBQP > "; cin >> n;
cout << "]Q=@J>FR<Q FRQ=9cQ<> ";
|
188 |
cin >> m; |
|
// ‚D^>=>?@> AHC;R@ |
// CHPF@CH=f?D> Œ=>C>?RD. |
MaxElem = new int[n]; |
|
Position = new int[n]; |
// AQI@c@@ CHPF. Œ=>C>?RQ<. |
Matrix = new int*[n];// CHRB@cH.
//‚D^>=>?@> AHC;R@ ^=; PHk^QK FRBQP@ ^<bEC>B?QK
//CHRB@cD @ >’ IHAQ=?>?@> F=bJHK?DC@ J@F=HC@. for(int i=0; i<n; i++)
{
Matrix[i] = new int[m]; FormkVect(Matrix[i], m);
}
//eHEQ^@C CHPF@CH=f?D> Œ=>C>?RD @ @E AQI@c@@. FindMaxAndPos(Matrix, MaxElem, Position, n, m);
//‚D<Q^ < QP?Q CHRB@cD
for(int i=0; i<n; i++) WriteVect("", Matrix[i], m);
cout << endl;
//‚D<Q^ < QP?Q CHPF@CH=f?DE Œ=>C>?RQ<. WriteVect("qHPF. Œ=>C>?R: ", MaxElem, n);
//‚D<Q^ < QP?Q AQI@c@@ CHPF@CH=f?DE Œ=>C>?RQ<.
WriteVect("GQI@c@;: |
", Position, n); |
//8F<Q9Qk^>?@> AHC;R@. for(int i=0; i<n; i++)
delete Matrix[i]; delete Matrix;
//†^>C ?HkHR@; =m9QK P=H<@w@ P=H<@HRbBD. system("pause");
}// ]Q?>c ab?Pc@@ _tmain.
189
D. 6.7. D Ex06_06
6.5. W8 6 4 ?9 / AO/8P99
e, , ; -
, Z ; . + C++ -
( ).
D = , -
; =:
double sin(double x); double cos(double x); double sqrt(double x); double MyFunc(double x); double Function35(double x);
Z = ;
double (*pfd)(double);
=
double Discr(double *x, int n, int m); double Matr (double *a, int n1, int n2);
190
double (*pf) (double*, int, int);
: ; ;@ =
:
const double PI = 3.1415926535897932; double y;
double (*pf)(double); // Q9:;<=>?@> bPHIHR>=; ?H ab?Pc@m.
pf = sin; // bPHIHR>=f ?H FRH?^HBR?Dm ab?Pc@m sin. y = pf(PI/4.0);
cout >> "sin(PI/4) = " << y << endl;
pf = cos; // bPHIHR>=f ?H FRH?^HBR?Dm ab?Pc@m cos. y = pf(PI/2.0);
cout >> "cos(PI/2) = " << y << endl;
. Z = r = ;, - @ ;@ ; double ;@ ; double. : Z , , -
= sin:
pf = sin; |
// bPHIHR>=f ?H FRH?^HBR?Dm ab?Pc@m sin. |
s Z ,
, , :
y = pf(PI/4.0);
? Z y
= sin(#$4). Z
= cos.
!, Z -
=, Z ;@ ; .
6.6.