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