C#で電卓がつくりたい

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
master

C#で電卓がつくりたい

#1

投稿記事 by master » 8年前

はじめまして。
課題で、Visual Studioで電卓を作らなければなりません。
「A + B」のような2つの計算まではできるのですが、
「A + B + C」のような3つ以上の計算ができずにいます。
どなたか、助けてください!

※数値のボタンを「btnNumber」で一纏めに、
 液晶にあたる部分をTextBoxで作り「txtDisplay」と名づけてあります。

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

bool isFirst = true;
decimal 値1 = 0;

private void btnNumber_Click(object sender, EventArgs e)
{
if (演算 != 四則演算の記号.未定義 && isFirst)
{
値1 = Convert.ToDecimal(txtDisplay.Text);
txtDisplay.Text = "";
isFirst = false;
}

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

decimal d = Convert.ToDecimal(text);

String text2 = d.ToString();

txtDisplay.Text = text2;
}

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

enum 四則演算の記号
{
未定義, ADD, SUB, MUL, DIV
};

四則演算の記号 演算 = 四則演算の記号.未定義;

private void btnAdd_Click(object sender, EventArgs e)
{
演算 = 四則演算の記号.ADD;
isFirst = true;
}

private void btnSub_Click(object sender, EventArgs e)
{
演算 = 四則演算の記号.SUB;
isFirst = true;
}

private void btnMul_Click(object sender, EventArgs e)
{
演算 = 四則演算の記号.MUL;
isFirst = true;
}

private void btnDiv_Click(object sender, EventArgs e)
{
演算 = 四則演算の記号.DIV;
isFirst = true;
}

private void btnEqu_Click(object sender, EventArgs e)
{
decimal 値2 = Convert.ToDecimal(txtDisplay.Text);
decimal 結果 = 0;

switch (演算)
{

case 四則演算の記号.ADD:
結果 = 値1 + 値2;
break;
case 四則演算の記号.SUB:
結果 = 値1 - 値2;
break;
case 四則演算の記号.MUL:
結果 = 値1 * 値2;
break;
case 四則演算の記号.DIV:
結果 = 値1 / 値2;
break;
}

txtDisplay.Text = 結果.ToString();

}

}
}

よろしくお願いします!

Math

Re: C#で電卓がつくりたい

#2

投稿記事 by Math » 8年前

Windows10,VS2017Community,C# でテストしています。
[Form1.cs]

コード:

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

        bool isFirst = true;
        decimal 値1 = 0;

        private void btnNumber_Click(object sender, EventArgs e)
        {
            if (演算 != 四則演算の記号.未定義 && isFirst)
            {
                値1 = Convert.ToDecimal(txtDisplay.Text);
                txtDisplay.Text = "";
                isFirst = false;
            }

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

            decimal d = Convert.ToDecimal(text);

            String text2 = d.ToString();

            txtDisplay.Text = text2;
        }

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

        enum 四則演算の記号
        {
            未定義, ADD, SUB, MUL, DIV
        };

        四則演算の記号 演算 = 四則演算の記号.未定義;

        private void btnAdd_Click(object sender, EventArgs e)
        {
            演算 = 四則演算の記号.ADD;
            isFirst = true;
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            演算 = 四則演算の記号.SUB;
            isFirst = true;
        }

        private void btnMul_Click(object sender, EventArgs e)
        {
            演算 = 四則演算の記号.MUL;
            isFirst = true;
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            演算 = 四則演算の記号.DIV;
            isFirst = true;
        }

        private void btnEqu_Click(object sender, EventArgs e)
        {
            decimal 値2 = Convert.ToDecimal(txtDisplay.Text);
            decimal 結果 = 0;

            switch (演算)
            {

                case 四則演算の記号.ADD:
                    結果 = 値1 + 値2;
                    break;
                case 四則演算の記号.SUB:
                    結果 = 値1 - 値2;
                    break;
                case 四則演算の記号.MUL:
                    結果 = 値1 * 値2;
                    break;
                case 四則演算の記号.DIV:
                    結果 = 値1 / 値2;
                    break;
            }

            txtDisplay.Text = 結果.ToString();

        }
    }
}
はエラーがないようなので
Form1.Designer.cs を提示して頂ければ作って見ましょう。

[Program.cs]は不要

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cal
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
[/size]

アバター
usao
記事: 1889
登録日時: 12年前
連絡を取る:

Re: C#で電卓がつくりたい

#3

投稿記事 by usao » 8年前

オフトピック
これの完全なコピーかと.
http://dixq.net/forum/viewtopic.php?f=3&t=19330

最近,これ系の行為を延々と繰り返している人がいるっぽいですね.

アバター
Dixq (管理人)
管理人
記事: 1662
登録日時: 14年前
住所: 北海道札幌市
連絡を取る:

Re: C#で電卓がつくりたい

#4

投稿記事 by Dixq (管理人) » 8年前

フォーラムルールにのっとって報告します。

http://dixq.net/forum/viewtopic.php?f=3&t=19375
の投稿者と全く同じアクセス元からの投稿ですね。

返信

“C言語何でも質問掲示板” へ戻る