C 试图理解为什么我的脚本会重复
我正在参加一个很少有人支持的在线课程。我正试图按照说明编写一个脚本,它接受输入并绘制两个矩形。不幸的是,它只是无限重复,我不知道我错过了什么。任何指导都会很有帮助!非常感谢您的帮助和时间,这是我的第一篇文章,我为任何格式错误道歉C 试图理解为什么我的脚本会重复,c,C,我正在参加一个很少有人支持的在线课程。我正试图按照说明编写一个脚本,它接受输入并绘制两个矩形。不幸的是,它只是无限重复,我不知道我错过了什么。任何指导都会很有帮助!非常感谢您的帮助和时间,这是我的第一篇文章,我为任何格式错误道歉 #include <stdio.h> #include <stdlib.h> /* * Determines if coord is in range between * offset (INCLUSIVE) and offset +
#include <stdio.h>
#include <stdlib.h>
/*
* Determines if coord is in range between
* offset (INCLUSIVE) and offset + size (EXCLUSIVE)
*/
int isInRange(int coord, int offset, int size) {
// if coord is in range, return 1
if ((coord >= offset) && (coord < (offset + size))) {
return 1;
}
// else, return 0
else {
return 0;
}
return 0;
}
/*
* Determines if coord is at border of offset or
* offset + size
*/
int isAtBorder(int coord, int offset, int size) {
// if coord is equal to offest or offset + size
if (coord == offset || (offset + size)) {
return 1;
}
// return 1, else return 0
else {
return 0;
}
return 0;
}
void squares(int size1, int x_offset, int y_offset, int size2) {
//compute the max of size1 and (x_offset + size2). Call this w
int w = (size1 + (x_offset + size2));
//compute the max of size1 and (y_offset + size2). Call this h
int h = (size1 + (y_offset + size2));
//count from 0 to h. Call the number you count with y
for (int y = 0; y < h; h++) {
//count from 0 to w. Call the number you count with x
for (int x = 0; x < w; x++) {
//check if EITHER
// ((x is between x_offset and x_offset +size2) AND
if (((isInRange(x, x_offset, size2) == 1) &&
// y is equal to either y_offset OR y_offset + size2 - 1)
(isAtBorder(y, y_offset, size2 - 1) == 1))
// OR
||
// ((y is between y_offset and y_offset + size2) AND
((isInRange(y, y_offset, size2) == 1) &&
// x is equal to either x_offset OR x_offset + size2 -1)
(isAtBorder(x, x_offset, size2-1)))) {
// if so, print a *
printf ("*");
}
//if not,
// check if EITHER
// x is less than size1 AND (y is either 0 or size1-1)
else {
if (((x < size1) && (isAtBorder(y, 0, size1 - 1) == 1))
// OR
||
// y is less than size1 AND (x is either 0 or size1-1)
((y < size1) && (isAtBorder(x, 0, size1 - 1) == 1))) {
//if so, print a #
printf ("#");
}
//else print a space
else {
printf (" ");
}
}
}
//when you finish counting x from 0 to w,
//print a newline
printf ("\n");
}
}
#包括
#包括
/*
*确定坐标是否在
*偏移量(含)和偏移量+大小(不含)
*/
整数isInRange(整数坐标、整数偏移、整数大小){
//如果坐标在范围内,则返回1
如果((坐标>=偏移量)&(坐标<(偏移量+大小))){
返回1;
}
//否则,返回0
否则{
返回0;
}
返回0;
}
/*
*确定坐标是否位于偏移或偏移的边界处
*偏移量+大小
*/
int isAtBorder(int坐标、int偏移、int大小){
//如果坐标等于offest或offset+大小
如果(坐标==偏移量| |(偏移量+大小)){
返回1;
}
//返回1,否则返回0
否则{
返回0;
}
返回0;
}
空方块(整数大小1、整数x_偏移、整数y_偏移、整数大小2){
//计算size1和(x_offset+size2)的最大值。将其称为w
int w=(尺寸1+(x_偏移量+尺寸2));
//计算size1和(y_offset+size2)的最大值。将其称为h
int h=(大小1+(y_偏移量+大小2));
//从0数到h。用y数到你的号码
对于(int y=0;y
我想,真正的问题是平方函数。检查此代码段中的for循环:
for (int y = 0; y < h; h++) {
//count from 0 to w. Call the number you count with x
for (int x = 0; x < w; x++) {
在if语句中始终为true,因此该函数基本上总是返回1。请提供完整的代码作为参考。还有,你调试过代码了吗?通常的方法是在调试器中运行代码。它会告诉你代码在做什么,你可以检查状态来找出为什么它在循环中。第一个是
isAtBorder()
中的| |(偏移量+大小)
,第二个是h++
中的for
循环中的squares()
。
int isAtBorder(int coord, int offset, int size) {
// if coord is equal to offest or offset + size
if (coord == offset || (offset + size)) {
return 1;
}
// return 1, else return 0
else {
return 0;
}
return 0;
}