Category : Recently Uploaded Files
Archive   : OPSTACK.ZIP
Filename : OPSTACK.PAS

 
Output of file : OPSTACK.PAS contained in archive : OPSTACK.ZIP
{OPSTACK by Matt Hum

This source is released into the public domain intended to teach new
programmers new techniques. you may do whatever you want with this source.

just my two cents...
}
uses
crt,
dos;

const
max=80;{max max amount of data to have in each stack}
cops=['+','-','*','/'];
cnums=['0','1','2','3','4','5','6','7','8','9'];

type
tops=0..3;{your operations, 0=+, 1=-, 2=*, 3=/}
tnumarr=array[0..max] of integer;{your number stack}
toparr=array[0..max] of tops;{your operation stack}

var
numarr:tnumarr;
oparr:toparr;
curnum,curop:byte;{your number and operation stack pointers}

procedure init;
{resetting your stack holder and pointers}
var
a:byte;

begin
for a:=0 to max do
begin
numarr[a]:=0;
oparr[a]:=0;
end;
curnum:=0;
curop:=0;
clrscr;
end;

procedure centertext(s:string;y:byte);
{procedure to make it look nice}
begin
gotoxy(40-(length(s) div 2),y);
write(s);
end;

procedure pushnum(var nums:tnumarr;num:integer);
{add a number to the stack}
begin
if curnum<>max then
begin
nums[curnum]:=num;
inc(curnum);
end;
end;

function popnum(nums:tnumarr):integer;
{pop a number off the stack}
begin
if curnum>=0 then
begin
dec(curnum);
popnum:=nums[curnum];
end;
end;

procedure pushop(var ops:toparr;op:tops);
{add an operation to the stack}
begin
if curop<>max then
begin
ops[curop]:=op;
inc(curop);
end;
end;

function popop(ops:toparr):tops;
{pop an operation off the stack}
begin
if curop<>0 then
begin
dec(curop);
popop:=ops[curop];
end;
end;

procedure putopinstack(op:char);
{figure which operation it is, and then assign it it's numerical value to be
stored in the stack}
var
opnum:tops;

begin
case op of
'+':opnum:=0;
'-':opnum:=1;
'*':opnum:=2;
'/':opnum:=3;
end;
pushop(oparr,opnum);
end;

procedure calcval;
{pops two numbers, and calculates them}
var
tmp1,tmp2:integer;
op:tops;
total:integer;

begin
tmp2:=popnum(numarr);{pops the}
op:=popop(oparr); {numbers and}
tmp1:=popnum(numarr);{operations}
case op of
0:total:=tmp1+tmp2;
1:total:=tmp1-tmp2;
2:total:=tmp1*tmp2;
3:total:=tmp1 div tmp2;
end;
pushnum(numarr,total);{push your answer}
end;

var
s,s1:string;
a,b,col:byte;
tmpnum,code:integer;
abt:boolean;

begin
col:=textattr;{things to make it look nice}
init;
textcolor(15);
centertext('Matt''s Mathematical Gizmo Solver',2);
centertext('(C)1995 by Wavelength Software Inc.',3);
centertext('all rights reserved',4);
textcolor(lightgray);
gotoxy(1,7);
writeln('enter fully parenthesized expresson below:');
abt:=false;
{ input(s,80,1,8,7,false,abt); If you have my units, you can uncomment this
and run it. as of now, my units have yet to
be released to the public domain.
}
readln(s);
gotoxy(1,9);
if abt then { not used here, but used if you have my unit}
begin
clrscr;
halt(1);
end;

a:=1;
repeat
if s[a] in cnums then {found a number}
begin
s1:=s[a];
b:=1;
while s[a+b] in cnums do {is the number more than one digit?}
begin
s1:=s1+s[a+b];
inc(b);
end;
val(s1,tmpnum,code); {get numerical value}
pushnum(numarr,tmpnum); {push it}
inc(a,b-1); {reset your string pointer}
end;
if s[a] in cops then {found an operation}
putopinstack(s[a]);
if s[a]=')' then {found a ")" and calculating values}
calcval;
inc(a); {increase string pointer}
until a>=length(s)+1;

if curnum=2 then {if only 1 operation, or anything left,}
calcval; {like forgot last parenthesis, calculate it}
writeln('The answer is:',numarr[0]);

textcolor(lightgreen+blink); {gotta get my ad in somewhere}
centertext('Call the BioInsanity BBS',15);
centertext('@(301)942-0504 between 5pm-7am',16);
textattr:=col;
readkey;{for those who run it from the IDE}
end.{Hey, done already?}

  3 Responses to “Category : Recently Uploaded Files
Archive   : OPSTACK.ZIP
Filename : OPSTACK.PAS

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/