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;
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;
function sumPolynomial(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;
sumPolynomial := coeficients;
end;
function productOfPolynomial(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;
productOfPolynomial := coeficients;
end;
function substractOfPolynomial(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;
substractOfPolynomial := 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;
var
polynomial1, polynomial2, result: Array[1..21] of Integer;
polynomialInput: String;
begin
readln(polynomialInput);
polynomial1 := parsePolynomial(polynomialInput);
readln(polynomialInput);
polynomial2 := parsePolynomial(polynomialInput);
printPolynomial(sumPolynomial(polynomial1, polynomial2));
writeln('');
printPolynomial(substractOfPolynomial(polynomial1, polynomial2));
writeln('');
printPolynomial(productOfPolynomial(polynomial1, polynomial2));
end.
答案 0 :(得分:1)
运行时错误201表示范围检查错误。快速浏览一下源代码会让我怀疑某个操作返回一个不适合整数范围的值(默认情况下为FreePascal中的-32786..32767)。最简单的解决方案是使用更大的数据类型,例如longint(大致在-2 * 10 ^ 9..2 * 10 ^ 9之间)或int64(~9 * 10 ^ 18..9 * 10 ^ 18)。
如果使用命令行编译器使用-gl
命令行选项,则会在运行时错误回溯中显示行号。这将使您更容易查明问题。