Algorithms and Programming
eXercises #6
Multiple Branching
using CASE - Solutions
1.
Procedure TForm1.throwdiceclick(sender : TObject);
{outputs the result of a die}
Var throw : byte;
begin
throw := random(6)+1;
case throw of
1 : showmessage('[ . ]');
2 : showmessage('[ : ]');
3 : showmessage('[: .]');
4 : showmessage('[: :]');
5 : showmessage('[:.:]')
else showmessage('[:::]')
end{case throw}
end;
2.
Procedure TForm1.classifyclick (sender : TObject);
{accepts a character from the keyboard and outputs a classification
based on what sort of character it is}
Var thing : char;
temp : string;
Begin
temp := inputbox('Char Entry','Enter a Character','');
thing := temp[1];
case thing of
'A'..'Z' : showmessage('Uppercase Letter');
'a'..'z' : showmessage('Lowercase Letter');
'0'..'9' : showmessage('Digit')
else case ord(thing) of
0..31,127 : showmessage('Control Character');
128..255 : showmessage('Extended ASCII')
else showmessage('Punctuation mark')
end{case ord thing}
end{case thing}
End.
3.
Procedure TForm1.raterclick(sender:TObject);
{accepts alpha and beta percentages, then outputs rating}
var alphapercent, betapercent : real;
alpha, beta : byte;
begin
betapercent:= strtofloat(inputbox('Beta','Please Enter your Beta Percent','0');
beta := trunc(betapercent + .5)
betapercent:= strtofloat(inputbox('Alpha','Please Enter your Alpha Percent','0');
alpha := trunc(alphapercent + .5);
case beta of
100..80 : case alpha of
100..70 : showmessage('VHA');
69..55 : showmessage('HA');
54..40 : showmessage('SA');
39..25 : showmessage('LA')
else showmessage('VLA')
end;{case alpha}
79..65 : case alpha of
100..55 : showmessage('HA');
54..40 : showmessage('SA');
39..25 : showmessage('LA')
else showmessage('VLA')
end;{case alpha}
64..45 : case alpha of
100..40 : showmessage('SA');
39..25 : showmessage('LA')
else showmessage('VLA')
end;{case alpha}
44..25 : case alpha of
100..25 : showmessage('LA')
else showmessage('VLA')
end;{case alpha}
else showmessage('VLA')
end;{case beta }
end;
4.
Procedure TForm1.taxclick (sender:TObject);
{prompts for income then calculates tax payable}
var income, base, rate, taxpayable : real;
bracket : integer; {the number of thousands in income}
begin
income:= strtofloat(inputbox(nINcome','Please Enter your Taxible Income','0');
bracket := trunc(income/1000);
case bracket of
0..3 : begin
base := 0.0;
rate := 0.0
end;
4..9 : begin
base := -760.0;
rate := 0.19
end;
10..19 : begin
base := -2140.0;
rate := 0.33
end;
20..29 : begin
base := -4960.0;
rate := 0.47
end;
30..39 : begin
base := -7660.0;
rate := 0.56
end
else begin
base := -12060.0;
rate := 0.67
end;
end {case bracket};
taxpayable := base + (rate * income)
showmessage('Tax payable = $' + floattostr(taxpayable))
end;
|