分類:[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割りでのエラー回避方法
この二点がわからず困っています
わかる方いましたら、教えていただきたいです