gpt4 book ai didi

c - Printf 在某些时候不起作用并且无法计算出公式

转载 作者:行者123 更新时间:2023-11-30 16:56:18 27 4
gpt4 key购买 nike

这是我为学校做的一个项目。我有两个问题一直无法解决。它们出现在 admindistance 和 distancefunc 函数中。我不明白为什么距离没有被正确计算。这会导致功能无法完全发挥作用。秒问题是在 admindistance 函数中应该打印距离,但是这个 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;
}

最佳答案

您计算rowpowercolumnpower,然后不使用它们。

rowdifference = nrow - grow;
rowpower = pow(rowdifference,2.0);
columndifference = ncolumn - gcolumn;
columnpower = pow(columndifference,2.0);
distance = sqrt(rowdifference - columndifference);

我认为最后一行应该是

distance = sqrt(rowpower + columnpower);

The seconds issue is that in the admindistance function it is supposed to print the distance, but this printf statement does not seem to work.

这里的 printf 语句本身没有任何问题,所以我假设它是由于相同的错误导致您的距离没有被打印。

关于c - Printf 在某些时候不起作用并且无法计算出公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39948316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com