1
.
:
- ;
- ( , main-);
- - ;
- - (<<, >>), (<,==) ;
- ;
- , , .
12 . (3 ).
.
1.1
1.1. ( 1). 1 .
1.2. (4 1). char*.
1.3. , . ( 3).
1.4. , :
- ;
- ;
- ;
- ;
- .
1.5. ( ) . , , .
1.6. .
1.2 ( 1)
2.1. 2.
2.2. >> ifstream .
2.3. << ofstream .
2.4. , .
2.5. () ID n, n - , . ( 2.).
1.3 ( 1)
3.1. < == , .
3.2. , , .
3.4. .
- , ( ). Planeta . .
|
|
- () Planeta . Planeta . .
- ( ).
1. 1.1, 1.2 1.3 .
2. :
, , .
, , . . : () .
, . .
, , .
- () , , . , , .
: , . , , , .
, : ( ) , . .
. , .
, .
- ( ) , , , . , . , , , .
? , , , . , . .
|
|
- , ? .
- . . , : (, , ) ? , , , . , , .
. , , , , . , . . ( ), - , .
- , , . / , - - .
. , , :
□ , . : , , () .
□ ) , - -. . , . , .
, , , , . , ( ) , .
. , , . , . , , , .
, , . . , . , .
|
|
- .
, . : public ( ), private ( ) protected ( ).
, , . , , -. , . , , , ( ).
. , . , . , . , .
, - , ( ) . : , , . , , . .
, : .
. . . , .
. , . C++ .
, - , - (, ).
C++ . C++ , . struct C++ , . C++ - .
1.
|
|
/* . 100 .
.
. :
( 20 ),
(4 ),
( 8 ).
, ,
, .
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include "d:\\C++\\MyLib\\ConsolCyr.h"
using namespace std;
const int l_name = 20;
struct Man {
char name[l_name];
int birth_year;
float pay;
};
int read_dbase(char* filename,Man* arr,int& n);
int menu();
int menu_f();
void print_dbase(Man* arr,int n);
int write_dbase(char* filename, Man* arr, int n);
/*
int add(Man arr, int n);
int edit(Man* arr, int n);
int remove(Man* arr, int n);
*/
int find(Man* arr,int n, char* name);
int find(Man* arr,int n, int birth_year);
int find(Man* arr,int n, float pay);
void find_man(Man* arr,int n);
//----------------------------------------
int main()
{
const int N=100;
Man arr[N];
char *filename="dbase.txt";
int n;
//
if (read_dbase(filename,arr,n)){
cout<<" "<<endl;
return 1;
}
print_dbase(arr,n);
while (true) {
switch (menu())
{
//case 1: add(arr,n); break;
//case 2: remove(arr,n); break;
case 3: find_man(arr,n); break;
//case 4: edit(arr,n); break;
case 5: print_dbase(arr,n); break;
case 6: write_dbase(filename,arr,n); break;
case 7: return 0;
default: cout<<" "<<endl; break;
}
}
return 0;
}
////////////////////////////////////////////////////////
int menu()
{
cout<<" ============== ========================\n";
cout<<"l - \t 4 - "<<endl;
cout<<"2 - copya\t\t 5 - "<<endl;
cout<<"3 - \t\t 6 - "<<endl;
cout<<"\t\t\t 7 - "<<endl;
cout<<" 1 7"<< endl;
int resp;
cin>>resp;
cin.clear();
cin.ignore(10,'\n');
return resp;
}
// ------------------------------------
int read_dbase(char* filename, Man* arr, int& n)
{
ifstream fin(filename,ios::in);
if (!fin)
{
cout<<"He "<< filename <<endl;
return 1;
}
fin>>n;
if(n>100)
{
cout<<" . n= "<< n <<endl;
return 1;
}
for(int i=0;i<n;i++)
fin>>arr[i].name>>arr[i].birth_year>>arr[i].pay;
fin.close();
return 0;
}
//------------------------------------
int write_dbase(char *filename, Man* arr,int n)
{
ofstream fout(filename,ios::out);
if (!fout)
{
cout<<" "<<endl;
return 1;
}
fout<<n;
for(int i=0; i<n; i++)
fout<< arr[i].name<<' '<< arr[i].birth_year<<' '<< arr[i].pay<<endl;
fout.close();
return 0;
}
//------------------------------
void print_dbase(Man* arr,int n)
{
cout<<" "<<endl;
for(int i=0;i<n;i++)
cout<<setw(3)<<i+1<<". "<<arr[i].name<<setw(20-strlen(arr[i].name)+6)
<<arr[i].birth_year<<setw(10)<<arr[i].pay<<endl;
}
//-----------------------------
int find(Man* arr, int n, char* name) //
// ,
{
int ind=-1;
for(int i=0;i<n;i++)
if (!strcmp(arr[i].name, name))
{
cout<<arr[i].name<<setw(20-strlen(arr[i].name)+6)
<<arr[i].birth_year<<setw(10)<<arr[i].pay<<endl;
ind=i;
}
return ind;
}
//-------
int find(Man* arr, int n, int birth_year)
{
int ind=-1;
for(int i=0;i<n;i++)
if (arr[i].birth_year < birth_year)
{
ind=i;
cout<<arr[i].name<<setw(20-strlen(arr[i].name)+6)
<<arr[i].birth_year<<setw(10)<<arr[i].pay<<endl;
|
|
}
return ind;
}
//-------- , "pay"
int find(Man* arr, int n, float pay)
{
int ind=-1;
for(int i=0;i<n;i++)
if (arr[i].pay > pay)
{
ind=i;
cout<<arr[i].name<<setw(20-strlen(arr[i].name)+6)
<<arr[i].birth_year<<setw(10)<<arr[i].pay<<endl;
}
return ind;
}
//--------------------------------------
int menu_f()
{
cout<<"\n----------------- -----------------\n";
cout<<"1 - 2 - \n"
<<"3 - 4 - \n ";
cout<<" 1 4\n";
int resp;
cin>> resp;
cin.clear();
cin.ignore(10,'\n');
return resp;
}
//-------------------------------------
void find_man(Man* arr, int n)
{
char buf[l_name];
int birth_year;
float pay;
while(true)
{
switch (menu_f())
{
case 1: cout<<" \n";
cin>> buf;
if(find(arr, n, buf)<0)
cout<<" "<< buf<<" \n";
break;
case 2: cout<<" "<< endl;
cin>> birth_year;
if(find(arr, n, birth_year)<0)
cout<< " , "
<<birth_year<<" \n";
break;
case 3: cout<<" "<< endl;
cin>> pay;
if(find(arr, n, pay)<0)
cout<< " , "
<< pay<<" .\n";
break;
case 4: return;
default:
cout<<" \n";
}
}
}
, , Man:
const int l_name=30;
struct Man {
char name[l_name];
int birth_year;
float pay;
};
, , , , :
class Man {
char name[l_name];
int birth_year;
float pay; };
. ! (private). main() Man man, - , : man.pay = value, , . . , (public).
. name l_name. . , Man . , , 1_name = 20, -, , , , , l_name = 200. . char name[l_name] char* pName. : ? : , .
, , ( Man). , , . , .
, , . ~().
, , . , , { }, , . new, :
Man* pMan = new Man;
delete, : delete pMan. , :
class Man { public:
Man(int I_name = 20) { pName = new char[l_name]: } //
~Man() { delete [] pName: } //
private:
char* pName;
int birth_year:
float pay: };
(;). , , .
: l_name (20). , . ? ? ! , .
-, , , : Man man[25]. 25 Man, ! , . , , .
. (inline) . , , :
// Man.h ( )
class Man {
public:
Man(int I_name = 30); //
~Man(); //
private:
char* pName;
int birth_year;
float pay;
};
// Man.cpp ( )
#include "Man.h"
Man::Man(int l_name) { pName = new char[l_name]; }
Man::~Man() { delete [] pName; }
, ::. : , . , , .
. ? ? : Man?
: . , , . , .
, . GetName(), GetBirthYear(), GetPay(). , , .
SetName(), SetBirthYear(), SetPay(). , , .
. , , , const . - , . const . , , - , , const. const , , .
. Constructor is working, Destructor is working . , .
-----------------------------------------------------------------------------------------------------
! , , . .
, ::, ?:, ., .*, C++, . , - ( ). :
_ operator # ( ) { }
# .
- , ( ) . - , , .
, Point.
:
class Point {
double x, ;
public:
//...
Point operator +(Point&);
};
Point Point::operator +(Point& p)
{ return Point(x + p.x, + .); }
, , , , :
class Point {
double x, : public: //...
friend Point operator +(Point&, Point&);
};
Point operator +(Point& p1, Point& p2) {
return Point(p1.x + p2.x. p1. + p2.y);
}
+ :
Point p1(0, 2), 2(-1, 5);
Point = p1 + 2;
, , p1 + 2, p1.operator +(p2), operator +(pl, 2).
3 = -1, = 7. , 3 , , -. (, , ?)
, , ? : , - , . , ( ) ( , int), . (, this , , , ).
2.
<< element.
//overload.cpp -
// <<
#include <iostream>
#include <fstream>
using namespace std;
struct element { //
int nk,nl;
float zn;
};
// -, <<
ofstream& operator<<(ofstream& out,element el){
out << ' ' << el.nk << ' ' << el.nl <<' ' << el.zn << '\n';
return out; }
int main()
{ const int numbeEl = 5; //
element arel[numbeEl] = { 1, 2, 3.45, 2, 3, 4.56,
22, 11, 45.6, 3, 24, 4.33, 3, 6, -5.3 };
// abc:
ofstream filel("abc.txt",ios::app);
if (!filel)
{ cout << " abc.\n";
return 1; }
// abc :
for (int i = 0; i < numbeEl; i++)
filel << arel[i];
return 0;
}
- element. abc:
1 2 3.45
2 3 4.56
22 11 45.6
3 24 4.33
3 6 5.3
1
Mercury 4878 0 0Venus 12104 0 0Earth 12774 1 1Mars 6786 1 2Jupiter 142796 0 16Saturn 120000 0 17Uranus 51108 0 5Neptune 49600 0 2Pluto 2280 0 1
2.
class gamma
{
private:
static int total; //
//( )
int id; //ID
public:
gamma() //
{
total++; //
id = total; //id total
cout << " ID " << id << endl;
}
~gamma() //
{
total--;
cout << " ID " << id << endl;
}
static void showtotal() //
{
cout << "\nBcero: " << total << endl;
}
void showID() //
{
cout<< "\nID: " << id << endl;
}
};
//---------------------------------
int gamma:: total=0;
void main()
{ gamma:: showtotal();
gamma g1;
gamma g1.showtotal();
gamma g2,g3;
g3.showtotal();
g1.showID();
g2.showID();
g3.showID();
cout<< <<endl;
}
//--------------------------------------------------------
3
, planet
:
planet.h ,
planet.cpp .
// sunsys.cpp
#include <fstream>
#include "planet.h"
#include "planet.cpp"
//#include "ConsolCyr.h"
using namespace std;
int read_db(char*, Planet*, const int);
int menu();
void print_db(Planet*, int);
int write_db(char*,Planet*, int);
int find(Planet*,int);
void sort_db(Planet*, int);
const int Size = 12;
const int l_record = 80;
int main()
{
char *file_name = "sunsys.txt";
Planet planets[Size];
int n_planet;
int ind;
while (true) {
switch (menu())
{
case 1: n_planet=read_db(file_name,planets,Size);
break;
case 2: write_db(file_name,planets,n_planet); break;
case 3: if((ind=find(planets,n_planet))>=0)
planets[ind].edit();
else
cout<<" "<<endl;
break;
case 4: print_db(planets,n_planet); break;
case 5: sort_db(planets,n_planet); break;
case 6: return 0;
default: cout<<" "<<endl; break;
}
}
return 0;
}