C语言C 90打印许多广场的副本彼此相邻

发布于 2025-01-18 01:06:35 字数 1154 浏览 2 评论 0原文

我创建了一个程序,该程序在用户的输入中打印一个特定维度的正方形并使用特定字符。我的目标是打印此形状的多个副本(用户将每次确定副本的数量),每个副本数量)其他。我尝试在Main中使用for循环,但副本是垂直打印的。我还考虑打印形状的第一行,第二行等,但我不知道如何将其应用于我的代码。 有帮助吗?

#include <stdio.h>

int getsize(void);
char get_char(void);
void printsquare(int size,char ch);

int main(){
    int size;
    char ch;

    size=getsize();
    ch=get_char();
    printsquare(size,ch);

}
int getsize(void){
    int size;
    printf("Enter the size of selected shape: ");
    scanf("%d",&size);
    return size ;
}

char get_char(void){
    char character;
    printf("Enter the character of selected shape: ");
    scanf("\n%c",&character);
    return character;
}

void printsquare(int size,char ch){
    int i,j;
    for (i=1;i<=size;i++){
        for (j=1;j<i;j++){
            printf("-");

        }
        if (i==1 || i==size){
            for (j=1;j<=size;j++){
                printf("%c",ch);
            }
        }
        else{
            printf("%c",ch);
            for (j=1;j<=size-2;j++){
                printf("-");
            }
            printf("%c",ch);
        }
        printf("\n");
    }
    return ;
}

I have created a program which prints a square of specific dimensions and uses a particular character,based on user's input.My goal is to print multiple copies of this shape(the user will determine the number of copies each time),each next to each other.I tried using a for loop in main,but the copies are printed vertically.I also thought of printing the first line of the shape,the second one etc, but I don't know how to apply this to my code.
Any help?

#include <stdio.h>

int getsize(void);
char get_char(void);
void printsquare(int size,char ch);

int main(){
    int size;
    char ch;

    size=getsize();
    ch=get_char();
    printsquare(size,ch);

}
int getsize(void){
    int size;
    printf("Enter the size of selected shape: ");
    scanf("%d",&size);
    return size ;
}

char get_char(void){
    char character;
    printf("Enter the character of selected shape: ");
    scanf("\n%c",&character);
    return character;
}

