dhodnoycontrol.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <string.h>
  4. #include <math.h>
  5. void vvod(int task, float* arguments) {
  6. if (task == 1) {
  7. printf_s("Ââåäèòå êîýôôèöèåíòû a, b è c: ");
  8. while (scanf_s("%f %f %f", &arguments[0], &arguments[1], &arguments[2]) != 3) {
  9. printf("Îøèáêà ââîäà. Ïîæàëóéñòà, ââåäèòå òðè ÷èñëà: ");
  10. while (getchar() != '\n');
  11. }
  12. }
  13. else if (task == 2) {
  14. printf_s("Ââåäèòå êîýôôèöèåíòû ñèñòåìû (a1, b1, c1, a2, b2, c2): ");
  15. while (scanf_s("%f %f %f %f %f %f", &arguments[0], &arguments[1], &arguments[2],
  16. &arguments[3], &arguments[4], &arguments[5]) != 6) {
  17. printf_s("Îøèáêà ââîäà. Ïîæàëóéñòà, ââåäèòå øåñòü ÷èñåë: ");
  18. while (getchar() != '\n');
  19. }
  20. }
  21. else if (task == 3) {
  22. printf_s("Ââåäèòå ÷èñëî äëÿ âû÷èñëåíèÿ ôàêòîðèàëà: ");
  23. while (scanf_s("%f", &arguments[0]) != 1 || arguments[0] < 0 || arguments[0] != (float)arguments[0]) {
  24. printf_s("Îøèáêà ââîäà. Ïîæàëóéñòà, ââåäèòå íåîòðèöàòåëüíîå öåëîå ÷èñëî: ");
  25. while (getchar() != '\n');
  26. }
  27. }
  28. }
  29. void vivod(int task, float* x, int numRoots) {
  30. if (task == 1) {
  31. if (numRoots == 1) {
  32. printf_s("Êîðåíü óðàâíåíèÿ: %.2f\n", x[0]);
  33. }
  34. else if (numRoots == 2) {
  35. printf_s("Êîðíè óðàâíåíèÿ: %.2f\n%.2f\n", x[0], x[1]);
  36. }
  37. else if (numRoots == 3)
  38. {
  39. printf_s("Àðãóìåíòû íå ìîãóò áûòü îòðèöàòåëüíûìè.\n");
  40. }
  41. else {
  42. printf_s("Íåò äåéñòâèòåëüíûõ êîðíåé.\n");
  43. }
  44. }
  45. else if (task == 2) {
  46. if (numRoots == 0)
  47. {
  48. printf_s("Ðåøåíèå ñèñòåìû: x = %.2f, y = %.2f\n", x[0], x[1]);
  49. }
  50. else
  51. {
  52. printf_s("Ñèñòåìà íå èìååò åäèíñòâåííîãî ðåøåíèÿ.\n");
  53. }
  54. }
  55. else if (task == 3) {
  56. printf_s("Ôàêòîðèàë ÷èñëà %.2f ðàâåí: %.20f\n", x[0], x[1]);
  57. }
  58. }
  59. int Task1(float* arguments, float* x) {
  60. float a = arguments[0];
  61. float b = arguments[1];
  62. float c = arguments[2];
  63. float D = b * b - 4 * a * c;
  64. if (a == 0 || b == 0 || c == 0)
  65. {
  66. return 3;
  67. }
  68. else if (D > 0) {
  69. x[0] = (-b + sqrt(D)) / (2 * a);
  70. x[1] = (-b - sqrt(D)) / (2 * a);
  71. return 2; // Äâà êîðíÿ
  72. }
  73. else if (D == 0) {
  74. x[0] = -b / (2 * a);
  75. return 1; // Îäèí êîðåíü
  76. }
  77. else {
  78. return 0; // Íåò äåéñòâèòåëüíûõ êîðíåé
  79. }
  80. }
  81. int Task2(float* arguments, float* x) {
  82. float a1 = arguments[0], b1 = arguments[1], c1 = arguments[2];
  83. float a2 = arguments[3], b2 = arguments[4], c2 = arguments[5];
  84. float D = a1 * b2 - a2 * b1;
  85. if (D != 0) {
  86. // Ðåøåíèå ñèñòåìû
  87. x[0] = (c1 * b2 - c2 * b1) / D; // x
  88. x[1] = (a1 * c2 - a2 * c1) / D; // y
  89. return 0;
  90. }
  91. else {
  92. return 1;
  93. }
  94. }
  95. double Task3(float n) {
  96. if (n == 0 || n == 1) {
  97. return 1;
  98. }
  99. double result = 1;
  100. for (int i = 2; i <= n; i++) {
  101. result *= i;
  102. }
  103. return result;
  104. }
  105. int main() {
  106. float arguments[6];
  107. float roots[2]; // Ìàññèâ äëÿ õðàíåíèÿ êîðíåé
  108. int task;
  109. setlocale(LC_ALL, "Russian");
  110. printf_s("Ââåäèòå íîìåð çàäàíèÿ (1 - êâàäðàòíîå óðàâíåíèå, 2 - ñèñòåìà óðàâíåíèé, 3 - ôàêòîðèàë): ");
  111. scanf_s("%d", &task);
  112. vvod(task, arguments);
  113. if (task == 1) {
  114. int numRoots = Task1(arguments, roots);
  115. vivod(task, roots, numRoots);
  116. }
  117. else if (task == 2) {
  118. Task2(arguments, roots);
  119. int numRoots = Task2(arguments, roots);
  120. vivod(task, roots, numRoots);
  121. }
  122. else if (task == 3) {
  123. int n = (int)arguments[0];
  124. roots[0] = n; // Ñîõðàíÿåì ÷èñëî äëÿ âûâîäà
  125. roots[1] = Task3(n); // Âû÷èñëÿåì ôàêòîðèàë
  126. vivod(task, roots, 0);
  127. }
  128. return 0;
  129. }