วันเสาร์ที่ 26 พฤศจิกายน พ.ศ. 2559

C++ UI Basic Threshold


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
int threshold_value = 0;
int threshold_type = 3;
int const max_value = 255;
int const max_type = 4;
int const max_BINARY_value = 255;

Mat srcimg;
Mat grayimg;
srcimg = imread("D:\\1.png", IMREAD_COLOR);
if (srcimg.empty())
{
MessageBox::Show("No Image");
Application::Exit();
}
DrawCVImage(pictureBox1, srcimg);
cvtColor(srcimg, grayimg, COLOR_BGR2GRAY);
Mat dst;
threshold(grayimg, dst, trackBar1->Value, max_BINARY_value, trackBar2->Value);
DrawCVImage(pictureBox2, dst);
}
void DrawCVImage(Control^ control, Mat& image)
{
Graphics^ graphic = control->CreateGraphics();
IntPtr ptr(image.ptr());
Bitmap^ b = gcnew Bitmap(image.cols, image.rows, image.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
RectangleF rec(0, 0, control->Width, control->Height);
graphic->DrawImage(b, rec);
}
private: System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e)
{
button1->PerformClick();
label1->Text = trackBar1->Value.ToString();
}
private: System::Void trackBar2_Scroll(System::Object^  sender, System::EventArgs^  e)
{
button1->PerformClick();
label3->Text = trackBar2->Value.ToString();
}
};

}


C++ UI Canny


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
Mat srcimg;
Mat grayimg;
int thresh = 100;
int max_thresh = 255;

RNG rng(12345);

srcimg = imread("D:\\1.png", IMREAD_COLOR);
if (srcimg.empty())
{
MessageBox::Show("No Image");
Application::Exit();
}
//convert RGB to GRAY
cvtColor(srcimg, grayimg, COLOR_BGR2GRAY);
blur(grayimg, grayimg, cv::Size(3, 3));
//draw source image
DrawCVImage(pictureBox1, srcimg);

Mat canny;
vector<vector<cv::Point>> contours;
vector<Vec4i> heirachy;

Canny(grayimg, canny, trackBar1->Value, max_thresh, 3);
findContours(canny, contours, heirachy, RETR_TREE, CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

Mat drawing = Mat::zeros(canny.size(), CV_8UC3);
for (size_t i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, (int)i, color, 2, 8, heirachy, 0, cv::Point());
}
//draw Canny Image
DrawCVImage(pictureBox2, drawing);
}
void DrawCVImage(Control^ control, Mat& image)
{
Graphics^ graphic = control->CreateGraphics();
IntPtr ptr(image.ptr());
Bitmap^ b = gcnew Bitmap(image.cols, image.rows, image.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
RectangleF rec(0, 0, control->Width, control->Height);
graphic->DrawImage(b, rec);
}
private: System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e)
{
button1->PerformClick();
label1->Text = trackBar1->Value.ToString();
}


วันพุธที่ 23 พฤศจิกายน พ.ศ. 2559

C++ open file Dialog



//insert picture to picturebox
DrawCVImage(pictureBox1, img);
void DrawCVImage(System::Windows::Forms::Control^ control, cv::Mat& colorImage)
{
System::Drawing::Graphics^ graphics = control->CreateGraphics();
System::IntPtr ptr(colorImage.ptr());
System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(colorImage.cols, colorImage.rows, colorImage.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr);
System::Drawing::RectangleF rect(0, 0, control->Width, control->Height);
graphics->DrawImage(b, rect);
}

//simulate button click
private: System::Void trackBar1_Scroll(System::Object^  sender, System::EventArgs^  e)
{
button1->PerformClick();
label1->Text = trackBar1->Value.ToString();
}

C++ GUI setup









วันพุธที่ 16 พฤศจิกายน พ.ศ. 2559

C++ check ID card 13 digit


long long power(int p)//3,4
{
long long sum = 1;
for (int i = 1; i <= p; i++)
{
sum *= 10;
}
return sum;
}
int main()
{
system("cls");
long long n;
long long m[30];
long long sum = 0;
cout << "Enter Input : ";
cin >> n;
int k = 12;
for (int i = 0; i<k; i++)
{
m[i] = n % (power(k - i)) / (power(k - 1 - i));
}
for (int i = 0; i<k; i++)
{
sum += m[i] * (k - i + 1);
}
cout << endl;
long long mod = sum % 11;
mod = 11 - mod;
cout << n << " " << mod << " ";
        return 0;
}


