第18講 電卓の高度化
第1話 いろいろな関数を付け加える

講義の進展とともに、電卓は高度化していくと前に予告しておきました。
第18講で第1の高度化を図りましょう。

今回は、上の10個のボタンを加えましょう。
そして、それぞれのボタンをダブルクリックして、次のようにコーティングして下さい。
private: System::Void button18_Click(System::Object^ sender, System::EventArgs^ e) {
        z=sqrt(z);
        textBox1->Text=z.ToString();
     }
private: System::Void button19_Click(System::Object^ sender, System::EventArgs^ e) {
        z=sin(z);
        textBox1->Text=z.ToString();
     }
private: System::Void button20_Click(System::Object^ sender, System::EventArgs^ e) {
        z=cos(z);
        textBox1->Text=z.ToString();
     }
private: System::Void button21_Click(System::Object^ sender, System::EventArgs^ e) {
        z=tan(z);
        textBox1->Text=z.ToString();
     }
private: System::Void button22_Click(System::Object^ sender, System::EventArgs^ e) {
        z=log(z);
        textBox1->Text=z.ToString();
     }
private: System::Void button23_Click(System::Object^ sender, System::EventArgs^ e) {
        z=z*z;
        textBox1->Text=z.ToString();
     }
private: System::Void button24_Click(System::Object^ sender, System::EventArgs^ e) {
        z=z*z*z;
        textBox1->Text=z.ToString();
     }
private: System::Void button25_Click(System::Object^ sender, System::EventArgs^ e) {
        z=z*z*z*z;
        textBox1->Text=z.ToString();
     }
private: System::Void button26_Click(System::Object^ sender, System::EventArgs^ e) {
        z=z*z*z*z*z;
        textBox1->Text=z.ToString();
      }
private: System::Void button27_Click(System::Object^ sender, System::EventArgs^ e) {
        z=z*z*z*z*z*z;
        textBox1->Text=z.ToString();
      }
private: System::Void button28_Click(System::Object^ sender, System::EventArgs^ e) {
       zz+=int::Parse(textBox1->Text);
       z=0;
     }
private: System::Void button29_Click(System::Object^ sender, System::EventArgs^ e) {
       zz-=int::Parse(textBox1->Text);
       z=0;
     }
private: System::Void button30_Click(System::Object^ sender, System::EventArgs^ e) {
        textBox1->Text=zz.ToString();
        z=zz;
     }
private: System::Void button31_Click(System::Object^ sender, System::EventArgs^ e) {
        zz=0;
     }
private: System::Void button32_Click(System::Object^ sender, System::EventArgs^ e) {
        z=0;
        zz=0;
        cn=0;
        y2=2;
        textBox1->Text=L"";
     }
ただし、数学の関数を使いますので、冒頭で<math.h>をインクルードしておく必要があります。
#pragma once
#include<math.h>
int yy1;
int y2=0;
int cn=0;
double z=0;
double w;
namespace 簡易電卓 {


(コピーペースト用
#pragma once
#include<math.h>
int yy1;
int y2=0;
int cn=0;
double z=0;
double w;
namespace 簡易電卓 {
     ・
     ・
     ・
private: System::Void button18_Click(System::Object^ sender, System::EventArgs^ e) {
z=sqrt(z);
textBox1->Text=z.ToString();
}
private: System::Void button19_Click(System::Object^ sender, System::EventArgs^ e) {
z=sin(z);
textBox1->Text=z.ToString();
}
private: System::Void button20_Click(System::Object^ sender, System::EventArgs^ e) {
z=cos(z);
textBox1->Text=z.ToString();
}
private: System::Void button21_Click(System::Object^ sender, System::EventArgs^ e) {
z=tan(z);
textBox1->Text=z.ToString();
}
private: System::Void button22_Click(System::Object^ sender, System::EventArgs^ e) {
z=log(z);
textBox1->Text=z.ToString();
}
private: System::Void button23_Click(System::Object^ sender, System::EventArgs^ e) {
z=z*z;
textBox1->Text=z.ToString();
}
private: System::Void button24_Click(System::Object^ sender, System::EventArgs^ e) {
z=z*z*z;
textBox1->Text=z.ToString();
}
private: System::Void button25_Click(System::Object^ sender, System::EventArgs^ e) {
z=z*z*z*z;
textBox1->Text=z.ToString();
}
private: System::Void button26_Click(System::Object^ sender, System::EventArgs^ e) {
z=z*z*z*z*z;
textBox1->Text=z.ToString();
}
private: System::Void button27_Click(System::Object^ sender, System::EventArgs^ e) {
z=z*z*z*z*z*z;
textBox1->Text=z.ToString();
}
private: System::Void button28_Click(System::Object^ sender, System::EventArgs^ e) {
zz+=int::Parse(textBox1->Text);
z=0;
}
private: System::Void button29_Click(System::Object^ sender, System::EventArgs^ e) {
zz-=int::Parse(textBox1->Text);
z=0;
}
private: System::Void button30_Click(System::Object^ sender, System::EventArgs^ e) {
textBox1->Text=zz.ToString();
z=zz;
}
private: System::Void button31_Click(System::Object^ sender, System::EventArgs^ e) {
zz=0;
}

private: System::Void button32_Click(System::Object^ sender, System::EventArgs^ e) {
z=0;
zz=0;
cn=0;
y2=2;
textBox1->Text=L"";
}





int y1;
となっている方は、変数をyy1などと変更してしてください。
<math.h>をインクルードすると、この変数目だとエラーします。
理由は私にもよく分かりませんが、どうやら<math.h>の中でy1が関数として利用されているようです。

このようにコーティングした後、
ビルドして、2→3乗の順にボタンをクリックすると
と正しく結果が表示されます。
では、皆さん私はルートやsinなどを選らびましたが、皆さんはご自由にボタンを足してコーティングしてください。

尚、sqrt(z)はインクルードファイルmath.hに用意されている関数で、平方根を求める式です。
sqrt(9)ならsqrt(9)=3です。
その他は、数学の関数名と同じなのでお分かりですね。
5乗なども5回かければよいわけですから、z=z*z*z*z*zですね。
皆さんにはもう申し上げる必要はないと思いますが、
z=z*z*z*z*zはz←z*z*z*z*zの意味でしたね。


さて、では皆さんに問題です。

電卓には、普通メモリーという機能がついています。
私たちの関数電卓にもこのメモリー機能を付けたいと思います。
コーティングをどのようにしたらよいか考えてください。


第17講第8話へ 第18講第2話へ


VC++講義第1部へ
vb講義へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座
初心者のための世界で一番わかりやすいVisual Basic入門基礎講座