程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言問答 >> C語言幫慢修改一下

C語言幫慢修改一下

編輯:C語言問答

C語言幫慢修改一下

#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);

 


}

最佳回答:

目的?

追問:

計算一個二次方程

回答:
#include<stdio.h>

void loop_until(float sum, float b, float a);
float square(float sum);
float representation_sign(float a, float b, float c);

void 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,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);
}
//幫你編譯通過了 結果對不對就不知道了  正在測試
追問:

為什麼我寫的程序會出現這些警告呢?

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

回答:
warning  是可以忽視的 
function  - -哪來的?
追問:

不知道但我修改後就沒有提示了

 

#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);

不過這樣容易弄亂思路


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved