วันอาทิตย์ที่ 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);
}
}


วันอาทิตย์ที่ 11 มิถุนายน พ.ศ. 2560

C++ queue

#include <iostream>
using namespace std;
#define N 11
int queuedata[N];
int front = 0;
int rear = 0;

int add(int data)
{
if (rear < N - 1)
{
rear++;
queuedata[rear] = data;
if (!front)
return -1;
return 1;
}
return -1;
}
int get()
{
int temp;
if (front)
{
temp = queuedata[front];
if (front > rear)
{
front = 0;
rear = 0;
return -1;
}
else
front++;
}
else
return -1;

return temp;
}
void show()
{
for (int i = front+1; i < rear + 1; i++)
{
cout << queuedata[i] << " ";
}
cout << endl;
}

int main(int argc, char** argv)
{
add(5);
add(8);
get();
show();
return 0;
}

C++ stack

#include <iostream>
using namespace std;
#define N 10
int stackdata[N];
int top = -1;

int push(int n)
{
if (top < N - 1)
{
top++;
stackdata[top] = n;
return 1;
}
return -1;
}
int pop()
{
int r;
if (top > -1)
{
r = stackdata[top];
stackdata[top] = 0;
top--;
return r;
}
return -1;
}
int main(int argc, char** argv)
{
push(1);
push(3);
pop();
push(9);
push(9);
push(3);

for (int i = 0; i <= top; i++)
{
cout << stackdata[i] << " ";
}
cout << endl;
return 0;
}

วันเสาร์ที่ 10 มิถุนายน พ.ศ. 2560

C++ การเรียงกลับหลังใน Array

#include <iostream>
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int i;

for (i = 0; i < N / 2; i++)
{
int temp;
temp = data[i];
data[i] = data[N - 1 - i];
data[N - 1 - i] = temp;
}

for (int i = 0; i < N; i++)
cout << data[i] << " ";

cout << endl;
return 0;
}