Welcome to Laser Pointer Forums - discuss green laser pointers, blue laser pointers, and all types of lasers

LPF Donation via Stripe | LPF Donation - Other Methods

Links below open in new window

ArcticMyst Security by Avery

Any one know how to program in Turbo Pascal 7?

pauln

0
Joined
Oct 23, 2009
Messages
6
Points
0
Personally I don't feel that java would be a good choice for this type of application, unless you're doing a web page. Pascal is ideal for this type of coding, as you can code and test as you go. Instant feedback. Whenever I need a small program to do something, I write it in pascal, makes very fast work of small problems.

You should be aware of several things: get a copy of the swag code: Main SWAG (Sourceware Archive Group) HTML Index contains lots of useful pascal code snippets.

You also should consider having a copy of the Free Pascal compiler, its free and available for most CPUs.

Now, back to the problem. It's interesting, I haven't done the math for years so I checked wikipedia (don't right now, its wrong!) and wrote this: (let me know what you think, I working on a version that will allow you to change the amount of decimals in the answer on the fly!)

program Quadratics;

uses
crt;

const
reada = 'Please enter the value of a ';
readb = 'Please enter the value of b ';
readc = 'Please enter the value of c ';

var
e: integer;
a2,
x,
x1,
x2,
ix,
a,
b,
c,
d,
z: real;
ch: char;
done: boolean;

function getReal (s: string): real;
var
r: real;
rs: string;
begin
repeat
write (s);
readln (rs);
val (rs, r, e);
if (e <> 0) then
writeln ('Input must be a real number!');
until (e = 0);
getReal := r;
end;

function noZero (r: real; n: integer): string;
var
s: string;
begin
str (r:0:n, s);
while (s[length (s)] = '0') do
delete (s, length (s), 1);
if (s[length (s)] = '.') then
delete (s, length (s), 1);
noZero := s;
end;

begin
textColor (yellow);
done := false;
clrscr;
Writeln ('Hi, I am the Quadratic Calculator!');
writeln ('Press enter to get started');
ch := readKey;
clrscr;
while not done do begin
writeln;
textColor (lightGreen);
writeln ('Input the values for the Quadratic equation!');
textColor (yellow);
writeln;
Writeln ('Please use this as a guide! ax^2 + bx + c');
writeln;
a := getReal (reada);
b := getReal (readb);
c := getReal (readc);

d := (b * b) - (4 * a * c); { find the discriminant }
a2 := 2 * a;

writeln;
if (d = 0) then begin { discriminant is zero, one real root }
x1 := -b / a2;
writeln ('x = ', noZero (x1, 3));
end
else
if (d > 0) then begin { discriminant is positive, there are 2 roots }
x1 := (-b + sqrt (d)) / a2;
x2 := (-b - sqrt (d)) / a2;
writeln ('x = ', noZero (x1, 3), ', ', noZero (x2, 3));
end
else begin { discriminant is negative, roots are complex }
x1 := -b / a2;
ix := sqrt (abs ((b * b) - 4 * a * c)) / a2;
writeln ('x = ', noZero (x1, 3), ' + i', noZero (ix, 3),
', ', noZero (x1, 3), ' - i', noZero (ix, 3));
end;
writeln;
{
x := (-b) + Sqrt ((b * b) - (4 * a * c));
x := x / 2 * a;
x2 := (-b) - Sqrt (((b * b) - (4 * a * c)));
x2 := x2 / 2 * a;

clrscr;
writeln ('WOW THAT WAS EASY!');
writeln;
write ('x= ');
write (x:2:2);
write (' or ');
writeln (x2:2:2);
writeln;
writeln ('Type any number and press enter to exit!');
readln (z); }

writeln ('Esc to quit, any other key to go again');
ch := readkey;
if (ch = #27) then
done := true;
end;
end.
 





Joined
May 31, 2009
Messages
3,239
Points
63
Hey thats great... one problem tho..

out put looks like this (a=3 b=7 c=9)

Code:
x= -1.167 +  i1.28, -1.167 - i1.28

you also get the ixxx thing when b is neg
 

pauln

0
Joined
Oct 23, 2009
Messages
6
Points
0
But that's not a problem, that's the answer! The "i" represents the square root of -1.

Whenever the determinant (b*b-4ac) is negative, the answer will be complex. A complex number has two parts, a part called the real part and a part called the complex part. Your program was stopping because you were asking it to compute the square root of a minus number, turbo pascal sqrt function cannot do this. If you were to do it with a scientific calculator (like the TI-89) in complex mode it would also return the x+iy type of answer.

By moving the -1 out of the square root radical (the "i") the determinant is now positive and the result can be calculated. When the determinant is negative, there are always two answers. One with the form x+iy and one with the form x-iy.

Check out the wikipedia site on quadratic equation, there is much information there.
 

pauln

0
Joined
Oct 23, 2009
Messages
6
Points
0
I wrote that program in about an hour before I sent it. That's what I love about pascal, and you end up with an executable.
 

pauln

0
Joined
Oct 23, 2009
Messages
6
Points
0
Complex numbers were invented many years before there was a physical use for them. AC voltage/current calculations use them, amoung many others.
 
Joined
May 31, 2009
Messages
3,239
Points
63
OHHH I see.. yes I understand now! I just thought it was a bug! Kool! thanks! +rep!
 

pauln

0
Joined
Oct 23, 2009
Messages
6
Points
0
Here's the code for the improved program, let's you change the decimals on the fly.

program Quadratics;

uses
crt;

const
reada = 'Please enter the value of a: ';
readb = 'Please enter the value of b: ';
readc = 'Please enter the value of c: ';

var
e: integer;
a2,
x,
x1,
x2,
ix,
a,
b,
c,
d,
z: real;
ch: char;
done: boolean;

function getReal (s: string): real;
var
r: real;
rs: string;
begin
repeat
write (s);
readln (rs);
val (rs, r, e);
if (e <> 0) then
writeln ('Input must be a real number!');
until (e = 0);
getReal := r;
end;

function noZero (r: real; n: integer): string;
var
s: string;
begin
str (r:0:n, s);
while (s[length (s)] = '0') do
delete (s, length (s), 1);
if (s[length (s)] = '.') then
delete (s, length (s), 1);
noZero := s;
end;

var
t,
y: integer;
s: string;
next: boolean;
begin
textColor (yellow);
done := false;
clrscr;
Writeln ('Quadratic Calculator!');
writeln;
writeln (' If there are decimals, you may press the "+" and "-" keys to change');
writeln (' the amount of decimals shown or any other keys to continue');
writeln;
writeln ('Press enter to get started');
writeln;
ch := readKey;
clrscr;
while not done do begin
writeln;
textColor (lightGreen);
writeln ('Input the values for the Quadratic equation!');
textColor (yellow);
writeln;
Writeln ('Please use this as a guide! ax^2 + bx + c');
writeln;
a := getReal (reada);
b := getReal (readb);
c := getReal (readc);

d := (b * b) - (4 * a * c); { find the discriminant }
a2 := 2 * a;

writeln;
if (d = 0) then begin { discriminant is zero, one real root }
x1 := -b / a2;
writeln ('x = ', noZero (x1, 3));
end
else
if (d > 0) then begin { discriminant is positive, there are 2 roots }
x1 := (-b + sqrt (d)) / a2;
x2 := (-b - sqrt (d)) / a2;
writeln ('x = ', noZero (x1, 3), ', ', noZero (x2, 3));
end
else begin { discriminant is negative, roots are complex }
x1 := -b / a2;
ix := sqrt (abs ((b * b) - 4 * a * c)) / a2;
next := false;
t := 3;
repeat
s := 'x = ' + noZero (x1, t) + ' + i' + noZero (ix, t) +
', ' + noZero (x1, t) + ' - i' + noZero (ix, t);
y := length (s);
s := s + copy (' ', 1, 70-y) + #13;
write (s);
ch := readKey;
case ch of
'+': begin
inc (t);
if (t > 9) then
t := 9;
end;
'-': begin
dec (t);
if (t < 0) then
t := 0;
end
else
next := true;
end;
until (next);
writeln;
end;
writeln;

writeln ('Esc to quit, any other key to go again');
ch := readkey;
if (ch = #27) then
done := true;
end;
end.

Have fun.

Paul
 

pauln

0
Joined
Oct 23, 2009
Messages
6
Points
0
Oops, one small problem, try this instead:

program Quadratics;

uses
crt;

const
reada = 'Please enter the value of a: ';
readb = 'Please enter the value of b: ';
readc = 'Please enter the value of c: ';

var
e: integer;
a2,
x,
x1,
x2,
ix,
a,
b,
c,
d,
z: real;
ch: char;
done: boolean;

function getReal (s: string): real;
var
r: real;
rs: string;
begin
repeat
write (s);
readln (rs);
val (rs, r, e);
if (e <> 0) then
writeln ('Input must be a real number!');
until (e = 0);
getReal := r;
end;

function noZero (r: real; n: integer): string;
var
s: string;
begin
str (r:0:n, s);
while (s[length (s)] = '0') do
delete (s, length (s), 1);
if (s[length (s)] = '.') then
delete (s, length (s), 1);
noZero := s;
end;

var
t,
y: integer;
s: string;
next: boolean;
begin
textColor (yellow);
done := false;
clrscr;
Writeln ('Quadratic Calculator!');
writeln;
writeln (' If there are decimals, you may press the "+" and "-" keys to change');
writeln (' the amount of decimals shown or any other keys to continue');
writeln;
writeln ('Press enter to get started');
writeln;
ch := readKey;
clrscr;
while not done do begin
writeln;
textColor (lightGreen);
writeln ('Input the values for the Quadratic equation!');
textColor (yellow);
writeln;
Writeln ('Please use this as a guide! ax^2 + bx + c');
writeln;
a := getReal (reada);
b := getReal (readb);
c := getReal (readc);

d := (b * b) - (4 * a * c); { find the discriminant }
a2 := 2 * a;

writeln;
next := false;
t := 3;
if (d = 0) then begin { discriminant is zero, one real root }
x1 := -b / a2;
repeat
s := 'x = ' + noZero (x1, t);
y := length (s);
s := s + copy (' ', 1, 30-y) + #13;
write (s);
ch := readKey;
case ch of
'+': begin
inc (t);
if (t > 9) then
t := 9;
end;
'-': begin
dec (t);
if (t < 0) then
t := 0;
end
else
next := true;
end;
until (next);
writeln;
end
else
if (d > 0) then begin { discriminant is positive, there are 2 roots }
x1 := (-b + sqrt (d)) / a2;
x2 := (-b - sqrt (d)) / a2;
repeat
s := 'x = ' + noZero (x1, t) + ', ' + noZero (x2, t);
y := length (s);
s := s + copy (' ', 1, 40-y) + #13;
write (s);
ch := readKey;
case ch of
'+': begin
inc (t);
if (t > 9) then
t := 9;
end;
'-': begin
dec (t);
if (t < 0) then
t := 0;
end
else
next := true;
end;
until (next);
writeln;
end
else begin { discriminant is negative, roots are complex }
x1 := -b / a2;
ix := sqrt (abs ((b * b) - 4 * a * c)) / a2;
repeat
s := 'x = ' + noZero (x1, t) + ' + i' + noZero (ix, t) +
', ' + noZero (x1, t) + ' - i' + noZero (ix, t);
y := length (s);
s := s + copy (' ', 1, 70-y) + #13;
write (s);
ch := readKey;
case ch of
'+': begin
inc (t);
if (t > 9) then
t := 9;
end;
'-': begin
dec (t);
if (t < 0) then
t := 0;
end
else
next := true;
end;
until (next);
writeln;
end;
writeln;

writeln ('Esc to quit, any other key to go again');
ch := readkey;
if (ch = #27) then
done := true;
end;
end.

Paul
 




Top