はじめまして。
課題で、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();
}
}
}
よろしくお願いします!
C#で電卓がつくりたい
Re: C#で電卓がつくりたい
Windows10,VS2017Community,C# でテストしています。
[Form1.cs]
はエラーがないようなので
Form1.Designer.cs を提示して頂ければ作って見ましょう。
[Program.cs]は不要
[/size]
[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());
}
}
}
Re: C#で電卓がつくりたい
オフトピック
- Dixq (管理人)
- 管理人
- 記事: 1662
- 登録日時: 14年前
- 住所: 北海道札幌市
- 連絡を取る: