Article View: comp.lang.pascal.borland
Article #588Re: Calculator
From: langtons manawatu.gen.nz Michael...
Date: Wed, 24 Sep 1997 00:00
Date: Wed, 24 Sep 1997 00:00
73 lines
3124 bytes
3124 bytes
Hello,
>> I just started with Pascal.. now I got a little problem! I want to do a
>> Calculator.. I want the calculator to look like this:
>>
>> 3+4+5+9 [enter]
>> Answer: 21
>>
>> I want to be able to use +,-,*,/ and write many numbers on the same row. I
>> hope someone can help me, maybe you know a good idea...
> That is a very hard problem in Pascal to solve. You have 2 solutions :
> 1 you take a such function from a book or from a website
> 2 you write it
Not hard at all, I just made one - it only took 15 minutes!
Here it is, it's not very elegantly done, and works from left to right
only (i.e. no operator precedence), and it's possible that there's
bugs, but it gave the right answer in a few tests.
> If you decide to write it, you should be a good programmer in
>Pascal. (you have to know pointers...)
Check out the lack of them...Though to implement operator precendence
a stack (presumably implemented with pointers) would be required.
{begin program calculator}
var
exp, s : string;
op : char;
i : integer;
a, b : real;
procedure process;
var
t : integer;
begin
val(s, b, t);
case op of
'+' : a := a+b;
'-' : a := a-b;
'*' : a := a*b;
'/' : a := a/b;
end; {case}
end; {process}
begin
write('Enter expression: ');
readln(exp);
s := '';
a := 0;
b := 0;
op := '+';
for i := 1 to length(exp) do
begin
if ((exp[i]>='0') and (exp[i]<='9')) or (exp[i]='.') then
s := s+exp[i]
else
case exp[i] of
'+', '-', '*', '/' : begin
process;
op := exp[i];
s := '';
end; {case +-*/}
else
end; {case}
end; {for}
process;
writeln('Result: ', a:10:10);
end.
{end program calculator}
--
Michael Langton <langtons@<remove>manawatu.gen.nz>
Technical Details
Message-ID:
<3428cb14.3343505@news.manawatu.gen.nz>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<01bcc51b$3c593780$a65ac6c2@anders> <3422EC5A.5B3E@club-internet.fr>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10261c,173f658d4bc382ca X-Google-Attributes: gid10261c,public X-Google-Thread: 102601,173f658d4bc382ca X-Google-Attributes: gid102601,public X-Google-Thread: fea06,173f658d4bc382ca X-Google-Attributes: gidfea06,public X-Google-Thread: 111659,173f658d4bc382ca X-Google-Attributes: gid111659,public X-Google-Thread: 10263e,173f658d4bc382ca X-Google-Attributes: gid10263e,public From: langtons@<remove>manawatu.gen.nz (Michael Langton) Subject: Re: Calculator Date: 1997/09/24 Message-ID: <3428cb14.3343505@news.manawatu.gen.nz> X-Deja-AN: 275088012 References: <01bcc51b$3c593780$a65ac6c2@anders> <3422EC5A.5B3E@club-internet.fr> Cache-Post-Path: news.manawatu.gen.nz!unknown@ppp147-128.manawatu.gen.nz Organization: PlaNet (NZ) Manawatu, +64 6 357-9245, +64 6 367-9944 Newsgroups: comp.lang.pascal.borland,comp.lang.pascal.misc,ctdl.lang.pascal,maus.lang.tpascal,sk.comp.pascal