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

标签 c

我希望程序能够求解我的方程式,但遗憾的是它没有。此外,我希望它根据我在等式中输入的 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/

相关文章:

python - 我从哪里开始学习对机器人进行编程?

c - 为什么使用 paho.mqtt.c 发送此消息会导致段错误?

c - 在子进程处于事件状态并运行时读取和写入

c - 如何对 PostgreSQL C 语言函数进行单元测试

c - 程序不打印空格和标点符号

无法打印 C 中的返回值

c++ - 如何使用makefile从C中的另一个目录包含.a静态库和.h文件?

c - asm 关键字作为 C 中的标识符名称?

c - 代码是否可以在两家不同公司生产的 Cortex A5 和 Cortex A9 之间轻松移植?

c++ - *(char**) 如何理解这个构造?