fork(1) download
  1. #include <stdio.h>
  2.  
  3. struct Complex
  4. {
  5. double real; //Действит. часть
  6. double imag; //Мнимая часть
  7. };
  8.  
  9. struct Complex inputComplex();
  10. void printComplex(struct Complex c);
  11. struct Complex addComplex(struct Complex a, struct Complex b);
  12. struct Complex subtractComplex(struct Complex a, struct Complex b);
  13. struct Complex multiplyComplex(struct Complex a, struct Complex b);
  14. struct Complex divideComplex(struct Complex a, struct Complex b);
  15.  
  16. int main()
  17. {
  18. struct Complex num1, num2;
  19.  
  20. printf("Введите первое комплексное число:\n");
  21. num1 = inputComplex();
  22.  
  23. printf("Введите второе комплексное число:\n");
  24. num2 = inputComplex();
  25.  
  26. printf("\nПервое число: ");
  27. printComplex(num1);
  28. printf("Второе число: ");
  29. printComplex(num2);
  30.  
  31. printf("\nРезультаты операций:");
  32.  
  33. struct Complex sum = addComplex(num1, num2);
  34. printf("\nСумма: ");
  35. printComplex(sum);
  36.  
  37. struct Complex diff = subtractComplex(num1, num2);
  38. printf("Разность: ");
  39. printComplex(diff);
  40.  
  41. struct Complex prod = multiplyComplex(num1, num2);
  42. printf("Произведение: ");
  43. printComplex(prod);
  44.  
  45. struct Complex quot = divideComplex(num1, num2);
  46. printf("Частное: ");
  47. printComplex(quot);
  48.  
  49. return 0;
  50. }
  51.  
  52. //Функция ввода комплексного числа:
  53. struct Complex inputComplex() {
  54. struct Complex c;
  55. printf("Введите действительную часть: \n");
  56. scanf("%lf", &c.real);
  57. printf("Введите мнимую часть: \n");
  58. scanf("%lf", &c.imag);
  59. return c;
  60. }
  61.  
  62. //Функция для вывода комплексного числа:
  63. void printComplex(struct Complex c) {
  64. if (c.imag >= 0)
  65. printf("%.2f + i*%.2f\n", c.real, c.imag);
  66. else
  67. printf("%.2f - i*%.2f\n", c.real, -c.imag);
  68. }
  69.  
  70. //Сложение:
  71. struct Complex addComplex(struct Complex a, struct Complex b)
  72. {
  73. struct Complex result;
  74. result.real = a.real + b.real;
  75. result.imag = a.imag + b.imag;
  76. return result;
  77. }
  78.  
  79. //Вычитание:
  80. struct Complex subtractComplex(struct Complex a, struct Complex b)
  81. {
  82. struct Complex result;
  83. result.real = a.real - b.real;
  84. result.imag = a.imag - b.imag;
  85. return result;
  86. }
  87.  
  88. //Умножение:
  89. struct Complex multiplyComplex(struct Complex a, struct Complex b)
  90. {
  91. struct Complex result;
  92. result.real = a.real * b.real - a.imag * b.imag;
  93. result.imag = a.real * b.imag + a.imag * b.real;
  94. return result;
  95. }
  96.  
  97. //Деление:
  98. struct Complex divideComplex(struct Complex a, struct Complex b)
  99. {
  100. struct Complex result;
  101. double denominator = b.real * b.real + b.imag * b.imag;
  102. if (denominator == 0)
  103. {
  104. printf("Ошибка: деление на ноль!\n");
  105. result.real = result.imag = 0;
  106. return result;
  107. }
  108. result.real = (a.real * b.real + a.imag * b.imag) / denominator;
  109. result.imag = (a.imag * b.real - a.real * b.imag) / denominator;
  110. return result;
  111. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Введите первое комплексное число:
Введите действительную часть: 
Введите мнимую часть: 
Введите второе комплексное число:
Введите действительную часть: 
Введите мнимую часть: 

Первое число: 0.00 + i*0.00
Второе число: 0.00 + i*0.00

Результаты операций:
Сумма: 0.00 + i*0.00
Разность: 0.00 + i*0.00
Произведение: 0.00 + i*0.00
Ошибка: деление на ноль!
Частное: 0.00 + i*0.00