Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Delphi программирование > Паскаль, Turbo Pascal, PascalABC.NET
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 23.06.2009, 15:41   #1
xMoNaHx
Пользователь
 
Регистрация: 23.06.2009
Сообщений: 12
По умолчанию Методы-в класс

У меня есть текст программы.
Она полностью правильная.
В ней есть две функции и процедура.

Их надо выделить в отдельный модуль, в класс.
Также есть модуль, el_func.
Если надо могу выложить.
Если можете сделайте пожалуйста этот класс))
Просто надо завтра с утра сдавать курсовую.Осталось только овт это исправить.

Вот прога:

program parse_expr;
uses el_func;
function BuildTree(var e : string) : PElementaryFunction; forward;
function BuildElement(var e : string) : PElementaryFunction;
var
lp, fname : string(255);
i, j : integer;
r : real;
begin
writeLn('Enter to BuildElement(''' + e + ''')!');
i := 1;

BuildElement := nil;
while ((i <= length(e)) and (e[i] in ['0'..'9', '.'])) do
inc(i);
if (i > 1) then
begin
lp := copy(e, 1, i - 1);
delete(e, 1, i - 1);
val(lp, r, i);
writeLn('Number: ', r:0:2);
if (i <> 0) then
writeLn('error: Invalid number format')
else
BuildElement := CreateFConst(r);
exit;
end;
fname := '';
while ((i <= length(e)) and (e[i] in ['a'..'z', 'A'..'Z'])) do
inc(i);
if (i > 1) then
begin
fname := copy(e, 1, i - 1);
delete(e, 1, i - 1);
if (fname = 'x') then
begin
writeLn('Variable: ' + fname);
BuildElement := CreateFEquivalence;
exit;
end;
i := 1;
writeLn('Function: ' + fname);
end;
if (e[i] <> '(') then
writeLn('error: Invalid expression format')
else
begin
j := 1;
while ((i <= length(e)) and (j > 0)) do
begin
inc(i);
if (e[i] = ')') then
dec(j)
else if (e[i] = '(') then
inc(j);
end;
if (j > 0) then
begin
writeLn('error: ''('' without '')''');
exit;
end;
lp := copy(e, 2, i - 2);
delete(e, 1, i);
writeLn('Expression: ' + lp);
end;
if (fname = '') then
BuildElement := BuildTree(lp)
else if (fname = 'sin') then
BuildElement := CreateFunction(CreateFSin, BuildTree(lp), oF1ofF2)
else if (fname = 'cos') then
BuildElement := CreateFunction(CreateFCos, BuildTree(lp), oF1ofF2)
else if (fname = 'tg') then
BuildElement := CreateFunction(CreateFTg, BuildTree(lp), oF1ofF2)
else if (fname = 'ctg') then
BuildElement := CreateFunction(CreateFCtg, BuildTree(lp), oF1ofF2)
else if (fname = 'arcsin') then
BuildElement := CreateFunction(CreateFArcSin, BuildTree(lp), oF1ofF2)
else if (fname = 'arccos') then
BuildElement := CreateFunction(CreateFArcCos, BuildTree(lp), oF1ofF2)
else if (fname = 'arctg') then
BuildElement := CreateFunction(CreateFArcTg, BuildTree(lp), oF1ofF2)
else if (fname = 'arcctg') then
BuildElement := CreateFunction(CreateFArcCtg, BuildTree(lp), oF1ofF2)
end;

