C语言C 90打印许多广场的副本彼此相邻
我创建了一个程序,该程序在用户的输入中打印一个特定维度的正方形并使用特定字符。我的目标是打印此形状的多个副本(用户将每次确定副本的数量),每个副本数量)其他。我尝试在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住始终尝试简单思考,以下代码显示了
printsquare
另一个实现
现在必须是多个副本的
remember always try think simple, the following code show how must
printsquare
must beanother implementation
now for multiple copies
一个简单的解决方案是尽可能多次重复每条线的整个打印逻辑。
我添加了一个以区分形状的空间,但这可能是另一个破折号,或者根本没有。
printsquare(4,'@',2)
给出stdout
:A simple solution is to repeat the entire printing logic of each line as many times as you need.
I have added a space to differentiate the shapes, but it could be another dash, or nothing at all.
printsquare(4, '@', 2)
gives thestdout
: