วันพุธที่ 18 เมษายน พ.ศ. 2561

Random Lot

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> ReadFile = File.ReadAllLines(@"D:\Data.txt").ToList();
            List<string> random = new List<string>();
            Random rnd = new Random();

            Console.Write("Do you want to Number of value : ");
            string number = Console.ReadLine();
            int want = 100- ReadFile.Count -(Int32.Parse(number))-1;
            for (int j = 0; j <100; j++)
            {
                random.Add(j.ToString());
       
            }
            for (int i = 0; i < ReadFile.Count; i++)
            {
                random.Remove(ReadFile[i]);
            }

            for (int j = 0; j < want; j++)
            {
                for (int i = 0; i < 1; i++)
                {
                    int k = rnd.Next(0, random.Count);
                    random.Remove(random[k]);

                }
                int count = 0;
                int row=20;
                foreach (string ii in random)
                {
                    if (Int32.Parse(count.ToString()) % row != 0)
                    {
                        if (Int32.Parse(ii) == Int32.Parse(random.Last()))
                        {
                            if (Int32.Parse(ii) < 10)
                            {
                                Console.Write("0" + ii.ToString());
                            }
                            else
                            {
                                Console.Write(ii.ToString());
                            }
                        }
                        else
                        {
                            if (Int32.Parse(ii) < 10)
                            {
                                Console.Write("0" + ii.ToString() + ",");
                            }
                            else
                            {
                                Console.Write(ii.ToString() + ",");
                            }
                        }                       
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    count++;
                    Thread.Sleep(10);
                }
                Console.WriteLine("\n");
            }
        }
    }
}

วันศุกร์ที่ 15 ธันวาคม พ.ศ. 2560

C# Search , Add , Delete DataGridView

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

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var item = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
            table1.Rows.Add(item.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            table = new DataTable();
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Rows.Add(1,"Mee");
            table.Rows.Add(2, "Yha");
            dataGridView1.DataSource = table;

            table1 = new DataTable();
            table1.Columns.Add("Name", typeof(string));
            dataGridView2.DataSource = table1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView2.CurrentCell.RowIndex;
                dataGridView2.Rows.RemoveAt(rowIndex);
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyData == Keys.Delete)
            {
                int rowIndex = dataGridView2.CurrentCell.RowIndex;
                    dataGridView2.Rows.RemoveAt(rowIndex);
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == (char)13)
            {
                DataView dv = table.DefaultView;
                dv.RowFilter = string.Format("Name like '%{0}%'" , textBox1.Text);
                dataGridView1.DataSource = dv.ToTable();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataView dv = table.DefaultView;
            dv.RowFilter = string.Format("Name like '%{0}%'", textBox1.Text);
            dataGridView1.DataSource = dv.ToTable();
        }
    }
}

วันศุกร์ที่ 24 พฤศจิกายน พ.ศ. 2560