C++ check ISBN 10 digit



long long power(int p)//3,4
{
long long sum = 1;
for (int i = 1; i <= p; i++)
{
sum *= 10;
}
return sum;
}
int main()
{
system("cls");
long long m1[20];
long long m2[20];
long long m3[20];
long long sum = 0;
int l = 0, p = 0, n = 0;
string c;
string ch;
cout << "Input ISBN : ";
cin >> ch;

string i = ch.substr(0, 3);
string j = ch.substr(4, 3);
string k = ch.substr(8, 3);
l = stoll(i);
p = stoll(j);
n = stoll(k);

for (int i = 0; i < 3; i++)
{
m1[i] = n % (power(3 - i)) / (power(2 - i));
}
for (int i = 0; i<3; i++)
{
sum += m1[i] * (3 - i + 1);
}
///////////
for (int i = 0; i < 3; i++)
{
m2[i] = p % (power(3 - i)) / (power(2 - i));
}
sum += m2[0] * 7;
sum += m2[1] * 6;
sum += m2[2] * 5;
//////
for (int i = 0; i < 3; i++)
{
m3[i] = l % (power(3 - i)) / (power(2 - i));
}
sum += m3[0] * 10;
sum += m3[1] * 9;
sum += m3[2] * 8;
int mod = 11-(sum % 11);
if (mod == 10)
{
c = 'X';
}
else
{
c = to_string(mod);
}
cout << endl;
cout << ch << "-" << c << endl;
        return 0;
}



C++ นับข้อความ


string c("what a wonderful world");
int word = 1, l;
l = c.length();
for (int i = 0; i<l; ++i)
{
if (c[i] == ' ')
{
word = word + 1;
}
}
cout << endl << c << endl;
cout <<endl<< word << endl;
cout << endl;


C++ แยกสตริงออกมาเป็นคำ



string c("what a wonderful world");
int l;
l = c.length();
cout << "\"";
for (int i = 0; i<l; ++i)
{
if (c[i] == ' ')
cout <<"\","<<c[i]<<"\"";
else
{
cout << c[i];
}
}
cout << "\"";
cout << endl;


C++ หา square root by bisection


double l=0, h, m;
int a;
cout << "Input a : ";
cin >> a;
if(a<0)
{
cout << "Error" << endl;
}
else
{
h = (double)a;
m = (l + h) / 2;
int c = 0;
while (c < 1000)
{
if (pow(m, 2) == a)
{
c = 1000;
}
else
{
if (pow(m, 2) > a)
{
h = m;
m = (l + h) / 2;
c++;
}
else
{
l = m;
m = (l + h) / 2;
c++;
}
}
}
}
cout << setprecision(20);
cout << "Square root of " << a << " = " << m << endl;


C++ 3x+1 problem ให้หาค่าที่มากที่สุด จาก 1 -1000



int k=1000;
int max = -1;
int icount,ik;
int count = 0;
int i,m=k;
cout << 1 << "\t";
for (int j = 1; j<=k; j++)
{
i = j;
m = j;
while (i >= 1)
{
if (m % 2 == 0)
{
m = m / 2;
cout << m << "\t";
count++;
if (m == 1)
{
cout << "count = " << count << "\t";
if (count > max)
{
max = count;
icount = max;
ik = j;
}
count = 0;
break;
}
}
else
{
m = (3 * m + 1);
cout << m << "\t";
count++;
if (m == 1)
{
cout << "count = "<<count << "\t";
if (count > max)
{
max = count;
icount = max;
ik = j;
}
count = 0;
break;
}
}
i--;
}
cout << endl;
cout << "-------------------------------------------------------------------------------------------------------------------" << endl;
if (j != k)
cout << j + 1 << "\t";
else
cout << "";
count = 0;
}
cout << endl;
cout << "Max  = " << ik << " , Count = " << icount << endl;


C++ แสดงวันของปี


int day, month, year;
int x=0;
cout << "Input {day month year} : ";
cin >> day >> month >> year;

