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)
        {

        }
    }
}