gpt4 book ai didi

c - 如何打印 C 中方程式的答案?

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:37 24 4
gpt4 key购买 nike

我希望程序能够求解我的方程式,但遗憾的是它没有。此外,我希望它根据我在等式中输入的 x 值打印一个答案。请告诉我如何打印答案或如何对其进行编程,以便方程式给出一个答案,然后我可以打印出来。

/* Preprocessor directives */
#include <stdio.h>
#include <math.h>

/* Main program */
void main ()
{
/*
variable declaration section comments

l: length value
q: value of q
ei: value of ei
s: l devided by 2 since 0 < x < l/2
b: the length l (thus, 20)
z: 0
first_equation: The first equation pertaining to 0 < x < l/2
second_equation:The second equation pertaining to l/2 < x < l
*/

double x, first_equation, second_equation, l, q, ei, s, b, z;

l = 20.0;
q = 4000.0;
ei = 1.2 * (pow(10.0, 8.0));
s = l / 2.0;
b = l;
z = 0.0;

printf ("please enter the x-value\n");
scanf ("%lf", &x);

/* Deflection equations */
first_equation = ((q * x) / (384.0 * ei)) * ((9 * (pow(l, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (16 * (pow(x, 3.0))));
second_equation = ((q * l) / (384.0 * ei)) * ((8 * (pow(x, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (17 * (pow(l, 2.0)) * x) - (pow(l, 3.0)));

/* Determining what equation to use */
if (x >= z && x <= s)
printf ("\n first_equation\n\n");
else if (x > s && x <= b)
printf ("\n second_equation\n\n", second_equation);
else if (x < 0 || x > b)
printf ("\n invalid location\n\n");

return;
}

最佳答案

这...

printf ("\n second_equation\n\n", second_equation);

... 不打印 second_equation 变量:它将它作为参数提供给 printf,但 printf 仅使用额外的参数作为由 %f 或作为第一个参数提供的文本中嵌入的其他转换指令指示。你可以这样写:

printf ("\n second_equation %f\n\n", second_equation);

您可能想对 first_equation 做类似的事情。

或者 [当我回答问题时标记为 C++] 你可以使用 C++ I/O 例程(scanfprintf 来自 C 库,并且有一个编号的缺点,这里最明显的是你必须记住有趣的字母代码,比如匹配你的数据类型的“lf”)...

#include <iostream>

...在文件的最顶部,然后在函数中写入...

std::cout << "\n second_equation " << second_equation << "\n\n";

您还可以使用 C++ I/O 进行输入,将 scanf 替换为...

if (!(std::cin >> x))
{
std::cerr << "you didn't enter a valid number\n";
exit(1);
}

关于c - 如何打印 C 中方程式的答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35217166/

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