if (month == 1)
{
day += 0;
}
else if (month == 2)
{
day = day + 31 ;
}
else if (month == 3)
{
day =  day + 31 + 28;
}
else if (month == 4)
{
day = day  + 31 + +28 + 30;
}
else if (month == 5)
{
day = day  + 31 +28 +  30 + 31;
}
else if (month == 6)
{
day = day  + 31 + 28 + 30 + 31 + 30;
}
else if (month == 7)
{
day = day + 31 + 28 + 30 + 31 + 30 + 31 ;
}
else if (month == 8)
{
day = day + 31 + 28 + 30 + 31 + 30 + 31 + 31;
}
else if (month == 9)
{
day = day +  31 + 28 + 30 + 31 + 30 + 31 + 31 + 30;
}
else if (month == 10)
{
day = day +  31 + 28 +  30 + 31 + 30 + 31 + 31 + 30 + 31;
}
else if (month == 11)
{
day = day + 31 + 28 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
}
else if (month == 12)
{
day = day +  31 + 28 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31;
}
if (year % 2 == 1 && month > 2)
day = day + 1;
cout << day << endl;


C++ การหาผลต่างของเวลา


int h1, h2, m1, m2, h, m;
do
{
cout << "Enter input {Hour minute} 1 : ";
cin >> h1 >> m1;
} while (h1<1 || h1>12 || m1<0 || m1>60);
do
{
cout << "Enter input {Hour minute} 2 : ";
cin >> h2 >> m2;
} while (h2<1 || h2>12 || m2<0 || m2>60);

h = abs(h2 - h1);
m = (m2 - m1);
if (m<0)
{
m = 60 + m;
h = h - 1;
}
cout << h << ":" << m << endl;


C++ แสดงค่า a,b,c ที่ a^2+b^2 = c^2 โดยที่เลขไม่ซ้ำกัน


int a, b, c;
a = b = c = 500;
int x, y;
for (int i = 1; i < a; i++)
{
for (int j = 1; j < b; j++)
{
for (int k = 1; k < c; k++)
{
if (pow(i, 2) + pow(j, 2) == pow(k, 2))
{
y = j;
x = i;
if (x >= j || y <= i)
cout << y << "\t" << x << "\t" << k << endl;
}
}
}
}


C++ หาค่ามากที่สุดที่อันดับที่สอง


int n[10];
int max = -pow(2, 31);
for (int i = 0; i<10; i++)
{
cout << "Input #" << i + 1 << " : ";
cin >> n[i];
}
for (int i = 0; i<10; i++)
{
if (n[i]>max)
{
max = i;
}
}
cout << endl;
cout << n[max] << endl;


C++ แสดงเครื่องหมายจุลภาค


bool isabs(int p) //5
{
if (p<0)
return false;
return true;
}
int power(int p)
{
int sum = 1;
for (int i = 1; i <= p; i++)
{
sum *= 10;
}
return sum;
}
int main()
{
system("cls");
int n;
int m[30];
int is;
cout << "Enter Input : ";
cin >> n;
int a = abs(n);
is = isabs(n);
string s = to_string(a);
int k = s.length();
if (!is)
{
cout << "-";
}
else
cout << "";
for (int i = 0; i<k; i++)
{
m[i] = a % (power(k - i)) / (power(k - 1 - i));
}
for (int i = 0; i<k; i++)
{
if (i % (k - 1) == k - 4 || i % (k - 1) == k - 7)
{
cout << m[i]<< ",";
}
else
cout << m[i];
}
        retrun 0;
}


C++ ตรวบสอบข้อมูลที่มีค่าเท่ากัน


int a[3], i, j = 0;
for ( i = 0; i < 3; i++)
{
cout << "Input a" << i << " : ";
cin>>a[i];
}
for ( i = 0; i < 2; i++)
{
if (a[i] == a[i + 1])
{
cout << "a[" << i << "] = a[" << i + 1 << "] {" << a[i] << "}" << endl;
}
else
{
j++;
}
}
if (j==2)
{
cout << "Have not a equal" << endl;
}



C++ ตรวจสอบด้านประกอบเป็นสามเหลี่ยม


int a, b, c,max;
cout << "Enter triangle {a,b,c} : ";
cin >> a >> b >> c;

if (a > b)
max = a;
else
max = b;
if (c > max)
max = c;
if (max + max < a + b + c)
cout << "This is a triangle" << endl;
else
cout << "This is not a triangle" << endl;


