fork download
  1. #include<stdio.h>
  2. struct complex
  3. {
  4. double real; //実数部
  5. double imaginary; //虚数部
  6. };
  7.  
  8. struct complex complex_add(struct complex func_a, struct complex func_b){
  9. struct complex func_c;
  10. func_c.real = func_a.real + func_b.real;
  11. func_c.imaginary = func_a.imaginary + func_b.imaginary;
  12. return func_c;
  13. }
  14.  
  15. struct complex complex_sub(struct complex func_a, struct complex func_b){
  16. struct complex func_c;
  17. func_c.real = func_a.real - func_b.real;
  18. func_c.imaginary = func_a.imaginary - func_b.imaginary;
  19. return func_c;
  20. }
  21.  
  22. struct complex complex_mul(struct complex func_a, struct complex func_b){
  23. struct complex func_c;
  24. func_c.real = func_a.real * func_b.real - func_a.imaginary * func_b.imaginary;
  25. func_c.imaginary = func_a.imaginary * func_b.real + func_a.real * func_b.imaginary;
  26. return func_c;
  27. }
  28.  
  29.  
  30. struct complex complex_div(struct complex func_a, struct complex func_b){
  31. struct complex func_c;
  32. 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);
  33. 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);
  34. return func_c;
  35. }
  36.  
  37. void print_complex(char message[],struct complex a){
  38. printf("%s = %3lf + %3lfi\n",message,a.real,a.imaginary);
  39. }
  40.  
  41. int main(){
  42.  
  43. struct complex a,b,c;
  44.  
  45. a.real = 10;
  46. a.imaginary = 5;
  47. b.real = 20;
  48. b.imaginary = 3;
  49.  
  50. print_complex("a",a);
  51. print_complex("b",b);
  52.  
  53. c = complex_add(a,b);
  54. print_complex("a + b",c);
  55. c = complex_sub(a,b);
  56. print_complex("a - b",c);
  57. c = complex_mul(a,b);
  58. print_complex("a * b",c);
  59. if(b.real != 0 || b.imaginary != 0){
  60. c = complex_div(a,b);
  61. print_complex("a / b",c);
  62. }else{
  63. printf("0除算エラー");
  64. }
  65.  
  66. return(0);
  67. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
a = 10.000000 + 5.000000i
b = 20.000000 + 3.000000i
a + b = 30.000000 + 8.000000i
a - b = -10.000000 + 2.000000i
a * b = 185.000000 + 130.000000i
a / b = 0.525672 + 0.171149i