Програмиране на С++
Увод
редактиранеТази УикиКнига е предназначена за хора, които са програмисти или вече имат известен опит свързан с програмиране.
Съдържание
редактиранеКато начало
Основни понятия
редактиранеОбекти и обектно-ориентирано програмиране
редактиранеСтандартна библиотека с шаблони: Standard Template Library, STL
редактиране- include (iostream);
#include "iostream.h" #include "math.h" int index_array=0, temp_index_3=0, temp_index_4=0; float x_1[20], y_1[20]; float x_2[20], y_2[20]; float x_3[20], y_3[20]; float x_4[20], y_4[20]; float distance_3, distance_4, temp_3, temp_4; char exit_loop; void enter_data(){ do{ do{ cout<<"Vavedete tochka ot II kvadrant, x<0, y>0\n"; cout<<"x2["<<index_array<<"]=";cin>>x_2[index_array]; cout<<"y2["<<index_array<<"]=";cin>>y_2[index_array]; }while (x_2[index_array]>=0 || y_2[index_array]<=0); do{ cout<<"Vavedete tochka ot III kvadrant, x<0, y<0\n"; cout<<"x3["<<index_array<<"]=";cin>>x_3[index_array]; cout<<"y3["<<index_array<<"]=";cin>>y_3[index_array]; }while (x_3[index_array]>=0 || y_3[index_array]>=0); do{ cout<<"Vavedete tochka ot IV kvadrant, x>0, y<0\n"; cout<<"x4["<<index_array<<"]=";cin>>x_4[index_array]; cout<<"y4["<<index_array<<"]=";cin>>y_4[index_array]; }while (x_4[index_array]<=0 || y_4[index_array]>=0); do{ cout<<"Vavedete tochka ot I kvadrant, x>0, y>0\n"; cout<<"x1["<<index_array<<"]=";cin>>x_1[index_array]; cout<<"y1["<<index_array<<"]=";cin>>y_1[index_array]; }while (x_1[index_array]<=0 || y_1[index_array]<=0); cout<<"Exit Y/N:"; ++index_array; cin>>exit_loop; }while (exit_loop!='Y' && exit_loop!='y' && index_array<20); } void closest_point(){ temp_3=sqrt(pow(x_3[0],2)+pow(y_3[0],2)); distance_3=temp_3; temp_4=sqrt(pow(x_4[0],2)+pow(y_4[0],2)); distance_4=temp_4; for (int i=1;i<index_array;++i){ distance_3=sqrt(pow(x_3[i],2)+pow(y_3[i],2)); distance_4=sqrt(pow(x_4[i],2)+pow(y_4[i],2)); if (distance_3<temp_3){ temp_3=distance_3; temp_index_3=i; } if (distance_4<temp_4){ temp_3=distance_3; temp_index_4=i; } } cout<<"\nNaj-blizka tochka do koordinatnoto nachalo v III kvadrant e:\n"; cout<<"x3["<<temp_index_3<<"]="<<x_3[temp_index_3]<<", y3["<<temp_index_3<<"]="<<y_3[temp_index_3]<<"\n"; cout<<"Razstoianie do koordinatnoto nachalo = "<<distance_3; cout<<"\n\nNaj-blizka tochka do koordinatnoto nachalo v IV kvadrant e:\n"; cout<<"x4["<<temp_index_4<<"]="<<x_4[temp_index_4]<<", y4["<<temp_index_4<<"]="<<y_4[temp_index_4]<<"\n"; cout<<"Razstoianie do koordinatnoto nachalo = "<<distance_4<<"\n"; } float calculate_contour(){ float l1=0, l2, l3, l4; l2=sqrt(pow(x_2[0]-x_3[0],2)+pow(fabs(y_2[0])+fabs(y_3[0]),2)); l3=sqrt(pow(fabs(x_3[0])+fabs(x_4[0]),2)+pow(y_3[0]-y_4[0],2)); l4=sqrt(pow(x_4[0]-x_1[0],2)+pow(fabs(y_4[0])+fabs(y_1[0]),2)); for (int i=1;i<index_array;++i){ l1+=sqrt(pow(fabs(x_1[i-1])+fabs(x_2[i]),2)+pow(y_1[i-1]-y_2[i],2)); l2+=sqrt(pow(x_2[i]-x_3[i],2)+pow(fabs(y_2[i])+fabs(y_3[i]),2)); l3+=sqrt(pow(fabs(x_3[i])+fabs(x_4[i]),2)+pow(y_3[i]-y_4[i],2)); l4+=sqrt(pow(x_4[i]-x_1[i],2)+pow(fabs(y_4[i])+fabs(y_1[i]),2)); } return l1+l2+l3+l4; } int main (){ enter_data(); cout<<"Daljinata na otworenia kontur e, L="<<calculate_contour()<<"\n"; closest_point(); }