C# は簡単な計算機を実装します
環境:VS2010以降
- Windowsフォームアプリケーションを作成する
- ツールボックスに 2 つの TextBox をドラッグし、1 つ目を上に、2 つ目を下に配置します。ここでの主な名前は、上が textBox1、下が textBox2 です。これには、背後にコードを記述することが含まれます
- ツールバー内のボタンをドラッグして配置します。上記の位置合わせツールを使用して、設計を支援します。
- プロパティで各ボタンのテキストを次のように変更します。1
~ 9、小数点、±*/テキストは 1 文字のみであり、それ以上入力しないでください。←
- 任意のボタンを選択して右クリックし、コードの表示を選択して、Form1.cs に移動します。
-
コードを書き始める
-
AddNumはTextBoxのTextを変更し、1~9と小数点のClickイベントに適用します。
-
Reset は、temp、myoperator、および 2 つの TextBox の Text をリセットします。
-
削除は、textBox2 の Text の最後の文字を削除します。
-
Calculate は textBox2 の Text を double から temp に変換し、myoperator を変更します。
-
等しい特定の計算
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//----上面是自动生成的代码,下面得我们手写----
private double temp = 0; //存储临时数据
private char myoperator = ' '; //判断之前按的是+-*/中的哪个
private void AddNum(object sender, EventArgs e)
{ // 1~9与小数点的Click事件
//sender是引发该事件的控件,这里我们拆箱为Button
Button button = (Button)sender;
textBox2.Text += button.Text;
}
private void Reset(object sender, EventArgs e)
{ // CE的Click事件
temp = 0;
myoperator = ' ';
textBox1.Text = textBox2.Text = "";
}
private void Delete(object sender, EventArgs e)
{ // ←的Click事件
//移除最后一个字符
if (textBox2.TextLength > 0)
textBox2.Text = textBox2.Text.Remove(textBox2.TextLength - 1);
}
private void Calculate(object sender, EventArgs e)
{ // +-*/的Click事件
Button button = (Button)sender;
if (double.TryParse(textBox2.Text, out temp)) //尝试把textBox2的Text转为double并赋值给temp
{
myoperator = button.Text[0]; //Text是string,取第一个字符
textBox1.Text = temp.ToString() + ' ' + myoperator;
textBox2.Text = "";
}
else
{ //转换失败,重置所有
Reset(sender, e);
}
}
private void Equal(object sender, EventArgs e)
{ // = 的Click事件,计算并显示
double temp2;
//尝试转换,失败则重置并返回
if (!double.TryParse(textBox2.Text, out temp2)) { Reset(sender, e); return; }
switch (myoperator)
{
case '+':
temp += temp2;
break;
case '-':
temp -= temp2;
break;
case '*':
temp *= temp2;
break;
case '/':
temp /= temp2;
break;
default:
break;
}
textBox1.Text = "";
textBox2.Text = temp.ToString();
}
}
}
- 各ボタンのクリックイベントを設定する
- AddNum: 1~9と小数点のクリックイベント
- リセット:CEのクリックイベント
- 削除: ←のクリックイベント
- 計算:±*/クリックイベント
- 等しい := クリックイベント
- スタート(F5)