谁能用指针写一下这个C语言程序?

来源:百度知道 编辑:UC知道 时间:2024/05/24 15:49:47
Write a program, that computes the roots of an equation.
The coëfficients a, b and c of ax^2+bx+c=0 must be entered by using a function void Input() .
Then by a function void Root() and using the “a,b,c-formula” the roots are being calculated.
If the discriminant <0 , this will be printed to the screen.
Next a function Output() takes care of a neat printout.
Use pointers! Array’s are not allowed!

写一个程序,计算方程的根。必须使用函数void input()调用输入ax^2+bx+c=0中的a,b,c。 通过函数void void Root和使用a,b,c方程的根进行计算。如果判别式小于0,将它显示在屏幕上。然后显示output(),注意输出的整洁。请使用指针编程,不要使用数组。

#include<stdio.h>
#include<math.h>
float a,b,c;
void output(float r1,float r2);
void input()
{
scanf("%f%f%f",&a,&b,&c);
}

void Root()
{
float *root1,*root2,m,n;
if(b*b-4*a*c<0)
printf("没有实根!\n");
else
{
m=(float)(-b+sqrt(b*b-4*a*c))/2;
n=(float)(-b-sqrt(b*b-4*a*c))/2;
root1=&m;
root2=&n;
output(*root1,*root2);
}
}

void output(float r1,float r2)
{
printf("root1=%5.2f root2=%5.2f\n",r1,r2);
}

void main()
{
input();
Root();
}