function BuildTree(var e : string) : PElementaryFunction;
var
lf : PElementaryFunction;
begin
writeLn('Enter to BuildTree(''' + e + ''')!');
BuildTree := nil;
lf := BuildElement(e);
while (length(e) > 0) do
if (e[1] = '*') then
begin
writeLn('*');
delete(e, 1, 1);
lf := CreateFunction(lf, BuildElement(e), oMul);
writeLn('buildtree: e = ' + e);
end else if (e[1] = '/') then
begin
writeLn('/');
delete(e, 1, 1);
lf := CreateFunction(lf, BuildElement(e), oDiv);
writeLn('buildtree: e = ' + e);
end else if (e[1] = '+') then
begin
writeLn('+');
delete(e, 1, 1);
lf := CreateFunction(lf, BuildTree(e), oAdd);
writeLn('buildtree: e = ' + e);
end else if (e[1] = '-') then
begin
writeLn('-');
delete(e, 1, 1);
lf := CreateFunction(lf, BuildTree(e), oSub);
writeLn('buildtree: e = ' + e);
end;
BuildTree := lf;
end;

procedure OutputTree(f : PElementaryFunction);
begin
writeLn('Tree:');
f^.OutputFLine(0);
end;

var
s, t : string(255);
tree : PElementaryFunction;

begin
write('Enter expression: ');
readLn(s);{}
t := s;
tree := BuildTree(t);
writeLn('s = ' + s);
if (tree <> nil) then
OutputTree(tree)
else
writeLn('Tree is null');
end.
xMoNaHx вне форума Ответить с цитированием
Старый 23.06.2009, 16:02   #2
Stilet
Белик Виталий :)
Старожил
 
Аватар для Stilet
 
Регистрация: 23.07.2007
Сообщений: 57,097
По умолчанию

Ну в целом выглядеть это должно примерно так:
Код:
program Project1;

type
   PElementaryFunction=^boolean;
  tot=object
   function BuildTree(var e : string) : PElementaryFunction;
   procedure OutputTree(f : PElementaryFunction);
  end;


  function tot.BuildTree(var e : string) : PElementaryFunction;
var
lf : PElementaryFunction;
begin
writeLn('Enter to BuildTree(''' + e + ''')!');
BuildTree := nil;
while (length(e) > 0) do
if (e[1] = '*') then
begin
writeLn('*');
delete(e, 1, 1);
writeLn('buildtree: e = ' + e);
end else if (e[1] = '/') then
begin
writeLn('/');
delete(e, 1, 1);
writeLn('buildtree: e = ' + e);
end else if (e[1] = '+') then
begin
writeLn('+');
delete(e, 1, 1);
writeLn('buildtree: e = ' + e);
end else if (e[1] = '-') then
begin
writeLn('-');
delete(e, 1, 1);
writeLn('buildtree: e = ' + e);
end;
BuildTree := lf;
end;

procedure tot.OutputTree(f : PElementaryFunction);
begin
writeLn('Tree:');
end;

begin

  { TODO -oUser -cConsole Main : Insert code here }
end.
Но код твой неполный и абсолютно неработоспособен.
I'm learning to live...
Stilet вне форума Ответить с цитированием
Старый 23.06.2009, 16:25   #3
xMoNaHx
Пользователь
 
Регистрация: 23.06.2009
Сообщений: 12
По умолчанию

а функция BuildElement??
сейчас выложу модуль,может яснее станет)
Кстати прога работает нормально
xMoNaHx вне форума Ответить с цитированием
Старый 23.06.2009, 16:25   #4
xMoNaHx
Пользователь
 
Регистрация: 23.06.2009
Сообщений: 12
По умолчанию

кстати,и как потом вызывать эти методы в основной проге
xMoNaHx вне форума Ответить с цитированием
Старый 23.06.2009, 16:27   #5
xMoNaHx
Пользователь
 
Регистрация: 23.06.2009
Сообщений: 12
По умолчанию

unit el_func;

interface

const
OUT_WIDTH = 0;
OUT_DECIMALS = 2;

type
TCalcResult = (crOk, crFail);
TOperation = (oNULL, oOnlyFirst, oOnlySecond,
oAdd, oSub, oMul, oDiv, oPow, oLog, oF1ofF2);

PString =^string;

PBaseFunction =^TBaseFunction;
TBaseFunction = object
private
Name : PString;

public
constructor Create(s : string);
destructor Destroy;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PElementaryFunction =^TElementaryFunction;
TElementaryFunction = object (TBaseFunction)
private
Param : real;

public
constructor Create(s : string; p : real);
function GetParam : real;
procedure SetParam(p : real);
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFunction =^TFunction;
TFunction = object (TElementaryFunction)
private
Operation : TOperation;
First, Second : PElementaryFunction;

public
constructor Create(func1, func2 : PElementaryFunction; op : TOperation);
destructor Destroy;
procedure SetParam(p : real);
function SetOperation(op : TOperation) : TOperation;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

{ -= Elementary functions =- }
PFConst =^TFConst;
TFConst = object (TElementaryFunction)
public
constructor Create(p : real);
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFEquivalence =^TFEquivalence;
TFEquivalence = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFSin =^TFSin;
TFSin = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFCos =^TFCos;
TFCos = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFTg =^TFTg;
TFTg = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFCtg =^TFCtg;
TFCtg = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFArcSin =^TFArcSin;
TFArcSin = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFArcCos =^TFArcCos;
TFArcCos = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFArcTg =^TFArcTg;
TFArcTg = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

PFArcCtg =^TFArcCtg;
TFArcCtg = object (TElementaryFunction)
public
constructor Create;
function Calculate(x : real; var r : real) : TCalcResult; virtual;
procedure OutputFLine(level : integer); virtual;
end;

function CreateFunction(func1, func2 : PElementaryFunction; op : TOperation) : PFunction;
function CreateFConst(c : real) : PElementaryFunction;
function CreateFEquivalence : PElementaryFunction;
function CreateFSin : PElementaryFunction;
function CreateFCos : PElementaryFunction;
function CreateFTg : PElementaryFunction;
function CreateFCtg : PElementaryFunction;
function CreateFArcSin : PElementaryFunction;
function CreateFArcCos : PElementaryFunction;
function CreateFArcTg : PElementaryFunction;
function CreateFArcCtg : PElementaryFunction;

implementation
xMoNaHx вне форума Ответить с цитированием
Старый 23.06.2009, 16:27   #6
xMoNaHx
Пользователь
 
Регистрация: 23.06.2009
Сообщений: 12
По умолчанию

это без раздела реализации(описания этих методов),так как слишком много)
xMoNaHx вне форума Ответить с цитированием
Старый 23.06.2009, 16:32   #7
Stilet
Белик Виталий :)
Старожил
 
Аватар для Stilet
 
Регистрация: 23.07.2007
Сообщений: 57,097
По умолчанию

Цитата:
кстати,и как потом вызывать эти методы в основной проге
Создай переменку типа tot и вызывай ее методы:
переменка.OutputTree(чето там...)
I'm learning to live...
Stilet вне форума Ответить с цитированием
Старый 23.06.2009, 16:40   #8
xMoNaHx
Пользователь
 
Регистрация: 23.06.2009
Сообщений: 12
По умолчанию

посмотри на мой модуль тоже.
а этот класс не должен иметь родителя?
xMoNaHx вне форума Ответить с цитированием
Старый 23.06.2009, 16:44   #9
Stilet
Белик Виталий :)
Старожил
 
Аватар для Stilet
 
Регистрация: 23.07.2007
Сообщений: 57,097
По умолчанию

Во-первых: Я не пойму зачем тебе функции Create...?
Ведь если ты обернул некоторые функции в обьект, то "создавать" их проще просто создав их обьект.
Во-вторых: Соответственно зачем тебе указатели на эти обьекты?
I'm learning to live...
Stilet вне форума Ответить с цитированием
Старый 23.06.2009, 16:50   #10
xMoNaHx
Пользователь
 
Регистрация: 23.06.2009
Сообщений: 12
По умолчанию

мне не за чеМ))
а преподу зачем то надо))
это все им уже просмотрено и исправлено.
Он только сказал сделать то что я написал в первом сообщении
xMoNaHx вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
численные методы Desha Помощь студентам 2 24.05.2009 12:46
Численные методы improvement Общие вопросы .NET 4 08.05.2009 01:58
методы в ООП albatros Общие вопросы Delphi 3 04.02.2009 22:59
Методы... Arkuz Свободное общение 6 11.10.2008 16:53
Методы шифрования D@rk M@k Свободное общение 3 27.02.2008 13:56