วันศุกร์ที่ 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;
}





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

C++ opencv edit pixel

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

#include<iostream>
#include<conio.h>         

using namespace std;
using namespace cv;

void procFrame(cv::Mat &frame);
int main(int argc,char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Could not open camera" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

namedWindow("Exam", WINDOW_AUTOSIZE);
while (true)
{
Mat frame;
if (cap.read(frame))
{
procFrame(frame);
imshow("Exam", frame);
}
if (waitKey(30) == 27)
{
break;
}
}
return 0;
}

void procFrame(cv::Mat & frame)
{
static float alpha = 1.0f;
for (unsigned int z = 0; z<frame.rows; z++)
{
for (unsigned int i = 0; i < frame.cols; i++)
{
Vec3b &cur_pixel = frame.at<Vec3b>(z, i);
cur_pixel[0] *= (-alpha);
cur_pixel[1] *= (-alpha);
cur_pixel[2] *= alpha;
}
}
alpha += 0.1f;
}


C++ opencv drawing Box

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

#include<iostream>
#include<conio.h>       

using namespace std;
using namespace cv;

void my_mouse_calback(int event, int x, int y, int flags, void*data);

Rect Box;
Mat drawing;

bool drawing_box = false;

void draw_box(Mat& img, Rect box)
{
rectangle(img, box.tl(), box.br(), Scalar(255, 0, 0));
}

int main(int argc,char** argv)
{
int Bright = 1, Cont = 47;
Mat drawing, drawing2;
drawing = imread("D:\\3.jpg"); drawing.copyTo(drawing2);
Box = Rect(-1, -1, 0, 0);

namedWindow("Exam", CV_WINDOW_AUTOSIZE);


setMouseCallback("Exam", my_mouse_calback, (void*)&drawing);
while(1)
{
drawing.copyTo(drawing2);
if (drawing_box)draw_box(drawing2, Box);
imshow("Exam", drawing2);
if (waitKey(30) == 27)
break;
}
return 0;
}

void my_mouse_calback(int event, int x, int y, int flags, void*data)
{
Mat& drawing = *(Mat*)data;
int w, h, r;
if (event == EVENT_MOUSEMOVE)
{
if (drawing_box)
{
Box.width = x - Box.x;
Box.height = y - Box.y;
}
}

if (event == EVENT_LBUTTONDOWN)
{
drawing_box = true;
Box = Rect(x, y, 0, 0);
}
if (event == EVENT_LBUTTONUP)
{
drawing_box = false;

if (Box.width < 0)
{
Box.x += Box.width;
Box.width *= -1;
}
if (Box.height < 0)
{
Box.x = Box.height;
Box.width *= -1;
}
draw_box(drawing, Box);
}
}