小算盤(按鈕要自己拉)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
double num1 = 0.0, num2 = 0.0; // 運算元
string op; // 運算子
bool reinput = true;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
inputNumber(this.button1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
inputNumber(this.button2.Text);
}
private void button3_Click(object sender, EventArgs e)
{
inputNumber(this.button3.Text);
}
private void button4_Click(object sender, EventArgs e)
{
inputNumber(this.button4.Text);
}
private void button5_Click(object sender, EventArgs e)
{
inputNumber(this.button5.Text);
}
private void button6_Click(object sender, EventArgs e)
{
inputNumber(this.button6.Text);
}
private void button7_Click(object sender, EventArgs e)
{
inputNumber(this.button7.Text);
}
private void button8_Click(object sender, EventArgs e)
{
inputNumber(this.button8.Text);
}
private void button9_Click(object sender, EventArgs e)
{
inputNumber(this.button9.Text);
}
private void button10_Click(object sender, EventArgs e)
{
inputNumber(this.button10.Text);
}
private void button11_Click(object sender, EventArgs e)
{
inputOP(this.button11.Text);
}
private void button12_Click(object sender, EventArgs e)
{
inputOP(this.button12.Text);
}
private void button13_Click(object sender, EventArgs e)
{
inputOP(this.button13.Text);
}
private void button14_Click(object sender, EventArgs e)
{
inputOP(this.button14.Text);
}
private void button15_Click(object sender, EventArgs e)
{
inputOP(this.button15.Text);
}
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "0";
reinput = true;
}
private void button17_Click(object sender, EventArgs e)
{
num2 = Convert.ToDouble(this.textBox1.Text);
if (op == "+")
this.textBox1.Text = Convert.ToString(num1 + num2);
else if (op == "-")
this.textBox1.Text = Convert.ToString(num1 - num2);
else if (op == "*")
this.textBox1.Text = Convert.ToString(num1 * num2);
else if (op == "/")
{
if (num2 == 0) this.textBox1.Text = "除數不得為零!";
else this.textBox1.Text = Convert.ToString(num1 / num2);
}
else if (op == "%")
{
if (num2 == 0) this.textBox1.Text = "除數不得為零!";
else this.textBox1.Text = Convert.ToString(num1 % num2);
}
op = "";
reinput = true;
}
private void button18_Click(object sender, EventArgs e)
{
Int64 result;
if (Int64.TryParse(this.textBox1.Text, out result))
inputNumber(this.button18.Text);
}
private void button19_Click(object sender, EventArgs e)
{
this.textBox1.Text = Convert.ToString(0 - Convert.ToDouble(this.textBox1.Text));
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
this.button18.Visible = false;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
this.button18.Visible = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.button19.Visible = !this.button19.Visible;
}
private void inputNumber(string btText)
{
if ((this.textBox1.Text.Length == 1 && this.textBox1.Text == "0") || reinput == true)
{
if (btText != ".")
this.textBox1.Text = btText;
else
this.textBox1.Text = this.textBox1.Text + btText;
reinput = false;
}
else
this.textBox1.Text = this.textBox1.Text + btText;
}
private void inputOP(string btText)
{
op = btText;
num1 = Convert.ToDouble(this.textBox1.Text);
reinput = true;
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
-------------------------------------------------------------------------------------------------------------
OX
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
int OX = 0;
int[,] victory = new int[3, 3];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.button1_Click(sender, e);
}
private void button1_Click(object sender, EventArgs e)
{
OX = 0;
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
victory[x, y] = -3;
foreach (Control ctrl in this.Controls)
{
if (ctrl is PictureBox)
{
PictureBox p = (PictureBox)ctrl;
p.Image = null;
}
}
this.label1.Text = "";
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox1, 0, 0);
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox2, 0, 1);
}
private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox3, 0, 2);
}
private void pictureBox4_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox4, 1, 0);
}
private void pictureBox5_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox5, 1, 1);
}
private void pictureBox6_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox6, 1, 2);
}
private void pictureBox7_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox7, 2, 0);
}
private void pictureBox8_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox8, 2, 1);
}
private void pictureBox9_MouseDown(object sender, MouseEventArgs e)
{
drawOX(this.pictureBox9, 2, 2);
}
private void drawOX(PictureBox p, int x, int y)
{
if (p.Image == null && this.label1.Text == "")
{
OX = ++OX % 2;
victory[x, y] = OX;
if (OX == 1)
p.Image = System.Drawing.Image.FromFile("..\\..\\pic\\O.jpg");
else
p.Image = System.Drawing.Image.FromFile("..\\..\\pic\\X.jpg");
this.label1.Text = check_victory();
}
}
private string check_victory()
{
for (int x = 0; x < 3; x++) // 橫向檢查
{
int sum = 0;
for (int y = 0; y < 3; y++)
sum += victory[x, y];
if (sum == 0) return "X獲勝!";
else if (sum == 3) return "O獲勝!";
}
for (int y = 0; y < 3; y++) // 縱向檢查
{
int sum = 0;
for (int x = 0; x < 3; x++)
sum += victory[x, y];
if (sum == 0) return "X獲勝!";
else if (sum == 3) return "O獲勝!";
}
if (victory[0, 0] + victory[1, 1] + victory[2, 2] == 0) // 對角線檢查
return "X獲勝!";
else if (victory[0, 0] + victory[1, 1] + victory[2, 2] == 3)
return "O獲勝!";
if (victory[0, 2] + victory[1, 1] + victory[2, 0] == 0) // 對角線檢查
return "X獲勝!";
else if (victory[0, 2] + victory[1, 1] + victory[2, 0] == 3)
return "O獲勝!";
return "";
}
}
}
沒有留言:
張貼留言