|
Умножение многочленов (где ошибки?)
проблемы:
1. работает задание коэффициентов только в первом многочлене, второй не реагирует вообще, хотя коды вроде одинаковы, только менял переменные.
2. он как-то слишком странно выводит многочлен в эдит, иногда пропускается, например "*0x^00x^0", "-9x^0-2x^1*3x^23x^2-10x^3", т.е. вообще неверно задается
3. не уверен в том, что правильно написана процедура перемножения
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Edit2: TEdit;
Button3: TButton;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var
a,b,i,j,k: integer;
Ab:array[0..5,0..5] of integer;
s,t,r:string;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
randomize;
a:=random(5);
s:='';
for I:=0 to a-1 do
begin
randomize;
Ab[i,1]:=random(20)-10;
if Ab[i,1]>=0 then
s:=s+'*'+IntToStr(Ab[i,1])+'x^'+IntToStr(i);
s:=s+IntToStr(Ab[i,1])+'x^'+IntToStr(i);
end;
for j:=i+1 to 3 do
begin
Ab[j,1]:=0
end;
Edit1.Text:=s;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
randomize;
b:=random(5);
t:='';
for i:=0 to b-1 do
begin
randomize;
Ab[i,2]:=random(20)-10;
if Ab[i,2]>=0 then
t:=t+'*'+IntToStr(Ab[i,2])+'x^'+IntToStr(i);
t:=t+IntToStr(Ab[i,2])+'x^'+IntToStr(i);
end;
for j := i+1 to 3 do
begin
Ab[j,2]:=0
end;
Edit2.Text:=t;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
r:='';
begin
for i:=0 to 3 do
if Ab[i,1]=0 then Ab[i,k]:=Ab[i,2]
else if Ab[i,2]=0 then Ab[i,3]:=Ab[i,1]
else if (Ab[i,2]=0) and (Ab[i,1]=0) then Ab[i,3]:=0
else Ab[i,3]:= Ab[i,1]+Ab[i,1]*Ab[i,2];
if Ab[i,3]>0 then
r:=r+'*'+IntToStr(Ab[i,3])+'x^'+IntToStr(i)
else if Ab[i,3]<0 then
r:=r+IntToStr(Ab[i,3])+'x^'+IntToStr(i) ;
end;
Edit3.Text:=r;
end;
end.
|