2017-10-20 43 views
-2

問題二次方程根的功能

編寫計算二次方程

ax^2 +bx+c = 0 

您應該處理的三種根的實部和虛根的功能。

提示:使用此函數原型:

int calculateRoots(int a,int b,int c,float* root1,float* root2); 

我的問題:

  • 怎麼能解二次方程函數返回一個int?我在毫無頭緒這意味着什麼
  • 我改變了機能的研究返回類型void()但我不能處理2虛根不知道如何回到現實+ IMAG我

以下就是我這樣達成遠:

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 


void calculateRoots(int a,int b,int c,float* root1,float* root2); 
    int main() 
    { 
     float r1,r2; 
     int a,b,c; 
     printf("enter the coefficients :\n"); 
     scanf("%d%d%d",&a,&b,&c); 
      void calculateRoots(a,b,c,&r1,&r2); 
     printf("%d and %d are roots,r1,r2); 
     return 0; 
    } 
    void calculateRoots(int a,int b,int c,float* root1,float* root2) 
    { float x=b*b-4*a*c; 
     if(x==0) 
     { 
      *root1=(-1*b)/(2*a); 
      *root2=(-1*b)/(2*a); 
     } 
     else if(x>0) 
     { 
      *root1=(-1*b+x)/(2*a) ; 
      *root2=(-1*b-x)/(2*a) ; 
     } 
     if(x<0) 
     { 
      root1=// Any help here 
     } 
    } 
+0

這看起來像你應該問你的老師。你是否爲每個案例提供了樣本輸入/輸出? – Kevin

+0

你可能沒有被要求處理假想的根。你可能需要處理這3個案例,一個根,兩個根,根本沒有根。如果'x <0'返回0,否則返回1.如果函數返回0,則調用者可以知道沒有根。 –

+0

'float * root1'可以指向一個***數組***。所以這個函數被這樣調用:'float r1 [2],r2 [2]; calculateRoots(a,b,c,r1,r2);'返回值是1或2,即根的數量。 – user3386109

回答

0

怎麼能解二次方程函數返回一個int

您應該返回數的根,因此你會知道有多少根要打印。

我玩不轉2虛根不知道如何返回 真正+ IMAG

我認爲(但也許@deamentiaemundi是正確的,因爲這個問題你真的只有兩個不同的數字,與函數返回值的幫助,讓重建有效的結果,並再次閱讀您的問題後,措辭可能暗示它),您預計將使用兩個元素的數組:

float resultRoot1[2]; 
float resultRoot2[2]; 
int numRoots = calculateRoots(a, b, c, resultRoot1, resultRoot2); 

裏面calculateRoots

root1[0] = real_part; 
root1[1] = imaginary_part; 

例如,給定

2x^2 + 2x + 1 = 0 

你應該返回

-0.5 - 0.5i 
-0.5 + 0.5i 

所以resultRoot1應包含值-0.5-0.5resultRoot2-0.50.5


超出你asigment(對未來的考慮),您可以嘗試使用結構

struct ComplexNum 
{ 
    double re, im; 
}; 

int calculateRoots(int a, int b, int c, struct ComplexNum* root1, struct ComplexNum* root2) 
{ 
    ... 
    root1->re = real_part; 
    root1->im = imaginary_part; 
    ... 
    return num_roots; 
} 
... 
struct ComplexNum resultRoot1, resultRoot2; 
int numRoots = calculateRoots(a, b, c, &resultRoot1, &resultRoot2); 

甚至更​​好複雜的頭

#include <complex.h> 

int calculateRoots(int a, int b, int c, double complex* root1, double complex* root2) 
{ 
    ... 
    *root1 = real_part + imaginary_part * I; 
    ... 
    return num_roots; 
} 
... 
double complex resultRoot1; 
double complex resultRoot2; 
int numRoots = calculateRoots(a, b, c, &resultRoot1, &resultRoot2); 
0

的變化丹尼爾瑞典克朗的提示:

由於求解二次方程(w,real coefs)的規則非常簡單,所以可以使用這兩個方法float原型中的參數與您一樣。

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

int calculateRoots(int a, int b, int c, float *root1, float *root2); 

int main(void) 
{ 
    float r1, r2; 
    int a, b, c; 
    int ret; 

    puts("enter the coefficients a, b, c:"); 
    ret = scanf("%d%d%d", &a, &b, &c); 
    // always, An I mean _always_ check the returns of scanf! 
    if(ret != 3){ 
    puts("No, the three coeficients, please, and they have to be integers. Try again."); 
    return EXIT_FAILURE; 
    } 

    ret = calculateRoots(a, b, c, &r1, &r2); 
    // the function returns a value that is eather the sign of the 
    // determinant or an error. 
    switch (ret) { 
    case 0: 
     printf("r1 = r2 = %f\n", r1); 
     break; 
    case 1: 
     printf("r1 = %f and r2 = %f\n", r1, r2); 
     break; 
    case -1: 
     printf("r1 = %f + %fi and r2 = %f - %fi\n", r1, r2, r1, r2); 
     break; 
    default: 
     puts("No roots found, check input"); 
     break; 
    } 
    return 0; 
} 

int calculateRoots(int a, int b, int c, float *root1, float *root2) 
{ 
    float x; 
    // shortcut: check for a== 0 
    if (a == 0) { 
    return -2; 
    } 
    // find determinant 
    x = b * b - 4 * a * c; 
    fprintf(stderr, "determinant is: %f\n",x); 
    // checking if a float is exactly equal to a number is highly problematic! 
    if (x == 0.0) { 
    *root1 = (-1 * b)/(2 * a); 
    *root2 = *root1; 
    return 0; 
    } 
    // macro avaliable with c >= c99 
    else if (isgreater (x , 0.0)) { 
    *root1 = (-b + sqrt(x))/(2 * a); 
    *root2 = (-b - sqrt(x))/(2 * a); 
    return 1; 
    } 
    // macro avaliable with c >= c99 
    if (isless(x , 0.0)) { 
    // not the nicest way possible, but good enough if documented: 
    // put the real part in one variable, the imaginary part in another, 
    // return the sign and let the caller sort it out. 
    *root1 = -b/(2 * a); 
    *root2 = sqrt(-x)/(2 * a); 
    return -1; 
    } 
    // notreached 
    return -3; 
} 

C99和更高版本有複雜的數字。您可以改用它們。