0

我有一个 ac 程序,它使用扩展的 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但我错了。我所做的一切都在底部边框下方打印右侧边框。

我的目标是最终允许您传入第三个参数,以允许您说创建顶部、底部、右侧而不是左侧。因此,为什么我要尝试将每个边框都作为一个功能。

任何帮助表示赞赏。

4

1 回答 1

0

I think you should handle both sides (left and right) simultaneously... Print the left border, fill with spaces until you reach the right border and print that as well.

Should you need to omit either one of the two borders, you just print spaces instead of the ASCII characters you chose..

I believe you could follow the same approach for the top - bottom borders as well, but in that case it also works just fine as you have done it

于 2015-04-11T00:43:50.543 回答