C语言 如何在C中放入数学方程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6118146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to put in mathematical equation in C
提问by redkimono
I've been trying to look up on Google how to put in an equation in my program but wasn't able to find any. How do you include:
我一直在尝试在 Google 上查找如何在我的程序中放入方程式,但找不到任何方程式。你如何包括:
x = ( -b + √b2 - 4ac ) / 2a
in the program?
在节目中?
Here's my code:
这是我的代码:
{
int a, b, c;
float x;
//statements
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
//computeforX
x = ( -b + √b2 - 4ac ) / 2a
printf("The value of x is %.1f", x);
return 0;
}
回答by Oliver Charlesworth
Assuming we're talking about C (or C++) here, you will need to investigate the sqrtfunction, and maybe also the powfunction as well (although that's unnecessary because b-squared can be computed as b*b).
假设我们在这里讨论的是 C(或 C++),您将需要研究sqrt函数,也许还需要研究函数pow(尽管这是不必要的,因为b-squared 可以计算为b*b)。
Note that you will need to convert all of your input values to floator doublebefore you start the calculation, otherwise you will not get the intended result.
请注意,您需要在开始计算之前float或double之前将所有输入值转换,否则将无法获得预期结果。
回答by sigfpe
You need a table to allow you to translate:
您需要一个表格来进行翻译:
a+b -> a+b
a+b -> a+b
a-b -> a-b
ab-> a-b
a/b -> a/b
a/b -> a/b
ab -> a*b
ab-> a*b
√x -> sqrt(x)
√x -> sqrt(x)
x2 -> x*x(If you want to square something more complicated it might be best to use a temporary variable for the value to be squared, breaking your equation up into pieces.)
x2 -> x*x(如果你想对更复杂的东西进行平方,最好使用一个临时变量来对要平方的值进行平方,将你的方程分成几部分。)
Note that if you divide an intby an intin C you get an int. So better convert those ints to doubles before dividing.
请注意,如果在 C 中将anint除以 an int,则会得到一个 int。所以最好在除法之前将这些ints转换为doubles。
回答by colinross
If we are dealing with C++ it would be something like
如果我们正在处理 C++,它将类似于
#include <iostream.h>
#include <cmath>
int main ()
{
//Declare Variables
double x,x1,x2,a,b,c;
cout << "Input values of a, b, and c." ;
cin >>a >>b >>c;
if ((b * b - 4 * a * c) > 0)
cout << "x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)" &&
cout << "x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)";
if else ((b * b - 4 * a * c) = 0)
cout << "x = ((-b + sqrt(b * b - 4 * a * c)) / (2 * a)"
if else ((b * b - 4 * a * c) < 0)
cout << "x1 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a) &&
cout << "x2 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a);
return (0);
}
Now why do i have this wierd feeling I just did someone's first semester programming class' homework?
现在为什么我有这种奇怪的感觉,我刚刚做了某人第一学期编程课的作业?
Granted its been years and I don't even know if that will compile but you should get the idea.
当然,它已经多年了,我什至不知道这是否会编译,但你应该明白这个想法。
回答by linux23423
I am really depressed looking the quality of above answers and help, which has been given.
看着上面给出的答案和帮助的质量,我真的很沮丧。
I hope to improve the content of this thread.
我希望改进这个线程的内容。
One can compile the C file below with the command line gcc file.c -o file -lm.
可以用命令行编译下面的 C 文件gcc file.c -o file -lm。
Herewith a possible solution in C:
这里有一个可能的 C 解决方案:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(){
int da, db, dc;
double x, a,b,c;
//statements
printf("Enter three integers: ");
scanf("%d %d %d", &da, &db, &dc);
a = (double)da;
b = (double)db;
c = (double)dc;
//computeforX
x = (double) ( -b + sqrt(b * b) - 4 * a * c ) / ( 2 * a ) ;
printf("The value of x is %g \n", x);
return 0;
}

