Dobrý den,
chtěl bych se zeptat, jak vyřešit u toho příkladu chybu -runtime 201. Už si vůbec nevím rady. Předem díky za odpověd.
Program Polynomial;
type
arrayOfInt = Array[1..21] of Integer;
biggerArrayOfInt = Array[1..41] of Integer;
function isNumber(c : Char): Boolean;
var
res : Boolean;
code : Longint;
begin
code := Ord(c);
if ((code > 47) AND (code < 58)) then begin
res := true
end
else
begin
res := false;
end;
isNumber := res;
end;
//parse
function parsePolynomial(polynomial : String): arrayOfInt;
var
coeficients : Array[1..21] of Integer;
number : Integer;
coef : Integer;
tmp : String;
i, j : Integer;
positive : Boolean;
numberPosition, numberLength : Integer;
expectX : Boolean;
begin
i := 1;
for j:=1 to Length(coeficients) do
begin
coeficients[j] := 0;
end;
while (true) do
begin
coef := 0;
number := 0;
positive := true;
expectX := true;
if(polynomial[i] = '-') then begin
positive := false;
i := i + 1;
end;
if(polynomial[i] = '+') then begin
i := i + 1;
end;
if(isNumber(polynomial[i])) then begin
numberPosition:= i;
while (isNumber(polynomial[i])) do
begin
i := i + 1;
end;
tmp := Copy(polynomial, numberPosition, i - numberPosition);
Val(tmp, number);
if(not positive) then begin
number := number * -1;
end;
if ((not(polynomial[i] = '*')) OR (i > length(polynomial))) then begin
expectX := false;
end
else
begin
i:= i + 1;
end;
end
else
begin
if(positive) then begin
number := 1;
end
else
begin
number := -1;
end;
end;
if (expectX) then begin
if(not(polynomial[i] = 'x')) then begin
write('Bad input!');
exit;
end
else
begin
i := i + 1;
if (polynomial[i] = '^') then begin
i := i + 1;
if (not isNumber(polynomial[i])) then begin
write('Bad input!');
exit;
end;
numberPosition:= i;
while (isNumber(polynomial[i])) do
begin
i := i + 1;
end;
tmp := Copy(polynomial, numberPosition, i - numberPosition);
Val(tmp, coef);
end
else
begin
coef := 1;
end;
end;
end;
coeficients[coef + 1] := number;
if ((length(polynomial)) - 1 < i) then begin
break;
end;
end;
parsePolynomial := coeficients;
end;
//sum
function sumPolynomials(polynomial1, polynomial2 : array of Integer): arrayOfInt;
var
coeficients : Array[1..21] of Integer;
i : Integer;
begin
for i := Length(polynomial1) downto 0 do
begin
coeficients[i+1] := polynomial1[i] + polynomial2[i];
end;
sumPolynomials := coeficients;
end;
//product
function productOfPolynomails(polynomial1, polynomial2 : array of Integer): biggerArrayOfInt;
var
coeficients : Array[1..41] of Integer;
i, j : Integer;
begin
for j:=1 to Length(coeficients) do
begin
coeficients[j] := 0;
end;
for i := Length(polynomial1) downto 0 do
begin
for j := Length(polynomial2) downto 0 do
begin
coeficients[i+j+1] := coeficients[i+j+1] + polynomial1[i] * polynomial2[j];
end;
end;
productOfPolynomails := coeficients;
end;
//division
function substractOfPolynomails(polynomial1, polynomial2 : array of Integer): arrayOfInt;
var
coeficients : Array[1..21] of Integer;
i : Integer;
begin
for i := Length(polynomial1) downto 0 do
begin
coeficients[i+1] := polynomial1[i] - polynomial2[i];
end;
substractOfPolynomails := coeficients;
end;
procedure printPolynomial(polynomial: array of Integer);
var
i : Integer;
isFirst : Boolean;
isZero : Boolean;
begin
isFirst := true;
isZero := true;
for i := length(polynomial) downto 0 do
begin
if polynomial[i] <> 0 then begin
isZero := false;
if((not isFirst) AND (polynomial[i] > 0)) then begin
write('+');
end;
if((polynomial[i] = -1)) then begin
write('-');
end;
if(((polynomial[i] > 1) OR (polynomial[i] < -1)) OR ((i = 0) AND not(polynomial[i] = 0))) then begin
write(polynomial[i]);
if((i > 0)) then begin
write('*');
end;
end;
if(i > 0) then begin
write('x');
isFirst := false;
if (i > 1) then begin
write('^', i);
end;
end;
write()
end;
end;
if (isZero) then begin
write(0);
end;
end;
//program logic
var
polynomial1, polynomial2, result: Array[1..21] of Integer;
polynomialInput: String;
begin
readln(polynomialInput);
polynomial1 := parsePolynomial(polynomialInput);
readln(polynomialInput);
polynomial2 := parsePolynomial(polynomialInput);
printPolynomial(sumPolynomials(polynomial1, polynomial2));
writeln('');
printPolynomial(substractOfPolynomails(polynomial1, polynomial2));
writeln('');
printPolynomial(productOfPolynomails(polynomial1, polynomial2));
end.