C++ 声明、定义和调用
我需要更好地理解使用这个程序的函数定义、声明和正确调用。我真的需要了解如何使用它们。你能告诉我正确的方法来编写这个程序吗?这三种方法都是正确的,并且解释得很清楚C++ 声明、定义和调用,c++,c,declaration,C++,C,Declaration,我需要更好地理解使用这个程序的函数定义、声明和正确调用。我真的需要了解如何使用它们。你能告诉我正确的方法来编写这个程序吗?这三种方法都是正确的,并且解释得很清楚 #include <stdio.h> #include <math.h> quad_equation(float a, float b, float c); int main() { float a, b, c, determinant, r1,r2; printf("Enter coe
#include <stdio.h>
#include <math.h>
quad_equation(float a, float b, float c);
int main()
{
float a, b, c, determinant, r1,r2;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0) { r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else (determinant<0);
{
printf("Both roots are complex");
}
return 0;
#包括
#包括
四元方程(浮点a、浮点b、浮点c);
int main()
{
浮点a,b,c,行列式,r1,r2;
printf(“输入系数a、b和c:”);
scanf(“%f%f%f”、&a、&b和&c);
行列式=b*b-4*a*c;
如果(行列式>0)
{
r1=(-b+sqrt(行列式))/(2*a);
r2=(-b-sqrt(行列式))/(2*a);
printf(“根是:%.2f和%.2f”,r1,r2);
}
如果(行列式==0){r1=r2=-b/(2*a);
printf(“根是:%.2f和%.2f”,r1,r2);
}
else(行列式我刚刚解决了这个问题:(我想这是作业的一部分)
同时看看你的代码。你从来没有使用过函数四元方程。而且你还没有定义函数的类型(int/void/float/char)等等
为方便起见:(这是完整的代码)--如果你什么都不懂,请问我
#include <stdio.h>
#include <math.h>
// function declarations
void twoRoots (float a,float b,float delta);
void oneRoot (float a,float b,float delta);
int main (void)
{
//Local Declarations
float a;
float b;
float c;
float delta;
// float solution;
printf("Input coefficient a.\n");
scanf("%f", &a);
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);
printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c);
delta = (float)(b*b) - (float)(4.0 * a * c);
printf("delta = %0.2f\n",delta);
if (delta > 0){
twoRoots(a,b,delta);
}else if (delta == 0) {
oneRoot(a,b,delta);
}else if (delta < 0.0){
printf("There are no real roots\n");
}
return 0;
}
void twoRoots (float a,float b,float delta)
{
float xOne;
float xTwo;
float deltaRoot;
printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", xOne);
printf("%.2f", xTwo);
}
void oneRoot(float a,float b,float delta)
{
float xOne;
// float xTwo;
// float deltaRoot;
printf("There is exactly one distinct root\n");
xOne = -b / (2*a);
printf("%.2f", xOne);
}
如果您输入以下内容,此操作将失败:121
因为scanf将把整个121
读入a
,而b
,c
将没有任何内容(而是将\n(输入)放入b,未定义到c)
因此,请按照我在代码中使用scanf的方式使用scanf,好吧-这充满了问题!我试图指出这些问题,并展示“更好”是什么样子。我希望这会有所帮助
quad_equation(float a, float b, float c);
这可能是一个“函数原型”。原型告诉编译器“我稍后将使用这个函数,这就是调用它的方式,以及它返回的类型”。您没有指定返回类型;您可能希望使用int
说明是否找到了根,并在函数中打印结果。最好将两个返回值的空格作为参数传递:
int quad_equation(float a, float b, float c, float* x1, float* x2);
现在我们可以使用main
程序获取输入/输出,让函数解决问题:
int main(void) {
{
float a, b, c, r1, r2;
int n;
// here you get the inputs; that seems OK
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a,&b,&c);
// now you have to "call your function"
// note that I make sure to follow the prototype: I assign the return value to an int
// and I pass five parameters: the coefficients a, b, c and the address of two variables
// x1 and x2. These addresses will be where the function puts the roots
n = quad_equation(a, b, c, &r1, &r2);
// when the function returns, I can print the results:
printf("There are %d roots:\n", n);
// based on the value of n, I change what I want to print out:
if (n == 2) printf(" %f and ", r1); // when there are two roots I print "root 1 and"
if (n > 0) printf("%f\n", r2); // when there is at least one root, I print it
// note that if n == 0, I would skip both print statements
// and all you would have gotten was "There are 0 roots" in the output
}
int quad_equation(float a, float b, float c, float* x1, float* x2) {
// function that computes roots of quadratic equation
// and returns result in x1 and x2
// it returns the number of roots as the return value of the function
float determinant;
determinant=b*b-4*a*c;
if (determinant>0)
{
*x1 = (-b+sqrt(determinant))/(2*a);
*x2= (-b-sqrt(determinant))/(2*a);
return 2;
}
if (determinant==0) {
*x1 = *x2 = -b/(2*a);
return 1;
}
return 0;
}
在声明中,我想知道“*”是什么意思浮点数旁边是表示?R1和R2是否也被假定为底部的X1和X2?*表示这不是一个浮点数,而是一个指向浮点数的指针-也就是说,这个变量包含一个存储浮点数的位置的地址。这允许函数修改该位置的内容-因此它可以传递更多信息我故意在函数中给他们一个不同的名称,因为这是你可以做的事情之一:你用来定义它的变量的名称不需要是你调用它的变量的名称。第一个浮点解决方案“未使用的变量声明”,最底层的xtwo和deltar也是如此oot@user2879277删除它们..好的,等等..我会编辑代码是的,这就是我所做的,我重新安排了一些事情,因为这与我最初所做的大不相同。我真的很感谢你的帮助。如果你什么都不懂,问我..如果这有助于你,请接受答案是的,没问题,但出于某种原因,我拒绝了i don’我输入的每一件东西都没有真正的根,我想不出来
int main(void) {
{
float a, b, c, r1, r2;
int n;
// here you get the inputs; that seems OK
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a,&b,&c);
// now you have to "call your function"
// note that I make sure to follow the prototype: I assign the return value to an int
// and I pass five parameters: the coefficients a, b, c and the address of two variables
// x1 and x2. These addresses will be where the function puts the roots
n = quad_equation(a, b, c, &r1, &r2);
// when the function returns, I can print the results:
printf("There are %d roots:\n", n);
// based on the value of n, I change what I want to print out:
if (n == 2) printf(" %f and ", r1); // when there are two roots I print "root 1 and"
if (n > 0) printf("%f\n", r2); // when there is at least one root, I print it
// note that if n == 0, I would skip both print statements
// and all you would have gotten was "There are 0 roots" in the output
}
int quad_equation(float a, float b, float c, float* x1, float* x2) {
// function that computes roots of quadratic equation
// and returns result in x1 and x2
// it returns the number of roots as the return value of the function
float determinant;
determinant=b*b-4*a*c;
if (determinant>0)
{
*x1 = (-b+sqrt(determinant))/(2*a);
*x2= (-b-sqrt(determinant))/(2*a);
return 2;
}
if (determinant==0) {
*x1 = *x2 = -b/(2*a);
return 1;
}
return 0;
}