Markus
2007-12-19 14:39:45
Tired of doing your homework? Or you cant understand howto do it?
Well, here is the ultimate quadratic function solver :D
Features
Changelog
1.1
Will solve linear functions now too ^^
1.0
Initial release
This script will teach you:
Bad standards at the end, oh well, who cares, it does the job :)
Well, here is the ultimate quadratic function solver :D
Features
- Great detection of A, B, C
- Demonstrates how the ABC formula works
- Works all the time, tested it at some old maths test :sasmokin:
- Now solves linear functions!
Changelog
1.1
Will solve linear functions now too ^^
1.0
Initial release
This script will teach you:
- Basic string manipulation
- How the ABC formula works
- How to solve linear functions
program ABCFormulaDemo;
const formula = '-10x+2x²=0';
//const formula = '2x²+3x=5';
const bool = true; //Want answer one or two? (Only for quadratic functions
function GetX(s : string; add : boolean) : extended;
var
a, b, c, d, i : integer;
a2, b2, c2 : integer;
s1, s2, ts : string;
begin
if (pos('=', s) = 0) then exit;
writeln('Formula: '+s);
s := trim(s);
s := Replace(s, ' ', '');
s1 := copy(s, 1, pos('=', s)-1);
s2 := copy(s, pos('=', s)+1, length(s));
writeln(s1);
writeln(s2);
writeln('Trying to get A of right side');
if (pos('x²', s2) <> 0) then
begin
for i := pos('x²', s2)-1 downto 1 do
begin
if (pos(s2[i], '-1234567890') = 0) then break;
ts := s2[i]+ts;
s2[i] := '&';
end;
end;
s2 := replace(s2, '&', '');
s2 := replace(s2, 'x²', '');
writeln(ts);
writeln(s2);
writeln('ea');
a2 := strtointdef(ts, 0);
ts := '';
writeln('Now trying to get B');
if (pos('x', s2) <> 0) then
begin
for i := pos('x', s2)-1 downto 1 do
begin
if (pos(s2[i], '-1234567890') = 0) then break;
ts := s2[i]+ts;
if s2[i]= '-' then
begin
s2[i] := '&';
break;
end else
s2[i] := '&';
end;
end else
writeln('No b2');
s2 := replace(s2, '&', '');
s2 := replace(s2, 'x', '');
writeln(ts);
writeln(s2);
b2 := strtointdef(ts, 0);
writeln('All other stuff is C2');
if (pos('-', s2) <> 0) then
begin
c2 := strtoint(getnumbers(s2));
c2 := -1*c2;
end else
c2 := strtointdef(getnumbers(s2), 0);
writeln('A2 '+INTTOSTR(A2)+' B2 '+INTTOSTR(B2)+' C2 '+INTTOSTR(C2));
ts := '';
if (pos('x²', s1) <> 0) then
begin
for i := pos('x²', s1)-1 downto 1 do
begin
if (pos(s1[i], '-1234567890') = 0) then break;
ts := s1[i]+ts;
s1[i] := '&';
end;
end;
s1 := replace(s1, '&', '');
s1 := replace(s1, 'x²', '');
writeln(ts);
writeln(s1);
a := strtointdef(ts, 0);
ts := '';
writeln('Now trying to get B');
if (pos('x', s1) <> 0) then
begin
for i := pos('x', s1)-1 downto 1 do
begin
if (pos(s1[i], '-1234567890') = 0) then break;
ts := s1[i]+ts;
if s1[i]= '-' then
begin
s1[i] := '&';
break;
end else
s1[i] := '&';
end;
end else
writeln('No b2');
s1 := replace(s1, '&', '');
s1 := replace(s1, 'x', '');
//writeln(ts);
//writeln(s1);
b := strtointdef(ts, 0);
writeln('All other stuff is C');
if (pos('-', s1) <> 0) then
begin
c := strtoint(getnumbers(s1));
c := -1*c;
end else
c := strtointdef(getnumbers(s1), 0);
writeln('A '+INTTOSTR(A)+' B '+INTTOSTR(B)+' C '+INTTOSTR(C));
writeln('Substracting right from left');
a := a - a2;
b := b - b2;
c := c - c2;
writeln('A '+INTTOSTR(A)+' B '+INTTOSTR(B)+' C '+INTTOSTR(C));
writeln('D = '+inttostr(b)+'² -4 * '+inttostr(a)+' * '+inttostr(c));
i := (4*a*c);
d := round(sqr(b)-i);
writeln(inttostr(d));
if (d < 0) then
begin
writeln('D is smaller then 0, that means there is no solution');
exit;
end;
if (c = 0) then
begin
writeln('Linear function! Dividing everything by X');
c := b;
b := a;
a := 0;
end;
if (a = 0) then
begin
c := -1*c; //Move it to the right side
writeln('A '+INTTOSTR(A)+' B '+INTTOSTR(B)+' C '+INTTOSTR(C));
writeln(inttostr(b)+'X = '+inttostr(c));
writeln('X = '+inttostr(c)+' / '+inttostr(b));
result := Int(c) div int(b);
writeln('X = '+floattostr(result));
exit;
end;
if add then
begin
writeln('-'+inttostr(b)+ ' + sqrt('+inttostr(d)+')');
writeln('----------------');
writeln(' 2 * '+inttostr(a));
result := ((-1*b)+sqrt(d))
end
else
begin
writeln('-'+inttostr(b)+ ' - sqrt('+inttostr(d)+')');
writeln('----------------');
writeln(' 2 * '+inttostr(a));
result := ((-1*b)-sqrt(d))
end;
result := result div (2 * a);
writeln('X = '+floattostr(result));
end;
begin
Getx(formula, bool);
end.
Bad standards at the end, oh well, who cares, it does the job :)