C# Binary Search

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Linkedlist
{
    class Program
    {
        static public int[] numsort(int[] x)
        {
            int[] a = x;

            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j < a.Length - 1; j++)
                {
                    if (a[j] > a[j + 1])
                    {
                        int temp;
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            return a;
        }
        static public int binarySerach(int[] x,int find)
        {
            int left = 0;
            int right = x.Length-1;
            int mid;
            while(left <= right)
            {
                mid = (left + right) / 2;
                if (find == x[mid])
                    return mid;
                else if (find < x[mid])
                {
                    right = mid - 1;
                }
                else
                    left = mid + 1;
            }
            return -1;
        }
        static public void output(int[] x)
        {
            for(int i=0;i<x.Length;i++)
            {
                Console.Write(x[i]+" ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            Random r = new Random();
            int find = 5;
            int[] x = new int[10];
            for(int i=0;i<10;i++)
            {
                x[i] = r.Next(10) + 1;
            }
            numsort(x);
            output(x);
            int c = binarySerach(x, find);
            Console.WriteLine("Find : " + find + " Index : " + (c+1));
         

            Console.ReadLine();
        }   
    }

}

วันศุกร์ที่ 3 พฤศจิกายน พ.ศ. 2560

C# sequential Search

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 LinkedList
{
    public partial class Form1 : Form
    {
        int n = 100;
        int[] a;
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            a = new int[n];
            Random x = new Random();
            textBox1.Text = "";
            for(int i=0;i<n;i++)
            {
                a[i] = x.Next(100) + 1;
                textBox1.Text = textBox1.Text + "[" +i.ToString() + "]"+a[i].ToString() + " , ";
            }
        }

        private int sequentialsearch(int k)
        {
            int i=0;
            while(i<n && a[i] !=k)
            {
                i++;
            }
            if (i < n)
                return i;
            else
                return -1;             
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int k = Convert.ToInt32(textBox2.Text);
            textBox3.Text = sequentialsearch(k).ToString();
        }


    }

}

C# LinkedList

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 LinkedList
{
    public partial class Form1 : Form
    {
        singlrLinkedList sll;
        public Form1()
        {
            InitializeComponent();
            startup();
        }
        public void startup()
        {
            sll = new LinkedList.singlrLinkedList();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            sll.addLinkedList(textBox1.Text, Int32.Parse(textBox2.Text.ToString()));
         
            textBox1.Clear();
            textBox2.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox3.Text = sll.showall();
   
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sll.deletepos(Convert.ToInt32(textBox4.Text));
            textBox3.Text = sll.showall();
        }
    }
    public class node
    {
        public string name;
        public int age;
        public node next;

        public node()
        {
            next = null;
        }
    }
    public class singlrLinkedList
    {
        public node header;
        public node cur;
        public node newnode;
        public singlrLinkedList()
        {
            header = null;
            cur = null;
        }
        public void addLinkedList(string name ,int age)
        {
            newnode = new LinkedList.node();
            newnode.name = name;
            newnode.age = age;
           
            if (header == null)
            {
                header = newnode;
                cur = header;
            }
            else
            {
                cur.next = newnode;
                cur = cur.next;
            }
        }
        public void deletepos(int i)
        {
            node delx;
            cur = header;
            if(i==1)
            {
                header = cur.next;
                cur = header;
            }
            else
            {
                for(int j=1;j<i-1;j++)
                {
                    cur = cur.next;
                }
                delx = cur.next;
                if(delx != null)
                {
                    cur.next = delx.next;
                }
            }
        }
        public string showall()
        {
            int i = 1;
            string str = "";
            cur = header;
            while(cur !=null)
            {
                str = str + i.ToString() + " : " + cur.name + "," + cur.age.ToString()+Environment.NewLine;
                i++;
                cur = cur.next;
            }         
            return str;
        }
    }
}

วันอาทิตย์ที่ 29 ตุลาคม พ.ศ. 2560

C++ Stack

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <thread>
typedef struct node
{
int data;
struct node* link;
}STACK_NODE;

typedef struct
{
int count;
STACK_NODE *top;

}STACK;

void insertData(STACK* pStack);
void print(STACK* pStack);
bool push(STACK* pList, int dataIn);
bool pop(STACK* pList, int *dataOut);


int main()
{
STACK *pStack;
pStack = new STACK;
printf("=============STACK=============\n\n");
pStack->top = NULL;
pStack->count = 0;
insertData(pStack);
print(pStack);

return 0;
}

void insertData(STACK* pStack)
{
int numIn;
bool success;
srand(time(NULL));
printf("Create Number : ");
for (int nodeCount = 0; nodeCount < 10; nodeCount++)
{
numIn = rand() % 999;
printf("%4d", numIn);
success = push(pStack, numIn);
if (!success)
{
printf("Error out of memory\n");
exit(101);
}
}
printf("\n\n");
return;
}
void print(STACK* pStack)
{
int printData;
printf("Stack contained : \n");
printf("\t\t +--------+\n");
while (!pop(pStack, &printData))
{
printf("\t\t |  %4d  |\n", printData);
}
printf("\t\t +--------+\n");
return;
}
bool push(STACK* pList, int dataIn)
{
STACK_NODE* pNew;
bool success;
pNew = (STACK_NODE*)malloc(sizeof(STACK_NODE));
if (!pNew)
{
success = false;
}
else
{
pNew->data = dataIn;
pNew->link = pList->top;
pList->top = pNew;
pList->count++;
success = true;
}
return success;
}
bool pop(STACK* pList, int *dataOut)
{
STACK_NODE* pDlt;
bool success;

if (pList->top)
{
success = false;
*dataOut = pList->top->data;
pDlt = pList->top;
pList->top = (pList->top)->link;
pList->count--;
free(pDlt);
}
else
{
success = true;
}
return success;
}


วันเสาร์ที่ 14 ตุลาคม พ.ศ. 2560

C++ partial Sum


#include <vector>
#include <iostream>
using namespace std;

double sum(int n)
{
double partialSum = 0.0;
for (int k = 2; k <= n; k++)
{
partialSum += sqrt(k / (k + 1.)) -sqrt((k - 1.) / k);
}
return partialSum;
}
int main(int argc,char*argv)
{
cout << sum(1000) << endl;
return 0;
}