gpt4 book ai didi

c - getPositiveValues 不会返回值

转载 作者:行者123 更新时间:2023-11-30 14:45:45 24 4
gpt4 key购买 nike

什么不起作用:

在下面的代码中,在getPositiveValue下的scanf中输入的值将不会返回。无论输入是什么,它们都会返回 0

我不知道如何解决这个问题。有人可以告诉我为什么它不起作用吗?

我尝试过的:

我尝试使用 return CHAN; 甚至 return CHAN.n; 以及所有其他成员,但这不起作用。

我的代码:

#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines

typedef struct CHANNEL_ //Structure CHANNEL
{
char name[9];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;

double computeVelocity(CHANNEL, double);

int main(void)
{
CHANNEL CHAN;
void getPositiveValue(CHANNEL);
void displayTable(CHANNEL);
//Function declarations

printf("Enter the name of the channel: ");
fgets(CHAN.name, 9, stdin);
getPositiveValue(CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}

void getPositiveValue(CHANNEL CHAN)
{
int Flag; //sentinel
do
{
Flag = FALSE;
printf("Give the coefficient for roughness, slope, width, and maxdepth: ");
scanf("%lf %lf %lf %lf", &CHAN.n, &CHAN.S, &CHAN.B, &CHAN.D);
if(CHAN.n < 0 || CHAN.S < 0 || CHAN.B < 0 || CHAN.D < 0) //sentinel checkpoint
{
Flag = TRUE;
printf("The values must be positive.\n");
}
} while(Flag == TRUE);
}

void displayTable(CHANNEL CHAN)
{
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N]; //N is used to avoid magic numbers when defining array size
double arrayvelocity[N]; //N is used to avoid magic numbers when defining array size
int i; //using separate integers for the two different arrays just so it looks better and less confusing
for ( i = 0; i < N; i++)
{
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}

double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2 / 3));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));

return U;
}

最佳答案

您遇到的输入问题是因为在 C 中函数是按值调用的。这意味着当您将 struct 传递给函数时,它是一个副本 是在函数中使用的 struct,而不是原始结构。一旦控制权返回到 main()getPositiveValue() 函数中对 struct 所做的任何更改都不可见。

要解决此问题,请传递指向该结构的指针。使用 -> 运算符一次性取消引用指针并访问成员。这是您的代码的修改版本。我还冒昧地将您的函数声明移至程序的顶部。

computeVelocity() 中调用 pow() 函数时也出现错误:

U = pow(U, (2 / 3));

应该是:

U = pow(U, (2.0 / 3.0));

表达式2/3执行整数除法,结果为零,因此在调用pow()之后,U始终为1. 这可以通过强制浮点除法轻松解决,如上面第二行所示。

#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines

typedef struct CHANNEL_ //Structure CHANNEL
{
char name[9];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;

double computeVelocity(CHANNEL, double);
void getPositiveValue(CHANNEL *);
void displayTable(CHANNEL);

int main(void)
{
CHANNEL CHAN;
printf("Enter the name of the channel: ");
fgets(CHAN.name, 9, stdin);
getPositiveValue(&CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}

void getPositiveValue(CHANNEL *CHAN)
{
int Flag; //sentinel
do
{
Flag = FALSE;
printf("Give the coefficient for roughness, slope, width, and maxdepth: ");
scanf("%lf %lf %lf %lf", &CHAN->n, &CHAN->S, &CHAN->B, &CHAN->D);
if(CHAN->n < 0 || CHAN->S < 0 || CHAN->B < 0 || CHAN->D < 0) //sentinel checkpoint
{
Flag = TRUE;
printf("The values must be positive.\n");
}
}while(Flag == TRUE);
}

void displayTable(CHANNEL CHAN)
{
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N]; //N is used to avoid magic numbers when defining array size
double arrayvelocity[N]; //N is used to avoid magic numbers when defining array size
int i; //using separate integers for the two different arrays just so it looks better and less confusing
for ( i = 0; i < N; i++)
{
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}

double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2.0 / 3.0));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));
return U;
}

程序交互示例:

Enter the name of the channel: chan
Give the coefficient for roughness, slope, width, and maxdepth: 0.035 0.0001 10 4.2
Channel data for chan

Coefficient of roughness: 0.035000
Slope: 0.000100
Width: 10.000000
Maximum depth: 4.200000
Depth Average Velocity
0.168000 0.917961
0.336000 0.566077
0.504000 0.423161
0.672000 0.342380
0.840000 0.289368
1.008000 0.251450
1.176000 0.222759
1.344000 0.200172
1.512000 0.181859
1.680000 0.166669
1.848000 0.153840
2.016000 0.142843
2.184000 0.133301
2.352000 0.124935
2.520000 0.117535
2.688000 0.110939
2.856000 0.105020
3.024000 0.099677
3.192000 0.094829
3.360000 0.090410
3.528000 0.086363
3.696000 0.082644
3.864000 0.079214
4.032000 0.076040
4.200000 0.073095

关于c - getPositiveValues 不会返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52808754/

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