Source code các giải thuật vẽ hình trong c
1) Hình vuông
void veHinhVuongDac(int dong) {
for (int i = 0; i < canh; i++){
for (int j = 0; j < canh; j++){
if (i == 0 || i == canh - 1) printf("*");
else if (j >= 0 || j <= canh - 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
*****
*****
*****
*****
*****
void veVuongRong(int dong) {
for (int i = 0; i < canh; i++){
for (int j = 0; j < canh; j++){
if (i == 0 || i == canh - 1) printf("*");
else if (j >= 0 || j <= canh - 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
*****
* *
* *
* *
*****
2) Hình chữ nhật
void veHinhChuNhatDac(int cao, int rong) {
for (int i = 0; i < cao; i++){
for (int j = 0; j < rong; j++){
if (i == 0 || i == cao - 1) printf("*");
else if (j >= 0 || j <= rong - 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
*******
*******
*******
*******
void veHinhChuNhatRong(int cao, int rong) {
for (int i = 0; i < cao; i++){
for (int j = 0; j < rong; j++){
if (i == 0 || i == cao - 1) printf("*");
else if (j == 0 || j == rong - 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
*******
* *
* *
*******
1) Tam giác
void veHinhTamGiacTrai(int dong) {
for (int i = 0; i < dong; i++)
{
for (int j = 0; j <= i; j++)
{
printf("*");
}
printf("\n");
}
}
********
*******
******
*****
****
***
**
*
void veHinhTamGiacTraiRong(int dong) {
for (int i = 0; i < dong; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == dong - 1) printf("*");
else if (i == j) printf("*");
else printf(" ");
}
printf("\n");
}
}
********
* *
* *
* *
* *
* *
**
*
void veTamGiacPhai(int dong) {
int tmp = 0;
for (int i = 0; i < dong; i++)
{
// in khoang tromg
for (int l = 0; l < tmp; l++)
{
printf(" ");
}
for (int j = tmp; j < dong; j++)
{
printf("*");
}
tmp++;
printf("\n");
}
}
********
*******
******
*****
****
***
**
*
void veTamGiacPhaiRong(int dong) {
int tmp = 0;
for (int i = 0; i < dong; i++)
{
// in khoang tromg
for (int l = 0; l < tmp; l++)
{
printf(" ");
}
for (int j = tmp; j < dong; j++)
{
if(i == 0 || j == dong -1) printf("*");
else if (i == j) printf("*");
else printf(" ");
}
tmp++;
printf("\n");
}
}
********
* *
* *
* *
* *
* *
**
*
void veTamGiacGiua(int x) {
if (x % 2 == 0 || x < 3) {
printf("Khong ve duoc");
return;
}
int mid = x / 2;
int left = mid, right = mid;
int tmp = x;
for (int i = 0; i < tmp; i++)
{
for (int j = 0; j < x; j++)
{
if (j == mid) printf("*");
else if (j >= left && j <= right) printf("*");
else printf(" ");
}
left--;
right++;
tmp--;
printf("\n");
}
}
*
***
*****
*******
2) Hình Thoi
void veTamGiacGiuaRong(int x) {
if (x % 2 == 0 || x < 3) {
printf("Khong ve duoc");
return;
}
int mid = x / 2;
int left = mid, right = mid;
int tmp = x;
for (int i = 0; i < tmp; i++)
{
for (int j = 0; j < x; j++)
{
if (j == mid) printf("*");
else if (j >= left && j <= right) printf("*");
else printf(" ");
}
left--;
right++;
tmp--;
printf("\n");
}
}
*
* *
* *
*******
void veHinhThoi(int x) {
if (x % 2 == 0 || x < 3) {
printf("Khong ve duoc");
return;
}
int mid = x / 2;
int left = mid, right = mid;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x; j++)
{
if (j == mid || j >= left && j <= right) printf("*");
else printf(" ");
}
printf("\n");
if (i < mid) {
left--;
right++;
}
else {
left++;
right--;
}
}
}
*
***
*****
*******
*****
***
*
void veHinhThoiRong(int x) {
if (x % 2 == 0 || x < 3) {
printf("Khong ve duoc");
return;
}
int mid = x / 2;
int left = mid, right = mid;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < x; j++)
{
if (j == mid && (i == 0 || i == x-1)) printf("*");
else if(j == left || j == right) printf("*");
else printf(" ");
}
printf("\n");
if (i < mid) {
left--;
right++;
}
else {
left++;
right--;
}
}
}
*
* *
* *
* *
* *
* *
*
3) Hình Bình Hành
void veHinhBinhHanh(int dong, int cot) {
int tmp = dong;
for (int i = 0; i < dong; i++)
{
for (int l = 0; l < tmp; l++)
{
printf(" ");
}
tmp--;
for (int j = 0; j < cot; j++)
{
printf("*");
}
printf("\n");
}
}
*******
*******
*******
*******
void veHinhBinhHanhRong(int dong, int cot) {
int tmp = dong;
for (int i = 0; i < dong; i++)
{
for (int l = 0; l < tmp; l++)
{
printf(" ");
}
tmp--;
for (int j = 0; j < cot; j++)
{
if (i == 0 || i == dong - 1) printf("*");
else if (j == 0 || j == cot - 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
*******
* *
* *
*******
void veHinhBinhHanhNguoc(int dong, int cot) {
int tmp = 0;
for (int i = 0; i < dong; i++)
{
for (int l = 0; l < tmp; l++)
{
printf(" ");
}
if (tmp < dong) tmp++;
for (int j = 0; j < cot; j++)
{
printf("*");
}
printf("\n");
}
}
*******
*******
*******
*******
void veHinhBinhHanhNguocRong(int dong, int cot) {
int tmp = 0;
for (int i = 0; i < dong; i++)
{
for (int l = 0; l < tmp; l++)
{
printf(" ");
}
if (tmp < dong) tmp++;
for (int j = 0; j < cot; j++)
{
if (i == 0 || i == dong - 1) printf("*");
else if (j == 0 || j == cot - 1) printf("*");
else printf(" ");
}
printf("\n");
}
}
*******
* *
* *
*******
Cảm ơn các bạn đã xem hết bài viết nếu thấy bổ ích hãy cùng chia sẻ.
2 Nhận xét
Hay dữ ta
Trả lờiXóaQuá ghê
Trả lờiXóa