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("Сумма: ");
  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. // 2. Функция для ввода комплексного числа
  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. // 3. Функция для вывода комплексного числа
  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. // 4. Функции арифметических операций
  71.  
  72. // Сложение
  73. struct Complex addComplex(struct Complex a, struct Complex b) {
  74. struct Complex result;
  75. result.real = a.real + b.real;
  76. result.imag = a.imag + b.imag;
  77. return result;
  78. }
  79.  
  80. // Вычитание
  81. struct Complex subtractComplex(struct Complex a, struct Complex b) {
  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. struct Complex result;
  91. result.real = a.real * b.real - a.imag * b.imag;
  92. result.imag = a.real * b.imag + a.imag * b.real;
  93. return result;
  94. }
  95.  
  96. // Деление
  97. struct Complex divideComplex(struct Complex a, struct Complex b) {
  98. struct Complex result;
  99. double denominator = b.real * b.real + b.imag * b.imag;
  100. if (denominator == 0) {
  101. printf("Ошибка: деление на ноль!\n");
  102. result.real = result.imag = 0;
  103. return result;
  104. }
  105. result.real = (a.real * b.real + a.imag * b.imag) / denominator;
  106. result.imag = (a.imag * b.real - a.real * b.imag) / denominator;
  107. return result;
  108. }
Success #stdin #stdout 0.01s 5288KB
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