淘宝订单申请退款:这个C程序有问题,大虾帮忙看看啊~~

来源:百度文库 编辑:高校问答 时间:2024/04/28 07:39:22
这是一个求解二元一次方程的程序,可是不知道为何只能求出当平方根小于0 的情况,平方根大于等于0的情况却求不出来,我是一个C的初学者,水平有限,找不出问题,麻烦大虾帮忙看看啊,谢啦~
/*File:quadratic equation.c
*This program test a function to calculate the solution of a *quadratic equation */
#include <stdio.h>
#include <math.h> /*prototype :square root*/

float x1,x2;
float GearterThanZero(float square);
float EqualThanZero(float square);
void PrintNoSolution(); /*Main program*/
main (void)
{
float a, b, c,square;
printf ("Enter coefficients for the quadratic equation:\n");
printf ("a= ");
scanf ("%f",&a);
printf ("b= ");
scanf ("%f",&b);
printf ("c= ");
scanf ("%f",&c);
printf ("\nequation :%5.2f*x*x + %5.2f*x +%5.2f=0\n ",a, b, c);
square=b*b-4*a*c;
if(square>0)
{
printf("The first solution is %f \n",GearterThanZero(x1) );
printf ("The second solution is %f \n",GearterThanZero(x2) );
}
if (square<0)
{
PrintNoSolution();
}
if (square==0)
{
printf("The solution is x1=x2= %f \n",EqualThanZero(x1));
}
getch();
}
/*Function :Calculate the square root */
float suare;
int a, b;
float square,c;
float GearterThanZero(float square)
{
x1=(-b+sqrt(square))/(2*a);
x2=(-b-sqrt(square))/(2*a);
return (x1);

}
float EqualThanZero()
{
x1=x2=(-b)/(2*a);
return (x1);
}
void PrintNoSolution()
{
printf("This quadratic equation hasn't solution.\n");
}

你的程序中错的地方有多处,第一是参数调用,你用X1来传递,而X1是你要求的值,实际上这个参数应是squaer ,
因为你是对它开平方,另一个重要问题是全局变量和局部变量的问题,你输入的 a,b 并未传到函数中去,它被新定义的变量屏蔽了等等。
你可以跟踪一下自己的程序,知道错在什么地方了,按f7键跟踪到函数里面,F8而不进入函数里面

写的不好

建议重新学函数一章