c语言问题,需要指教下

来源:百度知道 编辑:UC知道 时间:2024/05/10 09:54:27
#include<stdio.h>
#include<math.h>
float x1,x2,p,q;
void main()
{
void situation_one(float a,float b,float c);
void situation_two(float a,float b);
void situation_three(float,float b,float c);
float a,b,c,d;
scanf("%f,%f,%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{ situation_one(a,b,c);
printf("%6.2f,%6.2f\n",x1,x2);
}
else if(d==0)
{ situation_two(a,b);
printf("%6.2f\n",x1);
}
else
{ situation_three(a,b,c);
printf("%6.2f+%6.2fi,%6.2f-%6.2if\n",p,q,p,q);
}
}
void situation_one(float a,float b,float c)
{
x1=(-b+sqrt(b*b-4*a*c))/(2*a);
x2=(-b-sqrt(b*b-4*a*c))/(2*a);
}
void situation_two(float a,float b)
{
x1=x2=(-b)/(2*a);
}
void situation_three(float a,float b,float c)
{ p=-b/(2*a);
q=sqrt(-(b*b-

如果在不把X1和X2定义为全局变量的话,那么,X1与X2就得不到保存,在调用完函数后,就马上会释放;
如果用指针定义就不同了;
为了方便定义全局变量是最好不过的

大括号内定义的变量作用域仅在该大括号中
如果在main和其他函数中都定义x,两个x的地址是不同的
在全局定义x,那么main和其他函数是共用x的.

如果不定义x1,x2为全局变量而在主函数和几个void situation函数中定义x1,x2,这样由几个void situation函数中定义的x1,x2在运算后值得不到保存,函数结束后就释放了。而在这个方程中定义局部变量不是必须,可以地址传递参数和返回值完成。

全局变量在其他函数中都可以调用改变,而局部变量只是在其定义的函数内部起作用函数调用完之后其内存空间就被释放了。