|
|
Регистрация Восстановить пароль |
Повторная активизация e-mail |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
20.09.2011, 21:20 | #1 |
Форумчанин
Регистрация: 09.04.2011
Сообщений: 134
|
Программа на скалярное произведение векторов
Написал программку, все работает, но препод сказал сделать через два модуля. сделал, но теперь появилась ошибка.
главный модуль unit MainUnit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses VectorUnit; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var V, SecondV: TVector; a,b : array [0..10] of integer; i, N: integer; R: double; fresouble; begin memo1.Lines.Clear; Randomize; V:=TVector.Create; SecondV:=TVector.Create; try V.Length:=2; SecondV.Length:=2; for i:=0 to V.Length-1 do begin a[i]:=random(10); V[i]:=a[i]; end; for i:=0 to SecondV.Length-1 do begin b[i]:=random(10); secondv[i]:=b[i]; end; memo1.lines.Add('Координаты первого вектора'); Memo1.Lines.add(V.AsString); memo1.lines.Add('Координаты второго вектора'); Memo1.Lines.add(SecondV.AsString); V.Add(SecondV); memo1.lines.Add('Сумма векторов'); memo1.Lines.Add(V.AsString); n:=random(10); v.ReturnValues(a); v.Scalar(n); memo1.Lines.add('Произведение первого вектора на число '+inttostr(n)); memo1.lines.Add(v.AsString); secondv.ReturnValues(b); secondv.Scalar(n); memo1.Lines.add('Произведение второго вектора на число '+inttostr(n)); memo1.lines.Add(secondv.AsString); v.ReturnValues(a); secondv.ReturnValues(b); V.Product(secondv); memo1.lines.Add('Скалярное произведение векторов'); Memo1.Lines.add(floattostr(v.fres)) ;ошибка [Error] MainUnit.pas(76): Undeclared identifier: 'fres' finally V.Free; SecondV.Free; end; end; end. второй модуль unit VectorUnit; interface uses SysUtils, Classes; type TVector = class private FValues: array of real; FResouble; function GetLength:integer; procedure SetLength(const Value: integer); function GetValues(Index: integer)ouble; procedure SetValues(Index: integer; const Value: double); procedure CheckIndex(Index: integer); public constructor Create; function AsString: string; property Values[Index: integer]: double read GetValues Write SetValues; default; property Length: integer read GetLength write SetLength; procedure Add(const SecondVector: TVector); overload; procedure Scalar(Factor: integer); overload; procedure Product(const SecondVector: TVector); overload; procedure ReturnValues(a : array of integer); end; implementation constructor TVector.Create; begin System.SetLength(Fvalues, 1); end; function TVector.AsString: string; var i: integer; begin Result := '('; for i:=0 to Length-1 do begin if i>0 then Result:= Result+' , '; Result:=Result+Format('%.2n',[Values[i]]); end; Result:=Result+')'; end; function TVector.GetLength: integer; begin Result:=System.Length(FValues); end; function TVector.GetValues(Index: integer): double; begin CheckIndex(Index); Result:=FValues[Index]; end; procedure TVector.SetLength(const Value: integer); begin if Value<1 then raise Exception.Create('Invalid vector length'); System.SetLength(FValues, Value); end; procedure TVector.SetValues(Index: integer; const Value: double); begin CheckIndex(Index); FValues[Index]:=Value; end; procedure TVector.CheckIndex(Index: integer); begin if (Index>=0) and (Index>=Length) then raise Exception.Create('Index out of the bounds'); end; Procedure TVector.Product(const SecondVector: TVector); var i: integer; Begin fres:=0; For i:=0 to SecondVector.Length-1 do begin Fvalues[I]:= self[i] * SecondVector[i]; fres:=fres+fvalues[i]; end; end; Procedure TVector.Scalar(factor : integer); var i: integer; Begin For i:=0 to self.Length-1 do Fvalues[I]:= Self[i] * factor; end; Procedure TVector.Add(const SecondVector: TVector); var i: integer; Begin For i:=0 to SecondVector.Length-1 do Fvalues[I]:= Self[i] + SecondVector[i]; end; Procedure TVector.ReturnValues(a : array of integer); var i: integer; Begin For i:=0 to self.Length-1 do Fvalues[I]:= a[i]; end; end. |
21.09.2011, 07:16 | #2 |
Старожил
Регистрация: 25.08.2011
Сообщений: 2,841
|
Код:
Сделайте public. Код:
Skype - wmaster_s E-Mail - WorldMasters@gmail.com
Работаем по 3 критериям - быстро, качественно, недорого. Заказчик выбирает любые два. Последний раз редактировалось WorldMaster; 21.09.2011 в 07:19. |
21.09.2011, 07:18 | #3 |
personality
Старожил
Регистрация: 28.04.2009
Сообщений: 2,882
|
1. код надо оформлять тегом [code][/ code] кнопочкой #
2. Memo1.Lines.add(floattostr(v.fres)) ; тут вы обращаетесь к полю написанного Вами класса, но такое поле в private, значит недоступно другим модулям, оберните его в property с методом для получения или измените видимость на public! Возможно , кстати, надо написать просто Memo1.Lines.add(floattostr(fres)) ; а то у Вас и локальная переменная такая есть. |
21.09.2011, 22:40 | #4 |
Форумчанин
Регистрация: 09.04.2011
Сообщений: 134
|
Всем спасибо за помощь. Вы мне очень помогли.
|
Похожие темы | ||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
скалярное произведение векторов | Wika17 | Паскаль, Turbo Pascal, PascalABC.NET | 1 | 29.12.2010 11:32 |
скалярное произведение | tilperion777 | Паскаль, Turbo Pascal, PascalABC.NET | 0 | 11.04.2010 16:13 |
delphi скалярное произведение | Annie | Помощь студентам | 5 | 24.03.2010 10:35 |
Скалярное произведение. Рекурсия. | grave123 | Общие вопросы C/C++ | 0 | 20.12.2009 13:31 |