C++ แสดงข้อความเยื้องลงมาทีละ 1 ช่อง แล้วให้ทำย้อนกลับ


string str;
cout << "Enter String : ";
cin >> str;
for (int i = 0; i < str.length(); i++)
{
for (int j = 0; j < i; j++)
{
cout << "-";
}
cout << str[i];
cout << endl;
}
for (int i = str.length() - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
cout << "-";
}
cout << str[i];
cout << endl;
}


C++ palindrome


char str[50];
int start = 0, pcheck = 0, end;
cout << "Enter String : ";
cin >> str;

cout << "Result ";
end = strlen(str);
end = end - 1;
while ((end > start) && (pcheck == 0))
{
while (!ischeckchar(str[start]))start++;
while (!ischeckchar(str[end]))end--;
if (toupper(str[start]) != toupper(str[end])) pcheck = 1;
start++;
end--;
}
if (pcheck == 0)
cout << "This message is palindome. " << endl;
else
cout << "This message is not palindome. " << endl;


C++ แทรกสตริง


string str("There are two needles in this haystack with in needles.");
string keyword;
cout << str << endl;
cout << "Enter Keyword : ";
cin >> keyword;
int k = keyword.length();
int n = str.find(keyword);
cout << "Search "<<keyword<<" at : "<<n << endl;
str.erase(str.begin(),str.begin()+n+k+1);
//cout << str << endl;
int m = str.find(keyword);
if(m != -1)
cout << "Search " << keyword << " at : " << m+n << endl;


C++ แปลงอักขระโรมัน


char ch[30];
int sum = 0;
int m, d, c, l, x, v, i;
m = d = c = l = x = v = i = 0;
cout << "Input : ";
cin >> ch;

for (int j = 0; j<strlen(ch); j++)
{
if (ch[j] == 'M')
{
m += 1000;
}
if (ch[j] == 'D')
{
d += 500;
}
if (ch[j] == 'C')
{
c += 100;
}
if (ch[j] == 'L')
{
l += 50;
}
if (ch[j] == 'X')
{
x += 10;
}
if (ch[j] == 'V')
{
v += 5;
}
if (ch[j] == 'I')
{
i += 1;
}
sum = m + d + c + l + x + v + i;
}
cout << endl;
cout << sum << endl;


C++ LED Number



char x[10][5][5] = { {
"xxxx",
"x  x",
"x  x",
"x  x",
"xxxx",
},

{
"   x",
"   x",
"   x",
"   x",
"   x",
},

{
"xxxx",
"   x",
"xxxx",
"x   ",
"xxxx",
},
{
"xxxx",
"   x",
"xxxx",
"   x",
"xxxx",
},
{
"x  x",
"x  x",
"xxxx",
"   x",
"   x",
},
{
"xxxx",
"x   ",
"xxxx",
"   x",
"xxxx",
},
{
"x   ",
"x   ",
"xxxx",
"x  x",
"xxxx",
},
{
"xxxx",
"   x",
"   x",
"   x",
"   x",
},
{
"xxxx",
"x  x",
"xxxx",
"x  x",
"xxxx",
},
{
"xxxx",
"x  x",
"xxxx",
"   x",
"   x",
} };
int num, temp[12], i, count;
cout << "Enter Number : ";
cin >> num;
count = 0;
i = 0;
while (num > 0)
{
temp[i] = num % 10;
num = num / 10;
i++;
}
count = i;
for (int k = 0; k < 5; k++)
{
for (i = count - 1; i >= 0; i--)
{
cout << x[temp[i]][k] << "\t";
}
cout << endl;
}


C++ กระดานมหาสนุก



int i, j, temp, num;
char dir;
char x[4][4] = { { 'a','b','c','d' },
{ 'e','f','g','h' },
{ 'i','j','k','l' },
{ 'm','n','o','p' } };
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << x[i][j] << "\t";
}
cout << endl;
}
while (1)
{
cout << endl << "Input number {0-3} and char {l,r,u,d} : ";
cin >> num >> dir;
if (num == -1)
{
break;
}
if (dir == 'l')
{
temp = x[num][0];
for (i = 0; i < 3; i++)
{
x[num][i] = x[num][i + 1];
}
x[num][3] = temp;
}
else if (dir == 'r')
{
temp = x[num][3];
for (i = 3; i>0; i--)
{
x[num][i] = x[num][i - 1];
}
x[num][0] = temp;
}
else if (dir == 'd')
{
temp = x[3][num];
for (i = 3; i>0; i--)
{
x[i][num] = x[i - 1][num];
}
x[0][num] = temp;
}
else if (dir == 'u')
{
temp = x[0][num];
for (i = 0; i<3; i++)
{
x[i][num] = x[i + 1][num];
}
x[3][num] = temp;
}
cout << endl << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << x[i][j] << "\t";
}
cout << endl;
}
}


