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