電卓を作る
その4 プログラムの改良
前回作成したプログラムソースを少し改良してみます。改良するところは,
(1) 「=」キーを押して表示された数を使ってさらに計算を進められるようにする。
(2) 「=」キーを押す代わりに「+」や「ー」などの数学記号を押したときに計算を行い,さらに計算を進められる
ようにする。
の2つです。
(1)については,stが6のときに,記号キーが押されたら表示されている答えをvalue1に代入して,stを3にすることで解決します。
(2)についてはstが4または5のときに,記号キーが押されたら「=」キーと同じように計算を行い,その答えをvalue1に代入し,stを3にすることで解決します。
今回変更したところは,太字で表しています。
procedure TForm1.syori(ch: char);
begin
if ch='A' then begin
//オールクリアキーが押された場合
st:=0;
label1.Caption:='01';
exit;
end;
case st mod 10 of
0:begin
//数字を入力する前の場合
if ch='p' then exit;
//数学記号(+,ーなど)が押されたときは終了
if ch='=' then exit;
//=が押されたときは終了
if ch='C' then exit;
//クリアキーが押されたときは終了
if ch='.' then begin
//小数点が押されたとき
value1:='0.';
//value1の文字列を'0.'にする
label1.Caption:=value1+'1';
//表示するときには最後に1を付ける
st:=2;
//stの値を2にする
exit;
//終了
end;
value1:=ch;
//上記以外の数字キーが押されたときはvalue1に数字を代入する
label1.Caption:=value1+'1';
st:=1;
//stの値を1にする
end;
1,2:begin
//数字や小数点が押されている場合
if ch='p' then begin
//数学記号が押されたとき
st:=3;
//stの値を3にする
exit;
end;
if ch='=' then exit;
//=が押されたときは終了
if ch='C' then begin
//クリアキーが押されたとき
label1.Caption:='01';
//表示を'01'にする
st:=0;
//stを0にする
exit;
end;
if ch='.' then begin
//小数点が押されたとき
if st=2 then exit;
//もしstが2なら終了
value1:=value1+'.';
//value1に小数点を付ける
label1.Caption:=value1+'1';
st:=2;
//stの値を2にする
exit;
end;
//数字キーが押されたとき
value1:=value1+ch;
//value1に数字を付け足す
label1.Caption:=value1+'1';
if st=0 then st:=1;
end;
3:begin
//数学記号が押されている場合
if ch='p' then exit;
//数学記号が押されたときは終了
if ch='=' then exit;
//=が押されてたときは終了
if ch='C' then begin
//クリアキーが押された場合
st:=0;
//stの値を0にする
label1.Caption:='01';
exit;
end;
if ch='.' then begin/
/小数点が押された場合
value2:='0.';
label1.Caption:=value2+'1';
st:=5;
//stの値を5にする
exit;
end;
//数字キーが押されたとき
value2:=ch;
//value2に数字を代入する
label1.Caption:=value2+'1';
if st =3 then st:=4;
//もしstが3ならstを4にする
end;
4,5:begin
//数学記号が押された後,数字や小数点が押されている場合
if ch='p' then exit; //数学記号が押されたとき
case label1.Tag of
1:begin//value1に答えを代入する
value1:=floattostr(strtofloat(value1)+strtofloat(value2));
end;
2:begin
value1:=floattostr(strtofloat(value1)-strtofloat(value2));
end;
3:begin
value1:=floattostr(strtofloat(value1)*strtofloat(value2));
end;
4:begin
value1:=floattostr(strtofloat(value1)/strtofloat(value2));
end;
end;
label1.Caption:=value1+'1';
st:=3; //stを3にする
exit;
end;
if ch='=' then begin
//=が押された場合
case label1.Tag of
//ここで計算の答えをvalue1に代入する
1:begin
value1:=floattostr(strtofloat(value1)+strtofloat(value2));
end;
2:begin
value1:=floattostr(strtofloat(value1)-strtofloat(value2));
end;
3:begin
value1:=floattostr(strtofloat(value1)*strtofloat(value2));
end;
4:begin
value1:=floattostr(strtofloat(value1)/strtofloat(value2));
end;
end;
label1.Caption:=value1+'1'; //label1にはvalue1に1をつけたものを表示
st:=6;
//stを6にする
exit;
end;
if ch='C' then begin;
//クリアキーが押された場合
label1.Caption:='01';
//表示を'01'にする
value2:='';
//value2の文字列を空にする
st:=3;
//stを3にする
end;
if ch='.' then begin
//小数点が押された場合
if st=5 then exit;
//stが5なら終了
value2:='0.';
label1.Caption:=value2+'1';
st:=5;
exit;
end;
//数字キーが押された場合
value2:=value2+ch;
label1.Caption:=value2+'1';
end;
6:begin
//=が押された後の場合
if ch='p' then begin //数学記号が押されたとき
st:=3; //stを3にする
exit;
end;
if ch='=' then exit;
//=が押されたときは終了
if ch='C' then begin
//クリアキーが押された場合
label1.Caption:='01';
st:=0;
exit;
end;
if ch='.' then begin
//小数点が押された場合
value1:='0.';
label1.Caption:=value1+'1';
st:=2;
exit;
end;
//数字キーが押された場合
value1:=ch;
label1.Caption:=value1+'1';
st:=1;
end;
end;
end;
サンプルプログラムを付けておきますので,興味のある人はご覧ください。