2015年11月5日 星期四

期中100分

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        double n1, n2;
        string m = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "";
            label2.Text = "0";
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button26_Click(object sender, EventArgs e)
        {
            //0
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button21_Click(object sender, EventArgs e)
        {
            //1
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button22_Click(object sender, EventArgs e)
        {
            //2
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button23_Click(object sender, EventArgs e)
        {
            //3
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            //4
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button17_Click(object sender, EventArgs e)
        {
            //5
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button18_Click(object sender, EventArgs e)
        {
            //6
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            //7
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //8
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            //9
            Button b = sender as Button;
            if (label2.Text == "0")
                label2.Text = b.Text;
            else
                label2.Text += b.Text;
        }

        private void button27_Click(object sender, EventArgs e)
        {
            //.
            Button b = sender as Button;
            label2.Text += b.Text;
        }

        private void button28_Click(object sender, EventArgs e)
        {
            //+
            n1 = Double.Parse(label2.Text);
            label1.Text = label2.Text + "+";
            label2.Text = "";
            m = "+";
        }

        private void button24_Click(object sender, EventArgs e)
        {
            //-
            n1 = Double.Parse(label2.Text);
            label1.Text = label2.Text + "-";
            label2.Text = "";
            m = "-";
        }

        private void button19_Click(object sender, EventArgs e)
        {
            //*
            n1 = Double.Parse(label2.Text);
            label1.Text = label2.Text + "*";
            label2.Text = "";
            m = "*";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            ///
            n1 = Double.Parse(label2.Text);
            label1.Text = label2.Text + "/";
            label2.Text = "";
            m = "/";

        }

        private void button25_Click(object sender, EventArgs e)
        {
            //=
            if (label1.Text == "")
                return;

            double n2 = Double.Parse(label2.Text);
            double c = 0;
            switch (m)
            {
                case "+":
                    c = n1 + n2;
                    break;
                case "-":
                    c = n1 - n2;
                    break;
                case "*":
                    c = n1 * n2;
                    break;
                case "/":
                    c = n1 / n2;
                    break;
            }
            label2.Text = c.ToString();
            label1.Text = "";
        }

        private void button20_Click(object sender, EventArgs e)
        {
            //1/x
            double n = Double.Parse(label2.Text);
            label2.Text = (1 / n).ToString();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //根號
            double n = Double.Parse(label2.Text);
            label2.Text = (System.Math.Sqrt(n)).ToString();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //c
            label1.Text = "";
            label2.Text = "0";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //ce
            label1.Text = "";
            label2.Text = "0";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            label2.Text = label2.Text.Remove(label2.Text.Length - 1);
            if (label2.Text.Length == 0)
                label2.Text = "0";
        }
    }
}

期中


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

        private void button1_Click(object sender, EventArgs e)
        {
            double n1 = Double.Parse(textBox1.Text);
            double n2 = Double.Parse(textBox2.Text);
            label1.Text = "= " + (n1 + n2).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double n1 = Double.Parse(textBox1.Text);
            double n2 = Double.Parse(textBox2.Text);
            label1.Text = "= " + (n1 - n2).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double n1 = Double.Parse(textBox1.Text);
            double n2 = Double.Parse(textBox2.Text);
            label1.Text = "= " + (n1 * n2).ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            double n1 = Double.Parse(textBox1.Text);
            double n2 = Double.Parse(textBox2.Text);
            if (n2 == 0)
            {
                MessageBox.Show("除數不能為0");
                return;
            }
            label1.Text = "= " + (n1 / n2).ToString();
        }
    }
}

2015年10月23日 星期五

n*n個不重複的亂數


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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int buttonSize = 3;
        const int buttonStartX = 10;
        const int buttonStartY = 10;
        const int buttonWidth = 50;
        const int buttonHeight = 50;

        Button[] newBtn = new Button[buttonSize * buttonSize];
        Button startButton;
        Random rnd = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //b1.Location = new Point(10, 10);

            int count = 0;

            for (int i = 0; i < buttonSize; i++)
            {
                for (int j = 0; j < buttonSize; j++)
                {
                    newBtn[i + j * buttonSize] = new Button();
                    newBtn[i + j * buttonSize].Location = new Point(buttonStartX + buttonWidth * i, buttonStartY + buttonHeight * j);
                    newBtn[i + j * buttonSize].Text = (count + 1).ToString();
                    newBtn[i + j * buttonSize].Name = newBtn[i + j * buttonSize].Text;
                    newBtn[i + j * buttonSize].Size = new Size(buttonWidth, buttonHeight);
                    newBtn[i + j * buttonSize].TabIndex = count + 1;
                    this.Controls.Add(newBtn[i + j * buttonSize]);
                    newBtn[i + j * buttonSize].Click += new System.EventHandler(button1_Click);
                    newBtn[i + j * buttonSize].Enabled = false;
                    count++;
                }
            }

            startButton = new Button();
            startButton.Location = new Point(buttonStartX + buttonWidth * (buttonSize + 2), buttonStartY + buttonHeight * buttonSize);
            startButton.Text = (count + 1).ToString();
            startButton.Name = startButton.Text;
            startButton.Size = new Size(buttonWidth*2, buttonHeight*2);
            startButton.TabIndex = count + 1;
            this.Controls.Add(startButton);
            startButton.Click += new System.EventHandler(startbutton_Click);
            startButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
            startButton.BackgroundImage = Image.FromFile("C:\\Users\\student\\Downloads\\IMAG0003.jpg");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(sender.ToString());
        }

        private void startbutton_Click(object sender, EventArgs e)
        {
            int[] arr = new int[buttonSize * buttonSize + 1];
            for (int i = 0; i < buttonSize * buttonSize + 1; i++)
                arr[i] = i;

            for (int i = 0; i < buttonSize * buttonSize; i++)
            {
                int rand = rnd.Next(1, buttonSize * buttonSize);
                if (rand == i)
                    continue;
                int tem = arr[rand];
                arr[rand] = arr[i];
                arr[i] = tem;
                //newBtn[i].Text = rnd.Next(1, 10).ToString();
            }

            for (int i = 0; i < buttonSize * buttonSize; i++)
                newBtn[i].Text = arr[i].ToString();
        }
    }
}

2015年10月2日 星期五

紅綠燈

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int c = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            c++;
            button1.Text = c.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            c++;
            button1.Text = c.ToString();
            button2.Text = c.ToString();
            button3.Text = c.ToString();

            if (c % 3 == 0)
            {
                button2.BackColor = System.Drawing.Color.White;
                button1.BackColor = System.Drawing.Color.Red;
                button3.BackColor = System.Drawing.Color.White;
            }
            else if (c % 3 == 1)
            {
                button2.BackColor = System.Drawing.Color.Yellow;
                button1.BackColor = System.Drawing.Color.White;
                button3.BackColor = System.Drawing.Color.White;
            }
            else if (c % 3 == 2)
            {
                button2.BackColor = System.Drawing.Color.White;
                button1.BackColor = System.Drawing.Color.White;
                button3.BackColor = System.Drawing.Color.Green;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

程式設計工藝大師

程式設計工藝大師
D0250179 曾敬允