2017-03-25 27 views
1

我很感興趣的是如何使用標準庫win32在c語言中執行復數的算術運算。例如:c中的複數運算算法

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

int main(int argc, char **argv) 
{ 
    _Dcomplex a; 
    _Dcomplex b = 2. + 3. * _Complex_I; 
    _Dcomplex c = a + b; 

    return 0; 
} 

據我所知,Microsoft不支持C99標準。我如何繞過這些限制? 謝謝

+0

手卷你自己的複合類 –

+1

'a'未初始化。請參閱此處瞭解更多詳情:http://en.cppreference.com/w/c/numeric/complex/Complex_I – vivekn

回答

1

不幸的是,早期的微軟並不急於提供C99庫支持,因爲它的用戶基數相對較小,並且很難將現代gcc代碼移植到MSVC。這是一個優先事項。

但是,對開發者的需求,他們已經實現了在Visual Studio 2013及以上的許多C99庫此處提到:

https://blogs.msdn.microsoft.com/vcblog/2013/07/19/c99-library-support-in-visual-studio-2013/

因此,在C語言的代碼可以寫爲:

#include <stdio.h>  /* Standard Library of Input and Output */ 
#include <complex.h> /* Standard Library of Complex Numbers */ 

int main() { 

double complex z1 = 1.0 + 3.0 * I; 
double complex z2 = 1.0 - 4.0 * I; 

printf("Initial values: Z1 = %.2f + %.2fi \nZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2)); 

double complex sum = z1 + z2; 
printf("Sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum)); 

double complex diff = z1 - z2; 
printf("Diff: Z1 - Z2 = %.2f %+.2fi\n", creal(difference), cimag(difference)); 

double complex product = z1 * z2; 
printf("Product: Z1 x Z2 = %.2f %+.2fi\n", creal(product), cimag(product)); 

double complex quotient = z1/z2; 
printf("Quotient: Z1/Z2 = %.2f %+.2fi\n", creal(quotient), cimag(quotient)); 

double complex conjugate = conj(z1); 
printf("Conjugate of Z1 = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate)); 

return 0; 

}

功能,如COS(),實驗值()和SQRT()必須與它們複雜的形式,例如被替換ccos(),cexp(),csqrt(),他們工作得很好。

其他的解決方法可以是完全刮掉MS的編譯器和使用英特爾編譯器(這是更明智),其在Visual C.

更明智的做法是工作移動到英特爾CC或GCC,和使用Eclipse爲您的編程環境。不幸的是,跨Windows-Linux-Solaris-AIX等的代碼的可移植性通常很重要,MS工具根本不支持這些工具。

+0

感謝您的回答。要使用gcc編譯器,是否需要安裝另一個庫? –

+0

@ViteDecorum你可以查看這個[link](https://gcc.gnu.org/install/)來幫助你安裝gcc編譯器。你只需要下載源代碼然後安裝它。 – gaurav

+0

我知道C99 for Windows庫尚未完成。我需要繞過約束。也許有一個來源可以解決這個限制,不需要爲windows或'gnuwin32'安裝'gcc'? –