2016年2月18日 星期四

JAVA 資料庫

import java.sql.Connection;
import java.sql.DriverManager;

public class JAVA_3_1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection c = null;
 try {
  Class.forName("org.sqlite.JDBC");
  c = DriverManager.getConnection("jdbc:sqlite:test.db");
 } catch (Exception e) {
  System.err.println(e.getClass().getName() + ": " + e.getMessage());
  System.exit(0);
 }
 System.out.println("Opened database successfully");
}

}

2015年12月29日 星期二

Jacobi Iterative (雅可比迭代) C語言實現



#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void main(){
int i, j, k;
int n, N;
double **arrA, *arrB, *arrXO, *arrX, TOL, tem;

printf("***********  input  **********\n");

printf("n=");
scanf("%d", &n);

arrA = (double **)malloc(sizeof(double) * n * (n + 1));
for (i = 0; i < n; i++)
arrA[i] = (double *)arrA + n * (i + 1);
arrB = (double *)malloc(sizeof(double) * n);
arrXO = (double *)malloc(sizeof(double) * n);
arrX = (double *)malloc(sizeof(double) * n);

printf("\n");
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
printf("A[%d][%d]=", i, j);
scanf("%lf", &arrA[i][j]);
}
printf("\n");
}

printf("\n");
for (i = 0; i < n; i++){
printf("B[%d]=", i);
scanf("%lf", &arrB[i]);
}

printf("\n");
for (i = 0; i < n; i++){
printf("XO[%d]=", i);
scanf("%lf", &arrXO[i]);
}

printf("\nTOL=");
scanf("%lf", &TOL);

printf("\nN=");
scanf("%d", &N);

/*printf("***********  information  **********\n");
printf("\nA=\n");
for (i = 0; i < n; i++){
for (j = 0; j < n; j++)
printf(" %lf", arrA[i][j]);
printf("\n");
}
printf("\nB=\n");
for (i = 0; i < n; i++)
printf(" %lf", arrB[i]);
printf("\nXO=\n");
for (i = 0; i < n; i++)
printf(" %lf", arrXO[i]);*/

printf("\n\n***********  Jacobi Iterative (雅可比迭代?)  **********\n");

k = 0;
while (k <= N){
for (i = 0; i < n; i++){
arrX[i] = 0;
for (j = 0; j < n; j++){
if (i == j)
continue;
arrX[i] -= arrA[i][j] * arrXO[j];
}
arrX[i] = (arrX[i] + arrB[i]) / arrA[i][i];
}
tem = 0;
for (i = 0; i < n; i++)
tem += abs(arrX[i] - arrXO[i]);

if (tem < TOL){
printf("\n\nX =");
for (i = 0; i < n; i++)
printf(" %lf", arrX[i]);
break;
}
k++;
for (i = 0; i < n; i++)
arrXO[i] = arrX[i];

if (k > N)
printf("\n\nMaximum number of iterations exceeded\n\n");
}

free(arrA);
free(arrB);
free(arrXO);
free(arrX);

printf("\n\n***********  end  **********\n");
system("pause");
}


2015年12月18日 星期五

