Структура комплексное число
//arb2014_struct_complex
#include <iostream>
using namespace std;
struct complex
{
double re;
double im;
} ;
/////////////
complex create_complex()
{
complex p;
cout<<"enter re=";
cin>>p.re;
cout<<"enter im=";
cin>>p.im;
return p;
}
//////////////////
void show(complex p)
{
cout<<p.re<<" +i* "<<p.im<<endl;
}
///////////////////////////////////
complex add(complex p, complex q)
{
complex temp;
temp.re=p.re+q.re;
temp.im=p.im+q.im;
return temp;
}
//////////////////////////
complex subtr(complex p, complex q)
{
complex temp;
temp.re=p.re-q.re;
temp.im=p.im-q.im;
return temp;
}
//////////////////////////
complex mult (complex p, complex q)
{
complex temp;
temp.re=p.re*q.re-q.im*p.im;
temp.im=p.re*q.im+q.re*p.im;
return temp;
}
//////////////////////////////
complex div(complex p,complex q)
{
double r=q.re*q.re + q.im*q.im;
complex temp;
temp.re=(p.re*q.re+p.im*q.im)/r;
temp.im=(p.im*q.re-p.re*q.im)/r;
return temp;
}
////////////////////
void main()
{
complex p,q;
cout<<" enter complex p:"<<endl;
p=create_complex();
cout<<" enter complex q:"<<endl;
q= create_complex();
cout<<" complex number p: ";
show(p);
cout<<" complex number q: ";
show(q);
complex u=add(p,q);
cout<<" complex number u=p+q u: ";
show(u);
u=p;
cout<<" complex number u=p u: ";
show(u);
u=subtr(p,q);
cout<<" complex number u=p-q u: ";
show(u);
u=add(add(p,q),subtr(p,q));
cout<<" complex number u=(p+q)+(p-q) u: ";
show(u);
u=subtr( add(add(p,q),subtr(p,q)),p);
cout<<" complex number u=((p+q)+(p-q))-p u: ";
show(u);
u=mult(p,p);
cout<<" complex number u=p*p u: ";
show(u);
u=div(p,p);
cout<<" complex number u=p/p u: ";
show(u);
u=div(add(p,p),p);
cout<<" complex number u=(p+p)/p u: ";
show(u);
}
Лекция
Задача 2
Поиск треугольника с наибольшей площадью.
//arb2013_struct_triangle_max_square_variant2
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
#include<cstring>
using namespace std;
struct dot
{
double koor_x,koor_y;
};
struct triangle
{
dot a;
dot b;
dot c;
};
const int num_dot=30;
const int num_tr=10;
//////////////////////////////////////////////////
void create(ifstream &f, dot &x)
{
if (!f)
{
cout<<"File not found! ";
exit(-1);
}
f>>x.koor_x>>x.koor_y;
}
/////////////////////////////////////////////////
void create(triangle &Tr, dot dx, dot dy, dot dz)
{
Tr.a=dx;
Tr.b=dy;
Tr.c=dz;
}
///////////////////////////////////////////////
double len_side(dot dx,dot dy)
{
return sqrt( ( dx.koor_x - dy.koor_x )*( dx.koor_x - dy.koor_x ) +
( dx.koor_y - dy.koor_y)*( dx.koor_y - dy.koor_y ));
}
////////////////////////////////////////////////
bool usl_triangle(dot dx, dot dy, dot dz)
{
return
(dx.koor_x - dz.koor_x)*(dy.koor_y - dz.koor_y)!=
(dy.koor_x - dz.koor_x)*(dx.koor_y - dz.koor_y);
}
/////////////////////////////////
double square(triangle Tr)
{
double s[3];
s[0]=len_side(Tr.a,Tr.b);
s[1]=len_side(Tr.a,Tr.c);
s[2]=len_side(Tr.b,Tr.c);
double Semi_Per;
Semi_Per=(s[0]+s[1]+s[2])/2;
return sqrt(Semi_Per*(Semi_Per-s[0]) *(Semi_Per-s[1])
*(Semi_Per-s[2]));
}
/////////////////////////////////
void show(ofstream &f, triangle Tr)
{
f<<endl<<"Triangle= "<<endl;
double s[3];
s[0]=len_side(Tr.a,Tr.b);
s[1]=len_side(Tr.a,Tr.c);
s[2]=len_side(Tr.b,Tr.c);
cout.precision(4);
f<<"dot1= " << "(" <<setw(6)<<Tr.a.koor_x<<","<<setw(6)
<<Tr.a.koor_y<<")"<<endl;
f<<"dot2= " <<"(" <<setw(6)<<Tr.b.koor_x<<","<<setw(6)
<<Tr.b.koor_y<<")"<<endl;
f<<"dot3= " <<"("<<setw(6)<<Tr.c.koor_x<<","<<setw(6)
<<Tr.c.koor_y<<")"<<endl;
f<<"side AB= "<<setw(6)<<s[0]<<endl;
f<<"side AC= "<<setw(6)<<s[1]<<endl;
f<<"side BC= "<<setw(6)<<s[2]<<endl;
f<<"square tr ABC= "<<setw(6)<<square(Tr)<<endl;
}
//////////////////////////////////////////////////////////////////////
void main()
{
dot dt1,dt2,dt3;
triangle Tr[num_tr];
int i,num_tr_true;
ifstream in("arb.txt");
ofstream out("out.txt");
num_tr_true=-1;
for (i=0;i<num_dot;i+=3)
{
create(in,dt1);
create(in,dt2);
create(in,dt3);
if (usl_triangle(dt1,dt2,dt3))
{
num_tr_true++;
create(Tr[num_tr_true],dt1,dt2,dt3);
}
}
double max=0.0;
int ind=0;
for(i=0;i<num_tr_true;i++)
if (max<square(Tr[i]))
{
max=square(Tr[i]);
ind=i;
}
out<<"Max Square= "<<endl;
show(out,Tr[ind]);
out<<endl<<endl;
out<<"show all triangles:"<<endl;
for(i=0;i<num_tr_true;i++)
show(out,Tr[i]);
out<<endl;
out<<"num_tr_true= "<<num_tr_true+1<<endl;
out.close();
}
Файл arb.txt
1.0 1.0
22.0 27.0
32.0 39.0
1.11111 1.1
3.333 3.455
6.66 4.056
4.11111 5.1
7.333 8.455
6.66 4.056
11.11111 12.1
17.333 13.455
22.66 24.056
13.11111 14.1
34.333 36.455
61.66 64.056
41.11111 42.1
43.333 38.455
26.66 24.056
0.11111 1.1
0.333 3.455
0.66 4.056
12.91111 11.1
32.9333 3.455
6.966 4.9056
1.911111 5.1
3.9333 3.455
6.66 4.056
17.11111 18.1
37.333 38.455
61.66 64.0756
2.11111 4.1
3.333 9.455
16.66 14.056
10.11111 11.1
13.333 13.455
16.66 14.056
1.0 1.0
2.0 2.0
3.0 3.0
Файл out.txt
Max Square=
Triangle=
dot1= (12.9111, 11.1)
dot2= (32.9333, 3.455)
dot3= ( 6.966,4.9056)
side AB= 21.4321
side AC= 8.58574
side BC= 26.0078
square tr ABC= 84.7379
show all triangles:
Triangle=
dot1= ( 1, 1)
dot2= ( 22, 27)
dot3= ( 32, 39)
side AB= 33.4215
side AC= 49.0408
side BC= 15.6205
square tr ABC= 4
Triangle=
dot1= (1.11111, 1.1)
dot2= ( 3.333, 3.455)
dot3= ( 6.66, 4.056)
side AB= 3.23772
side AC= 6.28714
side BC= 3.38085
square tr ABC= 3.24986
Triangle=
dot1= (4.11111, 5.1)
dot2= ( 7.333, 8.455)
dot3= ( 6.66, 4.056)
side AB= 4.65152
side AC= 2.75441
side BC= 4.45018
square tr ABC= 5.95759
Triangle=
dot1= (11.1111, 12.1)
dot2= (17.333,13.455)
dot3= ( 22.66,24.056)
side AB= 6.36773
side AC= 16.623
side BC= 11.8642
square tr ABC= 29.3701
Triangle=
dot1= (13.1111, 14.1)
dot2= (34.333,36.455)
dot3= ( 61.66,64.056)
side AB= 30.8239
side AC= 69.6606
side BC= 38.8404
square tr ABC= 12.5748
Triangle=
dot1= (41.1111, 42.1)
dot2= (43.333,38.455)
dot3= ( 26.66,24.056)
side AB= 4.26882
side AC= 23.1175
side BC= 22.03
square tr ABC= 46.383
Triangle=
dot1= (0.11111, 1.1)
dot2= ( 0.333, 3.455)
dot3= ( 0.66, 4.056)
side AB= 2.36543
side AC= 3.00653
side BC= 0.6842
square tr ABC= 0.318365
Triangle=
dot1= (12.9111, 11.1)
dot2= (32.9333, 3.455)
dot3= ( 6.966,4.9056)
side AB= 21.4321
side AC= 8.58574
side BC= 26.0078
square tr ABC= 84.7379
Triangle=
dot1= (1.91111, 5.1)
dot2= (3.9333, 3.455)
dot3= ( 6.66, 4.056)
side AB= 2.60677
side AC= 4.86229
side BC= 2.79215
square tr ABC= 2.85038
num_tr_true= 10