C++ leading in digit number


int leadDigit(int n)
{
int m[9];
int k = 100'000'000;
int i;
for (int i = 7; i >=0; i--)
{
m[i] = n % k / (k / 10);
k /= 10;
}
for ( i = 7; i>=0;i--)
{
if (m[i] == 0)
{
m[i] = m[i - 1];
}
else
{
return m[i];
}
}
}
int main()
{
system("cls");
int k;
cout << "Enter Number : ";
cin >> k;
cout<<"The leading digit of " <<k<<" is "<<leadDigit(k) << endl;
        return 0;
}


วันเสาร์ที่ 12 พฤศจิกายน พ.ศ. 2559

C++ LED NUMBER


#include <iostream>
#include <math.h>
#include <iomanip>
#include <time.h>
#include <thread>
#include <windows.h>
using namespace std;

int main(int argc, char** args)
{
char x[10][5][5] = { {
"xxxx",
"x  x",
"x  x",
"x  x",
"xxxx",
},

{
"   x",
"   x",
"   x",
"   x",
"   x",
},

{
 "xxxx",
 "   x",
 "xxxx",
 "x   ",
 "xxxx",
 },
 {
  "xxxx",
  "   x",
  "xxxx",
  "   x",
  "xxxx",
  },
  {
"x  x",
"x  x",
"xxxx",
"   x",
"   x",
},
{
"xxxx",
"x   ",
"xxxx",
"   x",
"xxxx",
},
{
"x   ",
"x   ",
"xxxx",
"x  x",
"xxxx",
},
{
"xxxx",
"   x",
"   x",
"   x",
"   x",
},
{
"xxxx",
"x  x",
"xxxx",
"x  x",
"xxxx",
},
{
"xxxx",
"x  x",
"xxxx",
"   x",
"   x",
} };
    int num, temp[12], i, count;
char c;
srand(time(NULL));
for (int k = 1; k < 1000; k++)
{
if (k == 999)
k = 1;
num = k;
count = 0;
i = 0;
while (num > 0)
{
temp[i] = num % 10;
num = num / 10;
i++;
}
count = i;
cout << endl;
for (int k = 0; k < 5; k++)
{
for (i = count - 1; i >= 0; i--)
{
cout << x[temp[i]][k] << "\t";
}
Sleep(10);
cout << endl;
}
cout << endl;
}
return 0;
}


C+ กระดานมหาสนุก


#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;


int main(int argc, char** args)
{
int i, j, temp,num;
char dir;
char x[4][4] = { {'a','b','c','d'},
{'e','f','g','h'},
{'i','j','k','l'},
{'m','n','o','p'} };
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << x[i][j] << "\t";
}
cout << endl;
}
while (1)
{
cout <<endl<< "Input number {0-3} and char {l,r,u,d} : ";
cin >> num >> dir;
if (num == -1)
{
break;
}
if (dir == 'l')
{
temp = x[num][0];
for (i = 0; i < 3; i++)
{
x[num][i] = x[num][i + 1];
}
x[num][3] = temp;
}
else if (dir == 'r')
{
temp = x[num][3];
for (i = 3; i>0; i--)
{
x[num][i] = x[num][i - 1];
}
x[num][0] = temp;
}
else if (dir == 'd')
{
temp = x[3][num];
for (i = 3; i>0; i--)
{
x[i][num] = x[i-1][num];
}
x[0][num] = temp;
}
else if (dir == 'u')
{
temp = x[0][num];
for (i = 0; i<3; i++)
{
x[i][num] = x[i + 1][num];
}
x[3][num] = temp;
}
cout << endl << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << x[i][j] << "\t";
}
cout << endl;
}
}
return 0;
}