当前位置: 代码迷 >> C语言 >> 再次[求助]我看不出来有什么错误????
  详细解决方案

再次[求助]我看不出来有什么错误????

热度:433   发布时间:2007-07-11 18:16:13.0
再次[求助]我看不出来有什么错误????

//计算二次方程的根
#include <stdio.h>

int a, b, c;
float x1, x2, data;


int main (void)
{
float todata ( float data);//检验data 的正负 以确定有没有实数根
float sqrt ( float n);//计算二次方的函数

printf ("输入二次方程的a b c:\n");
printf ("a = ");
scanf ("%i", &a);

printf ("b = ");
scanf ("%i", &b);

printf ("c = ");
scanf ("%i", &c);

printf ("方程是: %ix^ + %ix + %i = 0 \n", a , b , c);

data = (float)b * b - 4 * a * c;
data = todata ( data );//检验data的正负

x1 = (-b + sqrt ( data )) / ( 2 * a );
x2 = (-b - sqrt ( data )) / ( 2 * a );

printf ("x1 = %i \nx2 = %i\n", x1, x2);

return 0;
}

float todata ( float data)
{
if ( data < 0)
{
printf ("根是复数\n");
}
else
data = data ;
return 0;
}

float sqrt ( float n)//计算二次方的函数
{
float guess = 1;
const double epsilon = 0.00001;

while ( n / ( guess * guess ) - 1 >= epsilon )
guess = ( n / guess + guess ) / 2;

return guess;
}

搜索更多相关的解决方案: 检验  include  

----------------解决方案--------------------------------------------------------

错了,
再次[求助]我看不出来有什么错误????//计算二次方程的根
#include <stdio.h>

int a, b, c;
float x1, x2, data;


int main (void)
{
float todata ( float data);//检验data 的正负 以确定有没有实数根
float sqrt ( float n);//计算二次方的函数

printf ("输入二次方程的a b c:\n");
printf ("a = ");
scanf ("%i", &a);

printf ("b = ");
scanf ("%i", &b);

printf ("c = ");
scanf ("%i", &c);

printf ("方程是: %ix^ + %ix + %i = 0 \n", a , b , c);

data = (float)b * b - 4 * a * c;
data = todata ( data );//检验data的正负

x1 = (-b + sqrt ( data )) / ( 2 * a );
x2 = (-b - sqrt ( data )) / ( 2 * a );

printf ("x1 = %f \nx2 = %f\n", x1, x2);//%i 该为%f

return 0;
}

float todata ( float data)
{
if ( data < 0)
{
printf ("根是复数\n");
}
else
data = data ;
return 0;
}

float sqrt ( float n)//计算二次方的函数
{
float guess = 1;
const double epsilon = 0.00001;

while ( n / ( guess * guess ) - 1 >= epsilon )
guess = ( n / guess + guess ) / 2;

return guess;
}


----------------解决方案--------------------------------------------------------

晕,还没看清楚!啊,就是这里。应该返回DATA;但你返回了0;
float todata ( float data)
{
if ( data < 0)
{
printf ("根是复数\n");
}
else
data = data ;
return 0;
}

[此贴子已经被作者于2007-7-11 18:36:51编辑过]


----------------解决方案--------------------------------------------------------

#include <stdio.h>

int a, b, c;
float x1, x2, data;


int main (void)
{
float todata ( float data);//检验data 的正负 以确定有没有实数根
float sqrt ( float n);//计算二次方的函数

printf ("输入二次方程的a b c:\n");
printf ("a = ");
scanf ("%i", &a);

printf ("b = ");
scanf ("%i", &b);

printf ("c = ");
scanf ("%i", &c);

printf ("方程是: %ix^ + %ix + %i = 0 \n", a , b , c);

data = (float)b * b - 4 * a * c;
data = todata ( data );//检验data的正负

x1 = (-b + sqrt ( data )) / ( 2 * a );
x2 = (-b - sqrt ( data )) / ( 2 * a );

printf ("x1 = %f \nx2 = %f\n", x1, x2);

return 0;
}

float todata ( float data)
{
if ( data < 0)
{
printf ("根是复数\n");
}
else
data = data ;
return data;
}

float sqrt ( float n)//计算二次方的函数
{
float guess = 1;
const double epsilon = 0.00001;//精度

while ( n / ( guess * guess ) - 1 >= epsilon )
guess = ( n / guess + guess ) / 2;

return guess;
}
还是不对呀 计算出来的答案有误
你们帮我看一下函数的调用 看有没有错


----------------解决方案--------------------------------------------------------
  相关解决方案