通过获取以前的位置来计算位移。(C)
我试图弄清楚如何从我编写的函数中获得以前的位置。我花了几个小时试图解决这个问题,但没能成功。敌人的(N)初始位置是(0,0),之后每x和y值随机生成一个。”O'是我的基地,它位于(6,6)。边界为11x11。问题是refresh_position函数总是通过将previus位置设置为“0”来计算位移。代码如下:通过获取以前的位置来计算位移。(C),c,C,我试图弄清楚如何从我编写的函数中获得以前的位置。我花了几个小时试图解决这个问题,但没能成功。敌人的(N)初始位置是(0,0),之后每x和y值随机生成一个。”O'是我的基地,它位于(6,6)。边界为11x11。问题是refresh_position函数总是通过将previus位置设置为“0”来计算位移。代码如下: #include <stdlib.h> #include <stdio.h> #include <time.h> #include <math.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
void track_machine();
void refresh_position(int *X, int *Y, double *D, double *R);
void main()
{
track_machine();
}
void track_machine()
{
int x=0, y=0;
char op;
double D, R;
refresh_position(&x, &y, &D, &R);
for(int i=1; i<=11; i++)
{
for(int j=1; j<=11; j++)
{
if(i==x && j==y)
printf("N ");
else if(i==6 && j==6)
printf("O ");
else
printf(". ");
}
printf("\n");
}
printf("Enemies X position:%d\nY position:%d\nDisplacement:%f\nDistance to our camp:%f\nCommand waiting...:\n", x, y, D, R);
scanf(" %c", &op);
if(op=='R')
track_machine();
else if(op=='E')
main();
else
printf("\nERROR!\n");
}
void refresh_position(int *X, int *Y, double *D, double *R)
{
int x=*X, y=*Y, xD, yD, xR, yR;
srand(time(0));
while( (*X==0 || *X==6) || (*Y==0 || *Y==6) )
{
*X = rand()%11;
*Y = rand()%11;
}
xD=abs(*X-x);
yD=abs(*Y-y);
xR=abs(*X-6);
yR=abs(*Y-6);
*D = sqrt( (xD*xD) + (yD*yD) );
*R = sqrt( (xR*xR) + (yR*yR) );
}
#包括
#包括
#包括
#包括
无效轨道_机器();
无效刷新_位置(int*X,int*Y,double*D,double*R);
void main()
{
履带式机器();
}
无效轨道_机器()
{
int x=0,y=0;
char op;
双D,R;
刷新位置(&x、y、D和R);
对于(int i=1;i
我认为,这是一种棋盘游戏,棋盘位置从1到11编号。我从循环得出的结论是,您不希望*X
&*Y
为“0”或“6”。但是,使用mod(11)w/o添加(增量)“1”永远不会产生随机结果“11”。因此,您最好将该循环更改为更有效的do while
,如
这样,rand()%11
将生成一个数字b/w 0-10(包括),然后添加“1”将生成一个数字b/w 1-11(包括)。您将不再检查“0”值
回答您关于代码的问题(不确定我是否100%理解了您的问题);我认为问题产生于没有参数的函数track\u machine
。在main
中初始化x
&y
,然后通过参数将这些变量传递给函数将解决您的问题(当然,如果我真正理解了您的问题)
我没有很好地描述我的问题。很抱歉,我发现了我的错误。代码在x和y方向上打印了11个点。我的目标是找到N(随机位于11x11区域中的每个“R”命令)和O(位于6,6)之间的距离当我发出“R”命令时,求出每次N改变位置时的位移。以下是工作代码:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
void track_machine();
void refresh_position(int *X, int *Y, double *D, double *R)
int main()
{
track_machine();
}
void track_machine()
{
int x=1, y=1;
char op;
double D, R;
srand(time(0));
D = 0;
R = 8.49;
while(1==1){
for(int i=1; i<=11; i++){
for(int j=1; j<=11; j++){
if(i==x && j==y)
printf("N ");
else if(i==6 && j==6)
printf("O ");
else
printf(". ");
}
printf("\n");
}
printf("Enemies X position:%d\nY position:%d\nDisplacement:%f\nDistance to our camp:%f\nCommand waiting...:\n", x, y, D, R);
scanf(" %c", &op);
if(op=='R')
refresh_position(&x, &y, &D, &R);
else if(op=='E'){
main();
break;
}
else
printf("\nERROR!\n");
}
}
void refresh_position(int *X, int *Y, double *D, double *R)
{
int x=*X, y=*Y;
do {
*X = rand()%11+1;
*Y = rand()%11+1;
} while((*X == 6) || (*Y == 6));
*D = sqrt( ((*X-x)*(*X-x)) + ((*Y-y)*(*Y-y)) );
*R = sqrt( ((*X-6)*(*X-6)) + ((*Y-6)*(*Y-6)) );
}
#包括
#包括
#包括
#包括
无效轨道_机器();
无效刷新位置(int*X,int*Y,double*D,double*R)
int main()
{
履带式机器();
}
无效轨道_机器()
{
int x=1,y=1;
char op;
双D,R;
srand(时间(0));
D=0;
R=8.49;
而(1==1){
对于(int i=1;iSeedsrand(time(0));
应该在main()
中使用,否则它会在每次函数调用时重新启动。哦,我没有注意到。修复了它,谢谢,但这不是主要问题:(我怀疑你想要而(!(*X==0&*Y==0)|(*Y==6&*Y==6))
。可能还有其他错误。因为你调用main()在您的一个选项中,问题仍然存在,而且R和E是相同的,因为在这个实例中调用main()或track_machine()是相同的,它应该返回int。至于您的问题,我将不得不把它留给其他用户,因为我现在无法测试它。@anastaciu:should-->yes.Required-->No.while条件..}而((*X==6)|(*Y==6));
在代码中;如果您不想让“N”位于第6行(独立于列)或者不位于第6列(独立于行),则可以。但是,如果您的目标是避免“N”位于“O”之上,则您的条件应该是…},而((*X==6)和(&&(*Y==6));
。
// main should be
int main()
{
int x = 1, y = 1;
track_machine(x, y);
}
// function decoration should be
void track_machine(int x, int y) {...}
// and your recursive call line should read as
if (op == 'R')
track_machine(x, y);
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
void track_machine();
void refresh_position(int *X, int *Y, double *D, double *R)
int main()
{
track_machine();
}
void track_machine()
{
int x=1, y=1;
char op;
double D, R;
srand(time(0));
D = 0;
R = 8.49;
while(1==1){
for(int i=1; i<=11; i++){
for(int j=1; j<=11; j++){
if(i==x && j==y)
printf("N ");
else if(i==6 && j==6)
printf("O ");
else
printf(". ");
}
printf("\n");
}
printf("Enemies X position:%d\nY position:%d\nDisplacement:%f\nDistance to our camp:%f\nCommand waiting...:\n", x, y, D, R);
scanf(" %c", &op);
if(op=='R')
refresh_position(&x, &y, &D, &R);
else if(op=='E'){
main();
break;
}
else
printf("\nERROR!\n");
}
}
void refresh_position(int *X, int *Y, double *D, double *R)
{
int x=*X, y=*Y;
do {
*X = rand()%11+1;
*Y = rand()%11+1;
} while((*X == 6) || (*Y == 6));
*D = sqrt( ((*X-x)*(*X-x)) + ((*Y-y)*(*Y-y)) );
*R = sqrt( ((*X-6)*(*X-6)) + ((*Y-6)*(*Y-6)) );
}