void printsquare(int size,char ch){
    int i,j;
    for (i=1;i<=size;i++){
        for (j=1;j<i;j++){
            printf("-");

        }
        if (i==1 || i==size){
            for (j=1;j<=size;j++){
                printf("%c",ch);
            }
        }
        else{
            printf("%c",ch);
            for (j=1;j<=size-2;j++){
                printf("-");
            }
            printf("%c",ch);
        }
        printf("\n");
    }
    return ;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2025-01-25 01:06:35

请记住始终尝试简单思考,以下代码显示了 printsquare

void printsquare(int size,char ch){
    int i;
    int j;
    // print first line
    for (i= 0; i < size; i++) {
        putchar(ch);
    }
    putchar('\n');
    // print inner line
    for (i= 0; i < size; i++) {
        putchar(ch);
        for (j = 1; j < size - 1; j++) {
            putchar('-');
        }
        putchar(ch);
        putchar('\n');
    }
    // print last line
    // print first line
    for (i= 0; i < size; i++) {
        putchar(ch);
    }
    putchar('\n');
}

另一个实现

void printsquare(int size,char ch){
    int i;
    int j;
    for (i= 0; i < size; i++) {
        if (i == 0 || i == size - 1) {
            // print last and first line
            for (j = 0; j < size; j++) {
                putchar(ch);
            }
            putchar('\n');
        }
        else {
            // print inner lines
            putchar(ch);
            for (j = 1; j < size - 1; j++) {
                putchar('-');
            }
            putchar(ch);
            putchar('\n');
        }
    }
}

现在必须是多个副本的

void printsquare(int size,char ch, int copies){
    int i;
    int j;
    int c;
    for (i= 0; i < size; i++) {
        if (i == 0 || i == size - 1) {
            for (c = 0; c < copies; c++) {
                // print last and first line
                for (j = 0; j < size; j++) {
                    putchar(ch);
                }
                putchar(' ');
            }
            putchar('\n');
        }
        else {
            for (c = 0; c < copies; c++) {
                // print inner lines
                putchar(ch);
                for (j = 1; j < size - 1; j++) {
                    putchar('-');
                }
                putchar(ch);
                putchar(' ');
            }
            putchar('\n');
        }
    }
}

remember always try think simple, the following code show how must printsquare must be

void printsquare(int size,char ch){
    int i;
    int j;
    // print first line
    for (i= 0; i < size; i++) {
        putchar(ch);
    }
    putchar('\n');
    // print inner line
    for (i= 0; i < size; i++) {
        putchar(ch);
        for (j = 1; j < size - 1; j++) {
            putchar('-');
        }
        putchar(ch);
        putchar('\n');
    }
    // print last line
    // print first line
    for (i= 0; i < size; i++) {
        putchar(ch);
    }
    putchar('\n');
}

another implementation

void printsquare(int size,char ch){
    int i;
    int j;
    for (i= 0; i < size; i++) {
        if (i == 0 || i == size - 1) {
            // print last and first line
            for (j = 0; j < size; j++) {
                putchar(ch);
            }
            putchar('\n');
        }
        else {
            // print inner lines
            putchar(ch);
            for (j = 1; j < size - 1; j++) {
                putchar('-');
            }
            putchar(ch);
            putchar('\n');
        }
    }
}

now for multiple copies

void printsquare(int size,char ch, int copies){
    int i;
    int j;
    int c;
    for (i= 0; i < size; i++) {
        if (i == 0 || i == size - 1) {
            for (c = 0; c < copies; c++) {
                // print last and first line
                for (j = 0; j < size; j++) {
                    putchar(ch);
                }
                putchar(' ');
            }
            putchar('\n');
        }
        else {
            for (c = 0; c < copies; c++) {
                // print inner lines
                putchar(ch);
                for (j = 1; j < size - 1; j++) {
                    putchar('-');
                }
                putchar(ch);
                putchar(' ');
            }
            putchar('\n');
        }
    }
}
§普罗旺斯的薰衣草 2025-01-25 01:06:35

一个简单的解决方案是尽可能多次重复每条线的整个打印逻辑。

void printsquare(int size,char ch, int reps) {
    int i,j;
    for (i=1;i<=size;i++) {
        for (int k = 0; k < reps; k++) {
            for (j=1;j<i;j++){
                printf("-");

            }
            if (i==1 || i==size){
                for (j=1;j<=size;j++){
                    printf("%c",ch);
                }
            }
            else{
                printf("%c",ch);
                for (j=1;j<=size-2;j++){
                    printf("-");
                }
                printf("%c",ch);
            }

            putchar(' ');
        }

        printf("\n");
    }
    return ;
}

我添加了一个以区分形状的空间,但这可能是另一个破折号,或者根本没有。

printsquare(4,'@',2)给出stdout

@@@@ @@@@ 
-@--@ -@--@ 
--@--@ --@--@ 
---@@@@ ---@@@@ 

A simple solution is to repeat the entire printing logic of each line as many times as you need.

void printsquare(int size,char ch, int reps) {
    int i,j;
    for (i=1;i<=size;i++) {
        for (int k = 0; k < reps; k++) {
            for (j=1;j<i;j++){
                printf("-");

            }
            if (i==1 || i==size){
                for (j=1;j<=size;j++){
                    printf("%c",ch);
                }
            }
            else{
                printf("%c",ch);
                for (j=1;j<=size-2;j++){
                    printf("-");
                }
                printf("%c",ch);
            }

            putchar(' ');
        }

        printf("\n");
    }
    return ;
}

I have added a space to differentiate the shapes, but it could be another dash, or nothing at all.

printsquare(4, '@', 2) gives the stdout:

@@@@ @@@@ 
-@--@ -@--@ 
--@--@ --@--@ 
---@@@@ ---@@@@ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文