uses
crt;
type
TVector=object
private
x,y:real;
public
procedure Init(x1,x2,y1,y2:real);
procedure SetVector(valueX,valueY:real);
procedure GetVector(var valueX:real;var valueY:real);
function length:real;
end;
TOperation=object
private
vector1,vector2:TVector;
public
procedure SetVectors(valueVector1,valueVector2:TVector);
end;
TOperationAdd=object(TOperation)
procedure Add(var result:TVector);
end;
TOperationSub=object(TOperation)
procedure Sub(var result:TVector);
end;
TOperationMulScalyar=object(TOperation)
function MulScalyar:real;
end;
TOperationMulVector=object(TOperation)
function MulVector:real;
end;
procedure TVector.Init(x1,x2,y1,y2:real);
begin
x:=x2-x1;
y:=y2-y1;
end;
procedure TVector.SetVector(valueX,valueY:real);
begin
x:=valueX;
y:=valueY;
end;
procedure TVector.GetVector(var valueX:real;var valueY:real);
begin
valueX:=x;
valueY:=y;
end;
function TVector.length:real;
begin
length:=sqrt(sqr(x)+sqr(y));
end;
procedure TOperation.SetVectors(valueVector1,valueVector2:TVector);
begin
vector1:=valueVector1;
vector2:=valueVector2;
end;
procedure TOperationAdd.Add(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:real;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX1+vectorX2,vectorY1+vectorY2);
end;
procedure TOperationSub.Sub(var result:TVector);
var
vectorX1,vectorX2,vectorY1,vectorY2:real;
begin
vector1.GetVector(vectorX1,VectorY1);
vector2.GetVector(vectorX2,VectorY2);
result.SetVector(vectorX2-vectorX1,vectorY2-vectorY1);
end;
function TOperationMulScalyar.MulScalyar:real;
var
vectorX1,vectorX2,vectorY1,vectorY2:real;
begin
vector1.GetVector(vectorX1,VectorX2);
vector2.GetVector(vectorX2,VectorY2);
MulScalyar:=vectorX1*vectorX2+vectorY1*vectorY2;
end;
function TOperationMulVector.MulVector:real;
var
scalar, valueCos:real;
MulScalyar :TOperationMulScalyar;
begin
MulScalyar.SetVectors(vector1,vector2);
scalar:=MulScalyar.MulScalyar;
valueCos:=scalar/(vector1.length*vector2.length);
MulVector:=2*scalar*valueCos;
end;
var
x,y,x1,x2,y1,y2 :real;
vector1,vector2,result:TVector;
operationAdd :TOperationAdd;
operationSub :TOperationSub;
operationMulScalyar :TOperationMulScalyar;
operationMulVector :TOperationMulVector;
mulScalyar,mulVector :real;
begin
clrscr;
randomize;
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector1.Init(x1,x2,y1,y2);
vector1.GetVector(x,y);
writeln('A(',x1:1:0,',',y1:1:0,'); B(',x2:1:0,',',y2:1:0,'); AB(',x:1:0,',',y:1:0,')');
x1:=random(10);
x2:=random(10);
y1:=random(10);
y2:=random(10);
vector2.Init(x1,x2,y1,y2);
vector2.GetVector(x,y);
writeln('C(',x1:1:0,',',y1:1:0,'); D(',x2:1:0,',',y2:1:0,'); CD(',x:1:0,',',y:1:0,')');
operationAdd.SetVectors(vector1,vector2);
operationAdd.Add(result);
result.GetVector(x,y);
writeln('Add: (',x:2:0,',',y:2:0,')');
operationSub.SetVectors(vector1,vector2);
operationSub.Sub(result);
result.GetVector(x,y);
writeln('Sub: (',x:2:0,',',y:2:0,')');
operationMulScalyar.SetVectors(vector1,vector2);
MulScalyar:=operationMulScalyar.MulScalyar;
writeln('Mul. Scalyar: ',mulScalyar:5:2);
operationMulVector.SetVectors(vector1,vector2);
MulVector:=operationMulVector.MulVector;
writeln('Mul. Vector: ',mulVector:5:2);
readkey;
end.
Комментарий
Прокоментируйте, пожалуйста, программу с использованием объектов (наследование), останавливаясь лишь на важных аспектах наследования. Программа выполняет действия (сложение, вычитание, скалярное и векторное произведение) с векторами.
Каким значком в блок-схеме обозначается объект?