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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public string str = ""; //把數字鍵字串存起來
public double ans = 0;//佔存變數
public int how = 0;//判斷按下+ - * /
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button16_Click(object sender, EventArgs e)
{
str = ""; //顯示0
textBox1.Text = "0";
}
private void button1_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }//數字鍵1
str += "1";
textBox1.Text = str;//顯示字串內容
}
private void button2_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }//數字鍵2
str += "2";
textBox1.Text = str;
}
private void button3_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }//數字鍵3
str += "3";
textBox1.Text = str;
}
private void button4_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "4";
textBox1.Text = str;
}
private void button5_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "5";
textBox1.Text = str;
}
private void button6_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "6";
textBox1.Text = str;
}
private void button7_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "7";
textBox1.Text = str;
}
private void button8_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "8";
textBox1.Text = str;
}
private void button9_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "9";
textBox1.Text = str;
}
private void button15_Click(object sender, EventArgs e)
{
if (str != "")//點擊=鍵
{
if (how == 1) { ans += Convert.ToDouble(str); }//轉換成浮點數
else if (how == 2) { ans -= Convert.ToDouble(str); }
else if (how == 3) { ans *= Convert.ToDouble(str); }
else if (how == 4) { ans /= Convert.ToDouble(str); }
else if (ans == 0) { ans = Convert.ToDouble(str); }
else ans = Convert.ToDouble(str);
}
how = 0;//再儲存使用者所按下的運算元
str = Convert.ToString(ans);
textBox1.Text = str;
label1.Text = "=";
}
private void button11_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };ans += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };ans -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };ans *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };ans /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } ans = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(ans);
how = 1;
str = "";
}
private void button12_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };ans += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };ans -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };ans *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };ans /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } ans = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(ans);
how = 2;
str = "";
}
private void button13_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };ans += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };ans -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };ans *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };ans /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } ans = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(ans);
how = 3;
str = "";
}
private void button14_Click(object sender, EventArgs e)
{
if (how == 1)
{ if (str == "") { str = "0"; };ans += Convert.ToDouble(str); }
else if (how == 2)
{ if (str == "") { str = "0"; };ans -= Convert.ToDouble(str); }
else if (how == 3)
{ if (str == "") { str = "1"; };ans *= Convert.ToDouble(str); }
else if (how == 4)
{ if (str == "") { str = "1"; };ans /= Convert.ToDouble(str); }
else if (how == 0)
{ if (str == "") { str = "0"; } ans = Convert.ToDouble(str); }
textBox1.Text = Convert.ToString(ans);
how = 4;
str = "";
}
private void button10_Click(object sender, EventArgs e)
{
if (str == "0") { str = ""; }
str += "0";
}
}
}
沒有留言:
張貼留言