Does anyone have an idea of how I should make a simple calculator without the case function?
- 2 Contributors
- forum4 Replies
- 182 Views
- 20 Hours Discussion Span
- comment Latest Post by Razi_1
SalmiSoft 102
The CASE statement can be replaced by a series of IF statements.
If your talking something like this, it is too big of a coding.
if gamemode=1 then {If gamemode 1 has been chosen}
begin
clrscr;
title;
writeln ('Welcome to Calculator');
writeln;
writeln ('To begin, press <Enter>');
readln;
clrscr;
title;
writeln ('Choose which symbol is needed');
writeln ('"+" "-" "*" "/"');
readln(symbol);
if symbol='+' then {addition}
begin
clrscr;
title;
writeln ('Addition Problem (One time use only)');
writeln;
writeln ('Enter two numbers(with spaces) to solve out.(No decimal numbers)');
readln (add1,add2);
addresult:=add1+add2;
writeln;
writeln ('The result is ',addresult);
readln;
clrscr;
title;
quit;
end;
if symbol='-' then {subtraction}
begin
clrscr;
title;
writeln ('Subtraction Problem (One time use only)');
writeln;
writeln ('Enter two numbers(with spaces) to solve out.(No decimal numbers)');
readln (sub1,sub2);
subresult:=sub1-sub2;
writeln;
writeln ('The result is ',subresult);
readln;
clrscr;
title;
quit;
end;
if symbol='*' then {multiplication}
begin
clrscr;
title;
writeln ('Multiplication Problem (One time use only)');
writeln;
writeln ('Enter two numbers(space after one number) to solve out.(No decimal numbers or symbols)');
readln (mult1,mult2);
multresult:=mult1*mult2;
writeln;
writeln ('The result is ',multresult);
readln;
clrscr;
title;
quit;
end;
if symbol='/' then {division}
begin
clrscr;
title;
writeln ('Division Problem (One time use only)');
writeln;
writeln ('Enter two numbers(with spaces) to solve out.(No decimal numbers)');
readln (div1,div2);
if div2<=0 then
begin
clrscr;
title;
writeln ('Syntax Error, Denominator can not be less than 0');
quit;
end;
divresult:=div1 div div2;
writeln;
writeln ('The result is ',divresult:1:5);
readln;
clrscr;
title;
quit;
end;
end
SalmiSoft 102
That is one way. (There are of course other ways.)
Why do you say this is too big coding? Too much typing? Too much to understand? Requires too much memory? In reality it is a tiny program.
If you just think it is too much code to type or read, you could trim it down quite easily. For example:
if gamemode=1 then {If gamemode 1 has been chosen}
begin
clrscr;
title;
writeln ('Welcome to Calculator');
writeln;
writeln ('To begin, press <Enter>');
readln;
clrscr;
title;
writeln ('Choose which symbol is needed');
writeln ('"+" "-" "*" "/"');
readln(symbol);
clrscr;
title;
if symbol='+' then {addition}
writeln ('Addition Problem (One time use only)')
else if symbol='-' then {subtraction}
writeln ('Subtraction Problem (One time use only)');
else if symbol='*' then {multiplication}
writeln ('Multiplication Problem (One time use only)');
else if symbol='/' then {division}
writeln ('Division Problem (One time use only)');
writeln;
writeln ('Enter two numbers(with spaces) to solve out.(No decimal numbers)');
readln (Val1,Val2);
if symbol='+' then {addition}
CalcResult := Val1 + Val2
else if symbol='-' then {subtraction}
CalcResult := Val1 - Val2
else if symbol='*' then {multiplication}
CalcResult := Val1 * Val2
else if symbol='/' then {division}
CalcResult := Val1 / Val2;
writeln;
writeln ('The result is ',CalcResult);
readln;
clrscr;
title;
quit;
end.
Well, from what I meant that it was big is that the the whole program which I am writing is alot of coding and this takes up the majority of it.
Because of the fact that I am on Windows XP, it is harder for me to scroll down trying to find my program.
Thanks for that coding, it will help me out.