วันพุธที่ 16 พฤศจิกายน พ.ศ. 2559
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;
}
}
สมัครสมาชิก:
บทความ (Atom)






