SRL Archive - Other Scripts

Math Homework maker! (quadratic functions)


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
  • 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 :)

Wizzup?

2007-12-19 15:31:18

Nice.

http://www.math.harvard.edu/computing/maxima/

Brilliant. ;)

Markus

2007-12-19 15:45:59

Ah, so it actually existed already? Never knew about Maxima, it looks nice :D

mastaraymond

2007-12-19 18:22:57

Well done!

I see a young benland :p

Bobarkinator

2007-12-19 22:29:25

Very nice! I might use this however I don't have a problem in math

Markus

2007-12-20 10:53:08

Updated!
I added support for linear functions now, it supports them in the form '20x²+5x=12x²+5x' and in the form '10x+5=3x+1' :)
20x²+5x=12x²+5x
20x²+5x-5x-12x=0
20x²-12x²=0
20x-12x=0
8x=0
x=0
And:
10x+5=3x+1
10x+5-1-3x=0
7x+4=0
7x=-4
X = -0,571428571428571

Gu3st...

2008-01-02 05:16:29

(-10)+5-(-10)= Error

EDIT: 2+2= Error
EDIT: 1+1= ERROR?! lol!

Markus

2008-01-07 12:26:56

From: zytex
(-10)+5-(-10)= Error

EDIT: 2+2= Error
EDIT: 1+1= ERROR?! lol!


It's not meant for noob sums -.-
Answer of first one is 5 btw

Gu3st...

2008-01-08 11:21:59

From: Markus
Answer of first one is 5 btw


No kidding?

But maybe add forms? I made a nub-answearer (lol)

program Nub;

const
s = 3*5+2/2+1*3; //Write the question here!

begin
Writeln(IntToStr(S))
end.

Markus

2008-01-08 12:28:48

I made this to be clean code, mixing it with a form makes the code ugly -.-

footballjds

2008-01-08 12:57:25

From: Markus
I made this to be clean code, mixing it with a form makes the code ugly -.-


^^ =p or bad standers makes it ugly =p… jk, looks gr8:rolleyes:

yanix

2008-01-18 16:29:55

Well, I find the idea of even doin your homework is kinda gay, and this script is great but I dont understand anything :D

Freddy1990

2008-01-27 16:20:28

From: mastaraymond
Well done!

I see a young benland :p


It's just simple algebra…

mastaraymond

2008-01-27 16:49:12

From: Freddy1990
It's just simple algebra…

I was only joking.. lol :rolleyes:

malotthouse

2008-01-27 17:23:04

awesome. if i was still in algebra. pre calc for me :)

WhoCares357

2008-03-06 17:20:18

Very well made. Mine looks like child's play compared to yours. By the way, how did you get the superscripts?

mixster

2008-03-06 17:35:01

Yay uber bump! Also, is it just me or does that look much more complex than it needs to be (at a basic level atleast) as all you have to do is get it assign the various parts to variables then run through the 'traditional' way of solving it, only using x,y&z/a,b&c instead of the actual numbers?
Also, <3 vivi :)