資料庫

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;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Random rnd = new Random(Guid.NewGuid().GetHashCode());

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'db1DataSet.asd' 資料表。您可以視需要進行移動或移除。
            this.asdTableAdapter.Fill(this.db1DataSet.asd);

            textBox1.DataBindings.Add("Text", asdBindingSource, "result");
            textBox2.DataBindings.Add("Text", asdBindingSource, "time");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //剪刀
            act(0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //石頭
            act(1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //布
            act(2);
        }

        string tra(int n)
        {
            if (n == 0)
                return "剪刀";
            else if (n == 1)
                return "石頭";
            else if (n == 2)
                return "布";
            return "GG";
        }

        void act(int player)
        {
            //0=剪刀 1=石頭
            int computer = rnd.Next(3);

            if (player == computer)
            {
                //平手
                label1.Text = "電腦出 " + tra(computer);
                textBox1.Text = "平手";
                //asdBindingSource.Add(1);
                //db1DataSet.asd.
                /*db1DataSet.asdDataTable dt = new db1DataSet.asdDataTable();
                //DataTable dt = new DataTable();
                DataRow dr;
                //dt.Columns.Add(new DataColumn("bankname", typeof(string))); //新增一欄位, 欄位名稱為"bankname"
                //加入第一筆銀行資料
                dr = dt.NewRow();
                dr["result"] = "1";
                dr["time"] = DateTime.Now;
                dt.Rows.Add(dr);
                dt.AcceptChanges();
                db1DataSet.AcceptChanges();
                asdTableAdapter.Update(dt.Rows);
                dataGridView1.Update();*/
                MessageBox.Show("平手");
            }
            else if ((player == 0 && computer == 1) ||
                (player == 1 && computer == 2) ||
                (player == 2 && computer == 0))
            {
                //電腦贏
                label1.Text = "電腦出 " + tra(computer);
                textBox1.Text = "輸";
                /*textBox1.DataBindings.Clear();
                textBox2.DataBindings.Clear();
                textBox2.Text = DateTime.Now.ToString();
                textBox2.DataBindings.Add("Text", bindingSource1, "time");
                textBox1.DataBindings.Add("Text", bindingSource1, "result");*/
                MessageBox.Show("電腦贏");
            }
            else if ((player == 1 && computer == 0) ||
                (player == 2 && computer == 1) ||
                (player == 0 && computer == 2))
            {
                //玩家贏
                label1.Text = "電腦出 " + tra(computer);
                textBox1.Text = "贏";
                MessageBox.Show("玩家贏");
            }


            textBox2.Text = DateTime.Now.ToString();

            asdBindingSource.Insert(1,dr);
            asdBindingSource.AddNew();
        }
        /*
        void dbUpdate(){
            DataSet dsChanges = (DataSet)db1DataSet.GetChanges();
            if (dsChanges == null)
            {
                return;
            }

            DataTable dtb = new DataTable();
            dtb = dsChanges.Tables["products"];
            //GetErrors方法返回一個數組,它由表中具一個或多個檢驗錯誤的行構成。如沒有誤返加空數組
            DataRow[] badRows = dtb.GetErrors();

            if (badRows.Length == 0)
            {
                //int numRows = suppliersTableAdapter.Update(dsChanges, "products");
                //要在需要时生成所需的命令,必须创建 SqlCommandBuilder 对象的实例并使用该构造函数中的 DataAdapter。
                //1、
                SqlCommandBuilder sqlbuiler = new SqlCommandBuilder();
                sqlbuiler.DataAdapter = asdTableAdapter;

                //2、
                SqlConnection dataConnection = new SqlConnection();
                dataConnection.ConnectionString = "Data Source=CCM02\\SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sh2_123";

                //dataConnection.Open();

                string sCmdText = "select * from products";
                //3、
                SqlCommand sqlComad = new SqlCommand();
                //指出CommandText讀取方式
                sqlComad.CommandType = CommandType.Text;
                //Connection:連接資料庫的Connection
                sqlComad.Connection = dataConnection;
                //CommandText:取得或設定要針對資料來源執行的文字命令。
                sqlComad.CommandText = sCmdText;

                //4、
                //suppliersTableAdapter.SelectCommand = new SqlCommand("select * from products ", dataConnection);
                //SelectCommand 取得或設定用來在資料來源中選取資料錄的命令。
                suppliersTableAdapter.SelectCommand = sqlComad;
                //取得或設定接資料庫的Connection
                //取得或設定用來將新的資料錄插入至資料來源的命令。
                //GetInsertCommand:取得在資料來源上執行插入時所需之自動產生的 DbCommand 物件。
                suppliersTableAdapter.InsertCommand = sqlbuiler.GetInsertCommand();
                suppliersTableAdapter.UpdateCommand = sqlbuiler.GetUpdateCommand();
                suppliersTableAdapter.DeleteCommand = sqlbuiler.GetDeleteCommand();


                //5、
                int numRows = suppliersTableAdapter.Update(dsChanges, "products");

                if (numRows > 0)
                {
                    MessageBox.Show("Update" + numRows + "rows", "Success");
                    //變更資料
                    dtSet.AcceptChanges();
                }
                else
                {
                    //返原所有更
                    dtSet.RejectChanges();
                }

                //清除與這個 DbCommandBuilder 關聯的命令。
                sqlbuiler.RefreshSchema();
                //關閉聯接
                dataConnection.Close();
                //釋放資源
                sqlComad.Dispose();
                sqlbuiler.Dispose();
            }
            else
            {
                string errorMsg = null;
                foreach (DataRow row in badRows)
                {
                    //每一行都可能一個或多個誤,而GetColumnsInError方法將返加一個集合,其中包含了數據有問題的所有例
                    foreach (DataColumn col in row.GetColumnsInError())
                    {
                        //GetColumnError方法獲取一個無效的列的錯誤消息。每一條錯誤消息都附加到errorMsg字符串上。
                        errorMsg += row.GetColumnError(col) + "\n";
                    }
                }
                MessageBox.Show("Errors in data:" + errorMsg, "please fix", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        */

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

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();
        }
    }
}