如何打印边框的另一侧?
How do I get the other side of the border to print?
我有一个 c 程序,它使用扩展的 ascii 根据您输入的两个参数打印出一个框,一个是宽度,另一个是高度。
目前我可以让我的盒子看起来像:
╔══════════╗
║
║
║
║
║
║
║
║
╚══════════╝
但我不知道如何在框的右侧做边框。我有以下内容:
box_ascii.h
#ifndef __box_ascii_h__
#define __box_ascii_h__
// Might not be exact Extended Ascii Characters but they come from:
// https://en.wikipedia.org/wiki/Box-drawing_character#Unicode
#define BOX_TOP_LEFT_CORNER "\u2554" // ╔
#define BOX_TOP_RIGHT_CORNER "\u2557" // ╗
#define BOX_BOTTOM_LEFT_CORNER "\u255A" // ╚
#define BOX_BOTTOM_RIGHT_CORNER "\u255D" // ╝
#define BOX_SIDE "\u2551" // ║
#define BOX_TOP_BOTTOM "\u2550" // ═
#endif
create_dynamic_box.c
#include <stdio.h>
#include <stdlib.h>
#include "box_ascii.h"
void print_border(int width, int height);
void print_top_border(int row, int col, int width, int height);
void print_left_side_border(int row, int width);
void print_bottom_border(int row, int col, int width, int height);
// Main App.
int main(int argc, char *argv[]) {
if(argc < 3) {
printf("Mustenter width and height.\n");
return -1;
}
// Print the actual border.
print_border(atoi(argv[1]), atoi(argv[2]));
return 0;
}
// We want to print the actual box border.
void print_border(int width, int height) {
int row = 0;
int col = 0;
print_top_border(row, col, width, height);
print_left_side_border(row, width);
print_bottom_border(row, col, width, height);
}
// This is the top part of the border.
void print_top_border(int row, int col, int width, int height) {
for(row = 0; row < width; row ++) {
for(col = 0; col < height; col++) {
if(row == 0 && col == 0) {
printf("%s", BOX_TOP_LEFT_CORNER);
}
if(row == 0 && col < height) {
printf("%s", BOX_TOP_BOTTOM);
}
if(row == 0 && col == height - 1) {
printf("%s\n", BOX_TOP_RIGHT_CORNER);
}
}
}
}
// Print the left side of the border.
void print_left_side_border(int row, int width) {
for(row = 0; row < width; row ++) {
if(row < width -2) {
printf("%s\n", BOX_SIDE);
}
}
}
// This is the bottom part of the border.
void print_bottom_border(int row, int col, int width, int height) {
for(row = 0; row < width; row ++) {
for(col = 0; col < height; col++) {
if(row == width - 1 && col == 0) {
printf("%s", BOX_BOTTOM_LEFT_CORNER);
}
if(row == width - 1 && col < height) {
printf("%s", BOX_TOP_BOTTOM);
}
if(row == width - 1 && col == height - 1) {
printf("%s\n", BOX_BOTTOM_RIGHT_CORNER);
}
}
}
}
除了 print_right_side_border
我认为与 print_left_side_border
相同但我错了。我所做的一切都在底部边框下方打印了右侧边框。
我的目标是最终允许您传入第三个参数,让您可以说创建顶部、底部、右侧但不是左侧。因此,为什么我要尝试将每个边框作为一个函数来做。
感谢任何帮助。
我认为你应该同时处理两侧(左侧和右侧)...
打印左边框,填充空格直到到达右边框并打印它。
如果您需要省略两个边框中的任何一个,您只需打印空格而不是您选择的 ASCII 字符..
我相信您也可以对顶部和底部边框采用相同的方法,但在那种情况下,它也可以像您所做的那样正常工作
我有一个 c 程序,它使用扩展的 ascii 根据您输入的两个参数打印出一个框,一个是宽度,另一个是高度。
目前我可以让我的盒子看起来像:
╔══════════╗
║
║
║
║
║
║
║
║
╚══════════╝
但我不知道如何在框的右侧做边框。我有以下内容:
box_ascii.h
#ifndef __box_ascii_h__
#define __box_ascii_h__
// Might not be exact Extended Ascii Characters but they come from:
// https://en.wikipedia.org/wiki/Box-drawing_character#Unicode
#define BOX_TOP_LEFT_CORNER "\u2554" // ╔
#define BOX_TOP_RIGHT_CORNER "\u2557" // ╗
#define BOX_BOTTOM_LEFT_CORNER "\u255A" // ╚
#define BOX_BOTTOM_RIGHT_CORNER "\u255D" // ╝
#define BOX_SIDE "\u2551" // ║
#define BOX_TOP_BOTTOM "\u2550" // ═
#endif
create_dynamic_box.c
#include <stdio.h>
#include <stdlib.h>
#include "box_ascii.h"
void print_border(int width, int height);
void print_top_border(int row, int col, int width, int height);
void print_left_side_border(int row, int width);
void print_bottom_border(int row, int col, int width, int height);
// Main App.
int main(int argc, char *argv[]) {
if(argc < 3) {
printf("Mustenter width and height.\n");
return -1;
}
// Print the actual border.
print_border(atoi(argv[1]), atoi(argv[2]));
return 0;
}
// We want to print the actual box border.
void print_border(int width, int height) {
int row = 0;
int col = 0;
print_top_border(row, col, width, height);
print_left_side_border(row, width);
print_bottom_border(row, col, width, height);
}
// This is the top part of the border.
void print_top_border(int row, int col, int width, int height) {
for(row = 0; row < width; row ++) {
for(col = 0; col < height; col++) {
if(row == 0 && col == 0) {
printf("%s", BOX_TOP_LEFT_CORNER);
}
if(row == 0 && col < height) {
printf("%s", BOX_TOP_BOTTOM);
}
if(row == 0 && col == height - 1) {
printf("%s\n", BOX_TOP_RIGHT_CORNER);
}
}
}
}
// Print the left side of the border.
void print_left_side_border(int row, int width) {
for(row = 0; row < width; row ++) {
if(row < width -2) {
printf("%s\n", BOX_SIDE);
}
}
}
// This is the bottom part of the border.
void print_bottom_border(int row, int col, int width, int height) {
for(row = 0; row < width; row ++) {
for(col = 0; col < height; col++) {
if(row == width - 1 && col == 0) {
printf("%s", BOX_BOTTOM_LEFT_CORNER);
}
if(row == width - 1 && col < height) {
printf("%s", BOX_TOP_BOTTOM);
}
if(row == width - 1 && col == height - 1) {
printf("%s\n", BOX_BOTTOM_RIGHT_CORNER);
}
}
}
}
除了 print_right_side_border
我认为与 print_left_side_border
相同但我错了。我所做的一切都在底部边框下方打印了右侧边框。
我的目标是最终允许您传入第三个参数,让您可以说创建顶部、底部、右侧但不是左侧。因此,为什么我要尝试将每个边框作为一个函数来做。
感谢任何帮助。
我认为你应该同时处理两侧(左侧和右侧)... 打印左边框,填充空格直到到达右边框并打印它。
如果您需要省略两个边框中的任何一个,您只需打印空格而不是您选择的 ASCII 字符..
我相信您也可以对顶部和底部边框采用相同的方法,但在那种情况下,它也可以像您所做的那样正常工作