這是我爲學校所做的一個項目。我有兩個問題,我一直無法解決。它們出現在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