Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Calculator Posted by killhha on 5 Mar 2009 at 4:57 AM
so i have this project for school to create a pascal program. i was thinking of making a calculator which can do stuff like cosine and all that stuff. like a scientific calculator. The program has to use crt because thats all weve learnt so far. im not an expert at pascal but i still understand some stuff like loops and menus and such. would the only way to make this be a menu saying stuff like 1) Plus, 2) Minus and so on? or is it possible to just enter in a whole line like 1+4 and then you get the answer? all i want is the basic layout of the program to start off with and then i can build it up.
Report
Re: Calculator Posted by Atex on 6 Mar 2009 at 12:03 AM
This is a simple calculator ( addition supported only ):
function calculate(s:string):string;
 var sl:byte absolute s; {lenght of s}
     a1,a2:real;
     i:byte;
 function to_val(si:string):real;
  var r:real;
      c:integer;
  begin
   val(si,r,c);
   to_val:=r;
  end;
 begin
  if sl>2 then begin  {must at least 3 char long }
   i:=pos('+',s);     {only addition is supported}
   if i=0 then s:='Syntax error' else begin
    a1:=to_val(copy(s,1,i-1));
    a2:=to_val(copy(s,i+1,sl-i));
    str(a1+a2:6:4,s);
   end;
  end;
  calculate:=s;
 end;


begin
 writeln(calculate('145+345'));
end.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.