这是我为学校所做的一个项目。我有两个问题,我一直无法解决。它们出现在admindistance和distancefunc函数中。我无法弄清楚为什么距离计算不准确。这会导致功能无法正常工作。秒问题是,在admindistance函数它应该打印距离,但这个printf语句似乎不工作。感谢任何人都能提供的帮助。Printf无法在某些点工作,无法找出公式
#include <stdio.h>
#include <time.h>
#include <math.h>
int generatenum (int num)
{
return rand() % num + 1;
}
char admin (int nrow, int ncolumn)
{
char bypass;
printf ("Anything to tell me?\n");
scanf (" %c", &bypass);
if (bypass == 'A')
{
printf ("Oh, your an administrator\n");
printf ("The random location is %i,%i\n", nrow, ncolumn);
return bypass;
}
else
{
}
}
float admindistance (int nrow, int ncolumn)
{
float grow,gcolumn,distance,rowdifference,columndifference,rowpower,columnpower,oldistance;
_Bool foundtarget=0;
printf ("What is your first guess?");
scanf ("%i%i", &grow, &gcolumn);
rowdifference = nrow - grow;
rowpower = pow (rowdifference, 2.0);
columndifference = ncolumn - gcolumn;
columnpower = pow (columndifference, 2.0);
distance = sqrt (rowdifference - columndifference);
printf ("Distance is %f\n", distance);
while (!foundtarget)
{
printf ("What is your next guess?");
scanf ("%i%i", &grow, &gcolumn);
if (grow >= nrow)
{
printf ("Error: Value entered is outside array size");
}
else if (gcolumn >= ncolumn)
{
printf ("Error: Value entered is outside array size");
}
else
{
/* CONTINUE */
}
rowdifference = nrow - grow;
rowpower = pow (rowdifference, 2.0);
columndifference = ncolumn - gcolumn;
columnpower = pow (columndifference, 2.0);
distance = sqrt (rowdifference - columndifference);
printf ("Distance is %f\n", distance);
if (nrow == grow && ncolumn == gcolumn)
{
foundtarget = 1;
printf ("Congrats you win!");
}
else if (nrow - grow <= 1 && ncolumn - gcolumn <= 1)
{
printf ("YOU ARE ON FIRE!\n");
}
else if (oldistance <= distance)
{
printf ("You are getting colder\n");
}
else
{
printf ("You are getting hotter\n");
}
oldistance = distance;
}
return 0;
}
float distancefunc (int nrow, int ncolumn)
{
float grow,gcolumn,distance,rowdifference,columndifference,rowpower,columnpower,oldistance;
_Bool foundtarget = 0;
printf ("What is your first guess?");
scanf ("%i%i", &grow, &gcolumn);
while (!foundtarget)
{
printf ("What is your next guess?");
scanf ("%i%i", &grow, &gcolumn);
if (grow >= nrow)
{
printf ("Error: Value entered is outside array size");
}
else if (gcolumn >= ncolumn)
{
printf ("Error: Value entered is outside array size");
}
else
{
/* CONTINUE */
}
rowdifference = nrow - grow;
rowpower = pow (rowdifference, 2.0);
columndifference = ncolumn - gcolumn;
columnpower = pow (columndifference, 2.0);
distance = sqrt (rowdifference - columndifference);
if (nrow == grow && ncolumn == gcolumn)
{
foundtarget = 1;
printf ("Congrats you win!");
}
else if (nrow - grow <= 1 && ncolumn - gcolumn <= 1)
{
printf ("YOU ARE ON FIRE!");
}
else if (oldistance <= distance)
{
printf ("You are getting colder");
}
else
{
printf ("You are getting hotter");
}
oldistance = distance;
}
return 0;
}
int main (char bypass)
{
int row, column,nrow,ncolumn,grow,gcolumn,admininput;
printf ("Welcome to the Hotter-Colder Game!\n\n");
printf ("How many rows and columns are in the grid?\n\n");
scanf ("%i%i", &row, &column);
time_t seconds = time (NULL);
int seed = (unsigned)(seconds);
srand (seed);
int i = 0;
nrow = generatenum (row);
ncolumn = generatenum (column);
admin (nrow, ncolumn);
if (admininput == bypass)
{
admindistance (nrow, ncolumn);
}
else
{
distancefunc (nrow, ncolumn);
}
return 0;
}
“这似乎不起作用”。你的意思是它什么都不打印 –
是的,那是我遇到的问题 – AustinPavlas