add, subtr,mult , d=mult(add(a,b,n),a,n);
//arb2014_array_3_function
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int** create(ifstream &f, int n)
{
int i,j;
int **x=new int*[n];
for(i=0;i<n;i++)
x[i]=new int[n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
f>>x[i][j];
return x;
}
void show(ofstream &f, int** x, int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
f<<setw(5)<<x[i][j];
f<<endl;
}
f<<endl;
}
////////////////////////////////
int** add(int** x, int** y, int n)
{
int i,j;
int **z=new int*[n];
for(i=0;i<n;i++)
z[i]=new int[n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
z[i][j]=x[i][j]+y[i][j];
return z;
}
//////////////////////////////////
int** subtr (int** x, int**y, int n)
{
int i,j;
int **z=new int*[n];
for(i=0;i<n;i++)
z[i]=new int[n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
z[i][j]=x[i][j]-y[i][j];
return z;
}
/////////////////////////////
int** mult(int** x, int**y, int n)
{
int i,j,k;
int **z=new int*[n];
for(i=0;i<n;i++)
z[i]=new int[n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
z[i][j]=0;
for(k=0;k<n;k++)
z[i][j]=z[i][j]+x[i][k]*y[k][j];
}
return z;
}
///////////////////////////////
void main()
{
ifstream in1("arb1.txt");
ifstream in2("arb2.txt");
ofstream out("out.txt");
int** a,**b, **c;
int n,i;
cout<<"enter size of array n=";
cin>>n;
a= create(in1,n);
b= create(in2,n);
in1.close();
in2.close();
out<<"array a:"<<endl;
show(out,a,n);
out<<"array b:"<<endl;
show(out,b,n);
c=add(a,b,n);
out<<"array c=a+b c:"<<endl;
show(out,c,n);
c=add(c,c,n);
out<<"array c=c+c c:"<<endl;
show(out,c,n);
c=subtr(add(c,c,n), add(a,c,n),n);
out<<"array c=(c+c)-(a+c) c:"<<endl;
show(out,c,n);
int** d =mult(a,b,n);
out<<"array d=a*b d:"<<endl;
show(out,d,n);
d=mult(add(a,b,n),a,n);
out<<"array d=(a+b)*a d:"<<endl;
show(out,d,n);
out.close();
}
arb1.txt
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
arb2.txt
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
out.txt
array a:
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
array b:
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
array c=a+b c:
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
array c=c+c c:
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4
array c=(c+c)-(a+c) c:
3 3 3 3
3 3 3 3
3 3 3 3
3 3 3 3
array d=a*b d:
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4
|
|
array d=(a+b)*a d:
8 8 8 8
8 8 8 8
8 8 8 8
8 8 8 8
.
Employee -
transport
(struct)
, . ++ , , .
struct _
{
_1 _1;
_2 _2;
.
_n _n;
};
, , .
, . .
. () -> .
struct Worker
{
char fio[30];
int code;
double salary;
}
Worker worker, staff[100],*ps;
worker.fio=;
staff[8].code=123;
ps->salary=12000;
,
struct A { int a; double k;};
struct B { A a; double x;};
B x[2];
x[0].a.a=1;
x[1].x=0.1
//arb2014_struct_employe_2
#include<iostream>
#include<iomanip>
using namespace std;
const int K=80;
const int N=5;
enum status { Boss=1,Accountant,Secretary,It,Office};
struct Employee
{
int Id;
char Name[K];
double Salary;
status Status;
};
void main()
{
int i;
Employee emp[N];
for(i=0;i<N;i++)
{
emp[i].Id=i+1;
cout<<"enter name "<<emp[i].Id<<" of employee: ";
cin.getline(emp[i].Name,80);
cout<<" enter salary "<< emp[i].Name<<": ";
cin>>emp[i].Salary;
int st;
do
{
cout<<"enter status of employee (1..5) st= ";
cin>>st;
} while (st<1 || st>5);
emp[i].Status =(status)st;
cin.get();//
}
cout<<setw(10)<<"Id"<<setw(10)<<"Name"<<setw(10)<<"Salary"<<setw(15)<<"Status"<<endl;
for(i=0;i<N;i++)
{
cout<<setw(10)<<emp[i].Id<<setw(10)<<emp[i].Name
<<setw(10)<<emp[i].Salary<<setw(15);
switch (emp[i].Status)
{
case Boss: cout<<"Boss"<<endl; break;
case Accountant: cout<<"Accountant"<<endl; break;
case Secretary: cout<<"Secretary"<<endl; break;
case It: cout<<"It"<<endl; break;
case Office: cout<<"Office"<<endl; break;
default:; break;
}
}
}
//arb2015_enum_day_of_week
#include<iostream>
using namespace std;
|
|
enum days_of_week { Sun, Mon, Tue, Wed, Thu,Fri,Sat };
void main()
{
days_of_week day1,day2;
day1=Mon;
day2=Thu;
int diff=day2-day1;
cout<<" the difference in days = "<<diff<<endl;
if (day1<day2) cout<<" day1 comes before day2"<<endl;
}
++ . .
, (, ).
enum,
enum _ { _ } _;
_ , . _ , , _ .
transport transport t1 t2.
enum transport {car, truck, airplane, train, boat };
transport t1, t2;
t1 = airplane;
, , ( ) .
cout<< car<< <<train;
0 3.
, , . , .
t2=1;//
, transport .
T2= (trunsport) 1;
t2 truck, transport- 1.
, . : . ( ) , .
enum transport {car, truck, airplane=10, train, boat };
transport
ar 0
truck 1
airplane 10
train 11
boat 12
//arb2014_type_enum
#include<fstream>
using namespace std;
void main()
{
ofstream out("out.txt");
enum transport {car, truck=2,airplane=10, train, boat };
transport t1,t2;
out<<"airplane=10 airplane= "<<airplane<<endl;
out<<"airplane=10 airplane+10 <<airplane+10<<endl;
t2=truck;
t2=(transport)((int)t2 +2);
out<<" t2=(transport)((int)t2 +2) t2= "<<t2<<endl;
out.close();
}
enum transport {car, truck=2,airplane=10, train, boat };
out.txt
airplane=10 airplane= 10
airplane=10 airplane+10 = 20
t2=(transport)((int)t2 +2) t2= 4
: 4 .
union
- . union. .
union utype
{
short int I;
char ch;
};
, short int char . : , , , i ch ( ) .
|
|
//arb2014_struct_union2
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void main()
{
struct strtype
{
int x;
union
{
short int age;
char title[30];
};
};
int n;
cout<<"enter size of array n=";
cin>>n;
strtype* a= new strtype[n];
ifstream in("arb.txt");
ofstream out("out.txt");
int i;
for(i=0;i<n;i++)
{
int y;
in>>y;
if(y==0) in>>a[i].age; else in>>a[i].title;
a[i].x=y;
}
in.close();
for(i=0;i<n;i++)
{
out<<a[i].x<<setw(20);
if(a[i].x) out<<a[i].title; else out<<a[i].age;
out<<endl;
}
out.close();
}
n=20
arb.txt
0 23 1 boss 0 17 1 it 1 office 1 landlord 0 20 1 sportsmen
0 23 1 boss 0 17 1 it 1 office 1 landlord 0 20 1 sportsmen
0 23 1 boss 0 17 1 it 1 office 1 landlord 0 20 1 sportsmen
0 23 1 boss 0 17 1 it 1 office 1 landlord 0 20 1 sportsmen
out.txt
0 23
1 boss
0 17
1 it
1 office
1 landlord
0 20
1 sportsmen
0 23
1 boss
0 17
1 it
1 office
1 landlord
0 20
1 sportsmen
0 23
1 boss
0 17
13
- ,
1.