. . , . .
++
-
-
681.142.2(072)
32.973.26-018.173
69
:
. . , . .
:
- , . .,
- , , . .
-
29 2005 , ___
, 2- , C++. , 1- 2- - . 2-3 . 14 .
681.142.2(072)
32.973.26-018.173
ã , 2005
ã , 2005
1 .
. . - ():
1) ;
2) () , ;
3) ;
4) .
:
class _ {
private
Public: //
};
. . . , , - .
, private, . , public:. private, public.
++ (struct) (union). , . , .
- , , , -. . . inline -. - . _::.
|
|
_:: _ ( )
{ /* */ }
:
_._();
__ -> _();
, . - this , .
- , friend, this . , , . ++, .
.
/* Student. . : , , .*/
#include <conio.h>
#include <string.h>
#include <iostream.h>
struct date //
{char daymon[6];
int year; };
//======= class Student =================
class Student{
char name[30]; //private
date t;
char adr[30], fac[20];
int kurs;
public:
Student();
char *getfac();
int getkurs();
void show();
};
Student::Student()
{cout<<"Input name:"; cin>>name;
cout<<"Input date of born\n";
cout<<"Day.mon:"; cin>>t.daymon;
cout<<"Year:"; cin>>t.year;
cout<<"Input adr:"; cin>>adr;
cout<<"Input fac:"; cin>>fac;
cout<<"Input kurs:"; cin>>kurs;
}
void Student::show()
{
cout<<"Name:"<<name<<endl;
cout<<"Was born:"<<t.daymon<<'.'<<t.year<<endl;
cout<<"Address:"<<adr<<endl;
cout<<"Fac:"<<fac<<endl;
cout<<"Kurs:"<<kurs<<endl;
}
char *Student::getfac() { return fac; }
int Student::getkurs() { return kurs; }
void spisfac(Student spis[],int n)//
{char fac[20];
cout<<"Input faculty:"; cin>>fac;
for(int i=0;i<n;i++)
if(strcmp(spis[i].getfac(),fac)==0)spis[i].show();
}
void spisfackurs(Student spis[],int n)
//
{int i,k;
char fac[20];
cout<<"Input faculty:"; cin>>fac;
cout<<"Input the course:"; cin>>k;
for(i=0;i<n;i++)
if ((strcmp(spis[i].getfac(),fac)==0)&&(spis[i].getkurs()==k))
spis[i].show();
}
//========= main ================
void main()
{ Student *spis;
int n;
cout<<"Input a number of students: "; cin>>n;
spis=new Student [n];
for(int i=0;i<n;i++) {
cout<<"=============================="<<endl;
spis[i].show();
}
spisfac(spis,n);
spisfackurs(spis,n);
delete [] spis;
cout<<"press any key!"
while(!kbhit());
}
|
|
. set (), get (), show (). .
1. Student: , , , , , , , . . :
) ;
) ;
) , .
2. Abiturient: , , , , . . :
) , ;
) , ;
) N , , , .
3. Aeroflot: , , , , . . :
) ;
) ;
) , .
4. Book: , , , , . . :
) ;
) , ;
) , .
5. Worker: , , , . . :
) , ;
) , ;
) , .
6. Train: , , , , , . . :
) , ;
) , ;
) , .
7. Product: , , , , . . :
) ;
) , ;
) , .
8. Patient: , , , , , . . :
) , ;
) , .
9. Bus: , , , , , . . :
) ;
) , 10 ;
) , 10 000 .
10. Customer: , , , , , , . . :
) ;
) , .
11. File: , , , . . :
) , ;
) , ;
) , .
12. Word: , , ( 1 10), . . :
) , N ;
) ;
) , .
13. House: , , , . . :
) , ;
) , , ;
|
|
) , , .
14. Phone: , , , , , , . . :
) , ;
) , ;
) , .
15. Person: , , , , , , . . :
) , ;
) ;
) .
1. ?
*:
*1) ; 2) ; *3) ; 4) .
2. :
*1) ; 2) - ; 3) - .
3. (-) :
1) ; 2) ; *3) .
4. arg ?
:
*1) this > arg; *2) arg; 3) A > arg; 4) A > this > arg.
5.
class A {public: int a; }; A *obj;
?
:
1) obj.a; 2) (*obj) > a; *3) obj > a; 5) obj:: a.
6.
class A {public: int a, b, c;}; A *obj;
b?
:
1) (*obj) > b; 2) A:: b; *3) (*obj).b; 4) obj > a.b.
* .
7. :
class A {
public:
int inc (int x) {return x++;}
int inc (short x) (return x+2;}
};
A obj; int y=5;
cout << obj.inc(y);?
:
*1) 5; 2) 6; 3) 7; 4) .
8. :
class A {
public:
int y;
int inc (int x) {return y++;}
int inc (short x) {return x+y;}
};
A obj; int y=5; obj.y= 6;
cout << obj.inc(y);?
:
1) 5; *2) 6; 3) 11; 4) 7; 5) .
9. ?
:
1) private; *2) public; 3) protected; 4) .
2 .
. , . - , . . , .
, . , , ~. . , .
-. , :
+, -, *, /, %, ^, &, |, ~,!, =, <, >,+=, [], ->, (), new, delete.
- (friend) . -- :
|
|
_ _::operator#(_)
{/* */}
operator#(a,b) a#b. # . >> << -. , . -- , -, . - this. this , , .
, .
. Polynom. .
#include <iostream.h>
#include <conio.h>
#include <math.h>
class Polynom {
int n;
double *koef;
public:
Polynom(); //
Polynom(int k);
Polynom(int k,double *mas);
Polynom(const Polynom&ob); //
~Polynom(){delete[]koef;}
void GiveMemory(int k);
void SetPolynom(int k,double *mas);
void SetDegree(int k){n=k;}; //
void CalculateValue(double x); //
int GetDegree(){return n;}; //
double GetOneCoefficient(int i){return(koef[i]);};
Polynom operator+(Polynom ob); //
Polynom operator*(Polynom ob);
double& operator[](int i){return(koef[i]);}// []
Polynom& operator = (const Polynom p) {
if(&p==this) return *this;
if(koef) delete [] koef;
n=p.n;
koef=new double [p.n+1];
for(int i=0;i<=p.n;i++)
koef[i]=p.koef[i];
return *this;
}
friend ostream& operator<<(ostream& mystream,Polynom &ob);
friend istream& operator>>(istream& mystream,Polynom &ob);
int min(int n,int m)
{return (n<m)? n:m; }
int max(int n,int m)
{return (n>m)? n:m; }
};
//*********** Polynom() **********************************
Polynom::Polynom()
{ randomize();
n=random(5);
koef=new double[n+1];
if(!koef){cout<<"Error";getch();return;}
for(int i=n;i>=0;i--)
koef[i]=random(10)-5;
}
//************* Polynom(int k) *******************************
Polynom::Polynom(int k)
{ n=k;
koef=new double[n+1];
if(!koef){cout<<"Error";getch();return;}
for(int i=n;i>=0;i--)
koef[i]=random(10)-5;
}
//****************** Polynom(int k,double mas[]) ******************
Polynom::Polynom(int k,double mas[])
{n=k;
koef=new double[n+1];
if(!koef){cout<<"Error";getch();return;}
for(int i=n;i>=0;i--)
koef[i]=mas[i];
}
//*************** Polynom(const Polynom&ob) *********************
Polynom::Polynom(const Polynom&ob)
{n=ob.n;
koef=new double[n+1];
if(!koef){cout<<"Error";getch();return;}
for(int i=0;i<=n;i++)
koef[i]=ob.koef[i];
}
//**************** void GiveMemory(int k) **********************
void Polynom::GiveMemory(int k)
{
if(koef) delete [] koef;
koef=new double[k+1];
if(!koef){cout<<"Error";getch();return;}
}
//******************** SetPolynom **************************
void Polynom::SetPolynom(int k,double *mas)
{ n=k;
if(koef) delete [] koef;
koef = new double [n+1];
for(int i=n;i>=0;i--)
koef[i]=mas[i];
}
//*************** CalculateValue *****************************
void Polynom::CalculateValue(double x=1.0)
{ double s;
int i;
for(s=koef[0],i=1;i<=n;i++)
s=s+koef[i]*pow(x,i);
cout<<"f("<<x<<")="; cout<<s<<endl;
}
//**************** Polynom operator+(Polynom ob) ***************
Polynom Polynom::operator+(Polynom ob)
{ int i;
Polynom rab;
rab.GiveMemory(max(n,ob.GetDegree()));
for(i=0;i<=min(n,ob.GetDegree());i++)
rab.koef[i]=koef[i]+ob.GetOneCoefficient(i);
if(n<ob.GetDegree())
{
for(i=min(n,ob.GetDegree())+1;i<=ob.GetDegree();i++)
rab.koef[i]=ob.GetOneCoefficient(i);
rab.n=ob.GetDegree();
}
else
{
for(i=min(n,ob.GetDegree())+1;i<=n;i++) rab.koef[i]=koef[i];
rab.n=n;
}
return rab;
}
//*************** Polynom operator*(Polynom ob) ***************
Polynom Polynom::operator*(Polynom ob)
{
int i,j,k;
double s;
Polynom rab;
rab.GiveMemory(n+ob.GetDegree());
for(i=0;i<=n+ob.GetDegree();i++)
{ s=0;
for(j=0;j<=n;j++)
for(k=0;k<=ob.GetDegree();k++)
|
|
if(j+k==i)s=s+koef[j]*ob.GetOneCoefficient(k);
rab.koef[i]=s;
}
rab.n=n+ob.GetDegree();
return rab;
}
//********** ostream& operator<<(ostream& mystream,Polynom &ob) ******
ostream& operator<<(ostream& mystream,Polynom &ob)
{ char c=' '; // +
for(int i=ob.n;i>=0;i--)
{ double ai=ob.koef[i];
if(ai==0) continue;
else {if(ai>0) mystream<<c; mystream<<ai;}
if(i==0) continue; else mystream<<"x";
if(i==1) continue; else mystream<<"^"<<i;
if(ai!=0)c='+';
}
if(c==' ')mystream<<0;
mystream<<endl;
return mystream;
}
//********* istream& operator>>(istream& mystream,Polynom &ob) *
istream& operator>>(istream& mystream,Polynom &ob)
{
int i;
cout<<"Enter Degree:"; mystream>>ob.n; cout<<endl;
for(i=ob.n;i>=0;i--)
{
cout<<"Enter koeff "<<i<<":"; mystream>>ob.koef[i];
}
return mystream;
}
//******************** MAIN ****************************
int main(int argc, char* argv[])
{ const int m=3;
Polynom f,g,masp[m],*p1,s;
int n=5,i;
double K[6]={1.0,3.2,0.0,4.1,0.0,1.1};
p1=new Polynom(n,K);
cout<<*p1;
p1->CalculateValue(2.0);
cin>>f;
cout<<" f(x)= "; cout<<f;
cout<<" g(x)= "; cout<<g;
s=f+g;
cout<<"f(x)+g(x) = "; cout<<s;
s=f*g;
cout<<" f(x)*g(x) = "; cout<<s;
s=masp[0]; cout<<masp[0];
for(i=1;i<m;i++)
{ s=s+masp[i]; cout<<masp[i];}
cout<<"Summa: "; cout<< s;
while(!kbhit());
delete p1;
return 0;
}
. : ) - ; ) - .
1. Complex. , , , , . . , .
2. Fraction . . , , . , , , , . , .
3. Vector . , . , , , , . , , , , , , . . , , .
4. Matrix. , . , , ; . , , . Matrix , - . .
5. Polynom . , . ; , . , , , , , , . . , -, .
6. Stack. . , . , . , . Stack. , (), , . . .
7. : , , . , , . , . , .
8. String n. , . , , . , , , , . , .
9. ( , , . .) Set n. , . , , , . , , (), , . , , , , . . , .
10. . , . , , , . , , , . , - .
11. , n´m . . , . , , ; , , . , , . , - .
12. BoolVector . , . , , . . . . , .
13. Tvector n. . tk =(t 1 k,,t nk) tl =(t 1 l,, tnl) , i, tik, til Î{0,1} tik ¹ til. : 1Ç1=1Ç X = X Ç1=1, 0Ç0=0Ç X = X Ç0=0, X Ç X = X. , , , , X. . . , .
14. BoolMatrix n´m. , . (), . . , , . BoolMatrix. , .
1. ?
class X {
int a;
int f() const; //1
int g() {return a++;} //2
int h() const {return a++;} //3
};
:
1) ; (*) 2) //3; 3) //2; 4) //1.
2. :
[1] - , this;
[2] - , ;
[3] : ;
[4] const.
:
1) ; (*) 2) [4]; (*) 3) [3]; 4) [2]; (*) 5) [1].
3 ?
class X {
static void f();
void f() const;
};
:
*1) ; 2) ; 3) , const.
4. ?
:
1) new; *2).; 3) *; 4) []; 5) ().
5. ?
#include < iostream.h >
class X {
int a;
public:
X(): a(1) {}
X& operator++() {a++; return *this;};
X& operator++(int) {a--; return *this;};
friend ostream& operator<<(ostream&, X&);
};
ostream& operator<<(ostream& _stream, X& _x) {
_stream << _x.a;
return _stream;
}
void main() {
X x;
cout << ++x;
cout << x++;
}
:
1) ; *2) 21; 3) 12; 4) 22; 5) 11.
6. , - :
[1] ;
[2] ;
[3] ( this).
?
:
1) [3]; 2) ; 3) [2]; *4) [1].
7. inline? .
[1] ;
[2] , ;
[3] ;
[4] ;
[5] .
:
1) [5]; *2) [4]; *3) [3]; *4) [2]; 5) [1].
8. :
class X {
friend void f(X&);
};
f?
:
1) f , . . private; 2) X x; X::f(x); 3) X x; f(&x); 4) X x; x.f(x); *5) X x; f(x).