#include<stdio.h>
float loop_until(float sum, float b, float a)
{
float x1, x2;
x1 = (-b + sum)/(2 * a);
x2 = (-b - sum)/(2 * a);
printf( "The loop is %f %f", x1, x2);
}
float absouluteValue(float sum)
{
if (sum < 0)
sum = -sum;
return sum;
}
float square(float sum)
{
float epsilon = 0.00001;
float guess = 1.0;
float absouluteValue(float x);
while( absouluteValue(guess * guess - sum) >= epsilon)
guess = ( sum / guess + guess ) / 2.0;
return guess;
}
float representation_sign(float a, float b, float c)
{
float sum;
sum = b*b-4*a*c;
if(sum < 0)
printf("The loop is complex");
else
return sum;
}
main (void)
{
float a, b, c;
float representation_sign(float a, float b, float c);
float square(float sum);
float loop_until(float sum, float b, float a);
printf("\tPlease in order to type a, b, c,\n");
printf("\tPRESS ENTER END\n");
scanf("%f%f%f", &a, &b, &c);
representation_sign(a, b, c);
square(representation_sign);
loop_until(square, a, b);
}
計算一個二次方程
為什麼我寫的程序會出現這些警告呢?
Compiling...
Text1.c
D:\u\Text1.c(40) : warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data
D:\u\Text1.c(79) : error C2115: 'function' : incompatible types
D:\u\Text1.c(79) : warning C4024: 'square' : different types for formal and actual parameter 1
D:\u\Text1.c(81) : error C2115: 'function' : incompatible types
D:\u\Text1.c(81) : warning C4024: 'loop_until' : different types for formal and actual parameter 1
不知道但我修改後就沒有提示了
#include<stdio.h>
float loop_until(float sum, float a, float b)
{
float x1, x2;
x1 = (-b + sum)/(2 * a);
x2 = (-b - sum)/(2 * a);
printf( "The loop is %f %f", x1, x2);
}
float absouluteValue(float sum)
{
if (sum < 0)
sum = -sum;
return sum;
}
float square(float sum)
{
const float epsilon = .00001f;
float guess = 1.0;
float absouluteValue(float x);
while( absouluteValue(guess * guess - sum) >= epsilon)
guess = ( sum / guess + guess ) / 2.0;
return guess;
}
float representation_sign(float a, float b, float c)
{
float sum;
sum = b*b-4*a*c;
if(sum < 0)
printf("The loop is complex");
else
return sum;
}
float main (void)
{
float a, b, c;
float representation_sign(float a, float b, float c);
float square(float sum);
float loop_until(float sum, float a, float b);
float temp1, temp2;
printf("\tPlease in order to type a, b, c,\n");
printf("\tPRESS ENTER END\n");
scanf("%f%f%f", &a, &b, &c);
temp1 = representation_sign(a, b, c);
temp2 = square(temp1);
loop_until(temp2, a, b);
}
我想問為什麼temp1 = representation_sign(a, b, c)和temp2 = square(temp1)要給一個變量temp1和temp2存放才能成功,我的書上沒有提到這點
representation_sign(a, b, c);
square(representation_sign);
loop_until(square, a, b);
改成
loop_until(square(representation_sign(a, b, c)), a, b);
不過這樣容易弄亂思路