C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 133 を表示中)
■78597 / )  C#で電卓
□投稿者/ K (1回)-(2016/01/27(Wed) 15:52:28)

分類:[C#] 

2016/01/27(Wed) 15:55:52 編集(投稿者)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        bool isFirst = true;
        decimal x=0;

        private void buttonNumber_Click(object sender, EventArgs e)
        {
            if (key != Operator.undefined && isFirst)
            {
                x = Convert.ToDecimal(textBox1.Text);
                textBox1.Text = "";

                isFirst = false;
            }

            String text = textBox1.Text + ((Button)sender).Text;

             decimal d = Convert.ToDecimal(text);

             String text2 = d.ToString();

             textBox1.Text = text2;
              
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = String.Empty;
        }


        private void buttonAllClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
        }

        private void buttonDot_Click(object sender, EventArgs e)      
     {
            if (textBox1.Text.IndexOf(".") >= 0)
            {
                return;
            }
            textBox1.Text = textBox1.Text + ".";
        }

       
        enum Operator
        {
            undefined, add, sub, mul, dvi
        }

        Operator key = Operator.undefined;


        private void Calculate()
        {
            decimal y = Convert.ToDecimal(textBox1.Text);
            decimal results = 0;
            switch (key)
            {
                case Operator.add:
                    results = x + y;
                    break;

                case Operator.sub:
                    results = x - y;
                    break;

                case Operator.mul:
                    results = x * y;
                    break;

                case Operator.dvi:
                    results = x / y;
                    break;

            }

            textBox1.Text = results.ToString();

            x = 0;
        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            key = Operator.add;
            isFirst = true;

            Calculate();
        }

        private void buttonSub_Click(object sender, EventArgs e)
        {
            key = Operator.sub;
            isFirst = true;

            Calculate();
        }

        private void buttonMul_Click(object sender, EventArgs e)
        {
            key = Operator.mul;
            isFirst = true;

            
        }

        private void buttonDvi_Click(object sender, EventArgs e)
        {
            key= Operator.dvi;
            isFirst = true;

            
        }


        private void buttonResult_Click(object sender, EventArgs e)
        {
            Calculate();

            
        }
            

       
    }
}


このようなコードなんですが
問題がいくつかありまして、皆さんの知恵をお借りしたいです

1.+以外の演算子を使って=を押さずに連続計算をしたい
2.0割りでのエラー回避方法

この二点がわからず困っています
わかる方いましたら、教えていただきたいです

返信 編集キー/


管理者用

- Child Tree -