C# exercises (5) Calculator

Contents

my電卓を作ろう

逆ポーランド記法電卓

Reverse Polish Notation Calculator

画面をデザインする

スクリーンショット 2016-05-02 16.20.21

ツールボックスの中で、

  • コモンコントロール「TextBox」
    • TextBox –>  (name): typeText;  text: 0
    • TextBox –> (name): lastText;  text: 0
  • コモンコントロール「Button」
    • Button1 –> (name): plusButton;  text: +
    • Button2 –> (name): subButton; text: –
    • Button3 –> (name): multiButton; text: x
    • Button4 –> (name): divButton; text: /
    • Button5 –> (name): clrButton; text: CL
    • Button6 –> (name): enterButton; text: ER

Formにドラッグ&ドロップする

プログラミング

KeyPressイベントを作成

Form1を選択し、Form1のイベントの表示に切り替えて、「KeyPress」イベントに、FormKeyPressを入力

スクリーンショット 2016-05-02 16.23.53

FormKeyPressメソッドを作成

六つボタンを選択した状態で、コントロール類の、「KeyPress」イベントに、FormKeyPressimageを選択。

スクリーンショット 2016-05-02 16.28.36

private void FormKeyPress(object sender, KeyPressEventArgs e)
{
    string key = e.KeyChar.ToString();
    int n = 0;
    try
    {
        n = int.Parse(key);
    }
    catch
    {
        return;
    }
    string num = typeText.Text + n;
    try
    {
        typeText.Text = "" + int.Parse(num);
    }
    catch
    {
        return;
    }
}

plusButtonClickメソッドを作成

スクリーンショット 2016-05-02 16.41.55

private void plusButtonClick(object sender, EventArgs e)
{
    try
    {
        int lastnum = int.Parse(lastText.Text);
        int typenum = int.Parse(typeText.Text);
        lastText.Text = "" + (lastnum + typenum);
        typeText.Text = "0";
    }
    catch
    {
        return;
    }
}

subButtonClickメソッドを作成

スクリーンショット 2016-05-02 16.42.20

private void subButtonClick(object sender, EventArgs e)
{
   try
   {
       int lastnum = int.Parse(lastText.Text);
       int typenum = int.Parse(typeText.Text);
       lastText.Text = "" + (lastnum - typenum);
       typeText.Text = "0";
   }
   catch
   {
       return;
   }
}

multiButtonClickメソッドを作成

スクリーンショット 2016-05-02 16.43.30

private void multiButtonClick(object sender, EventArgs e)
{
    try
    {
        int lastnum = int.Parse(lastText.Text);
        int typenum = int.Parse(typeText.Text);
        lastText.Text = "" + (lastnum * typenum);
        typeText.Text = "0";
    }
    catch
    {
        return;
    }
}

divButtonClickメソッドを作成

スクリーンショット 2016-05-02 16.44.14

private void divButtonClick(object sender, EventArgs e)
{
    try
    {
        int lastnum = int.Parse(lastText.Text);
        int typenum = int.Parse(typeText.Text);
        lastText.Text = "" + (lastnum / typenum);
        typeText.Text = "0";
    }
    catch
    {
        return;
    }
}

clrButtonClickメソッドを作成

スクリーンショット 2016-05-02 16.45.07

private void clrButtonClick(object sender, EventArgs e)
{
    if (typeText.Text == "0")
    {
        lastText.Text = "0";
    }
    typeText.Text = "0";
}

enterButtonClickメソッドを作成

スクリーンショット 2016-05-02 16.47.49

private void enterButtonClick(object sender, EventArgs e)
{
    string s = typeText.Text;
    lastText.Text = s;
    typeText.Text = "0";
}

作成したコード

 

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 Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void FormKeyPress(object sender, KeyPressEventArgs e)
        {
            string key = e.KeyChar.ToString();
            int n = 0;
            try
            {
                n = int.Parse(key);
            }
            catch
            {
                return;
            }

            string num = typeText.Text + n;
            try
            {
                typeText.Text = "" + int.Parse(num);
            }
            catch
            {
                return;
            }

        }

        private void plusButtonClick(object sender, EventArgs e)
        {
            try
            {
                int lastnum = int.Parse(lastText.Text);
                int typenum = int.Parse(typeText.Text);
                lastText.Text = "" + (lastnum + typenum);
                typeText.Text = "0";
            }
            catch
            {
                return;
            }
        }

        private void subButtonClick(object sender, EventArgs e)
        {
            try
            {
                int lastnum = int.Parse(lastText.Text);
                int typenum = int.Parse(typeText.Text);
                lastText.Text = "" + (lastnum - typenum);
                typeText.Text = "0";
            }
            catch
            {
                return;
            }

        }

        private void multiButtonClick(object sender, EventArgs e)
        {
            try
            {
                int lastnum = int.Parse(lastText.Text);
                int typenum = int.Parse(typeText.Text);
                lastText.Text = "" + (lastnum * typenum);
                typeText.Text = "0";
            }
            catch
            {
                return;
            }

        }

        private void divButtonClick(object sender, EventArgs e)
        {
            try
            {
                int lastnum = int.Parse(lastText.Text);
                int typenum = int.Parse(typeText.Text);
                lastText.Text = "" + (lastnum / typenum);
                typeText.Text = "0";
            }
            catch
            {
                return;
            }

        }

        private void clrButtonClick(object sender, EventArgs e)
        {
            if (typeText.Text == "0")
            {
                lastText.Text = "0";
            }
            typeText.Text = "0";
        }

        private void enterButtonClick(object sender, EventArgs e)
        {
            string s = typeText.Text;
            lastText.Text = s;
            typeText.Text = "0";
        }
    }
}

My電卓使い方

計算例: 123+456=

まずキーボードから数字123入力

スクリーンショット 2016-05-02 17.01.50

「ER」キーで数字を設定

スクリーンショット 2016-05-02 17.01.53

次の数字456を入力

スクリーンショット 2016-05-02 17.02.00

演算キーで計算

スクリーンショット 2016-05-02 17.02.11