c语言 4-22 显示出一个横向较长的长方形
1、for语句
#include <stdio.h>
int main(void)
{
int i, j, side1, side2;
puts("please input the sides.");
do
{
printf("side1 = "); scanf("%d", &side1);
printf("side2 = "); scanf("%d", &side2);
if (side1 == side2)
puts("side1 == side2!");
}
while (side1 == side2);
if (side1 >= side2)
{
for(i = 1; i <= side2; i++)
{
for(j = 1; j <= side1; j++)
{
putchar('*');
}
putchar('\n');
}
}
else
{
for(i = 1; i <= side1; i++)
{
for(j = 1; j <= side2; j++)
{
putchar('*');
}
putchar('\n');
}
}
return 0;
}
2、for语句
#include <stdio.h>
int main(void)
{
int i, j, side1, side2, shorter, longer;
puts("please input the sides.");
do
{
printf("side1 = "); scanf("%d", &side1);
printf("side2 = "); scanf("%d", &side2);
if(side1 == side2)
puts("side1 == side2!");
}
while(side1 == side2);
shorter = side1 > side2 ? side2:side1;
longer = side1 > side2 ? side1:side2;
for(i = 1; i <= shorter; i++)
{
for(j = 1; j <= longer; j++)
{
putchar('*');
}
putchar('\n');
}
return 0;
}
3、
#include <stdio.h>
int main(void)
{
int i, j, height, width, tmp;
puts("please input the height and width.");
do
{
printf("height = "); scanf("%d", &height);
printf("width = "); scanf("%d", &width);
if (height == width)
puts("height == width!");
}
while (height == width);
if (height > width)
{
tmp = height;
height = width;
width = tmp;
}
for(i = 1; i <= height; i++)
{
for(j = 1; j <= width; j++)
{
putchar('*');
}
putchar('\n');
}
return 0;
}