#include<stdio.h>
struct complex
{
double real; //実数部
double imaginary; //虚数部
} ;
struct complex complex_add( struct complex func_a, struct complex func_b) {
struct complex func_c;
func_c.real = func_a.real + func_b.real ;
func_c.imaginary = func_a.imaginary + func_b.imaginary ;
return func_c;
}
struct complex complex_sub( struct complex func_a, struct complex func_b) {
struct complex func_c;
func_c.real = func_a.real - func_b.real ;
func_c.imaginary = func_a.imaginary - func_b.imaginary ;
return func_c;
}
struct complex complex_mul( struct complex func_a, struct complex func_b) {
struct complex func_c;
func_c.real = func_a.real * func_b.real - func_a.imaginary * func_b.imaginary ;
func_c.imaginary = func_a.imaginary * func_b.real + func_a.real * func_b.imaginary ;
return func_c;
}
struct complex complex_div( struct complex func_a, struct complex func_b) {
struct complex func_c;
func_c.real = ( func_a.real * func_b.real + func_a.imaginary * func_b.imaginary ) / ( func_b.real * func_b.real + func_b.imaginary * func_b.imaginary ) ;
func_c.imaginary = ( func_a.imaginary * func_b.real - func_a.real * func_b.imaginary ) / ( func_b.real * func_b.real + func_b.imaginary * func_b.imaginary ) ;
return func_c;
}
void print_complex( char message[ ] , struct complex a) {
printf ( "%s = %3lf + %3lfi\n " , message
, a.
real , a.
imaginary ) ; }
int main( ) {
struct complex a, b, c;
a.real = 10 ;
a.imaginary = 5 ;
b.real = 20 ;
b.imaginary = 3 ;
print_complex( "a" , a) ;
print_complex( "b" , b) ;
c = complex_add( a, b) ;
print_complex( "a + b" , c) ;
c = complex_sub( a, b) ;
print_complex( "a - b" , c) ;
c = complex_mul( a, b) ;
print_complex( "a * b" , c) ;
if ( b.real != 0 || b.imaginary != 0 ) {
c = complex_div( a, b) ;
print_complex( "a / b" , c) ;
} else {
}
return ( 0 ) ;
}
I2luY2x1ZGU8c3RkaW8uaD4Kc3RydWN0IGNvbXBsZXgKewogICAgZG91YmxlIHJlYWw7ICAgIC8v5a6f5pWw6YOoCiAgICBkb3VibGUgaW1hZ2luYXJ5OyAgIC8v6Jma5pWw6YOoCn07CiAKc3RydWN0IGNvbX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