我希望程序可以解决我的方程式,但遗憾的是它没有。另外,我想要它根据我在等式中输入的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;
}
答案 0 :(得分:3)
此...
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例程(scanf
和printf
来自C库,并且有许多缺点,最明显在这里你必须记住有趣的字母代码,例如&#34; lf&#34;匹配你的数据类型)......
#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);
}
答案 1 :(得分:0)
你的代码真的不清楚;但按照你的问题,你似乎希望能够打印你的答案。在这种情况下,这是正确的语法
printf ("Answer: %d \n", yourAnswer); //if 'yourAnswer' is decimal or number
要使用您的某个代码段,您将拥有以下内容:
printf ("\n second_equation: %d\n", second_equation);