Calculator - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Calculator

killhhakillhha Posts: 9Member
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.

Comments

  • AtexAtex Posts: 268Member
    This is a simple calculator ( addition supported only ):[code][color=Blue]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.[/color][/code]
Sign In or Register to comment.