VvodnoeZadanie.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. //Ïðîòîòèïû ôóíêöèé
  7. void input_parameters(double *a, double *b, double *c, int task);
  8. void output_results(double result1, double result2, int task);
  9. void solve_quadratic_equation();
  10. void solve_linear_sustem();
  11. void solve_factorial();
  12. int validate_input(int min, int max);
  13. int main()
  14. {
  15. system("chcp 1251>null");
  16. int choice;
  17. do {
  18. //Âûâîä ìåíþ
  19. printf("Âûáåðèòå çàäà÷ó \n");
  20. printf("1. Ðåøåíèå êâàäðàòíîãî óðàâíåíèÿ\n");
  21. printf("2. Ðåøåíèå ñèñòåìû äâóõ ëèíåéíûõ óðàâíåíèé\n");
  22. printf("3. Ïåðåñòàíîâêè (Ôàêòîðèàë\n");
  23. printf("0. Âûõîä\n");
  24. printf("Ââåäèòå íîìåð çàäà÷è:");
  25. choice = validate_input(0, 3);
  26. //Âåòâëåíèå ïî âûáîðó
  27. switch (choice) {
  28. case 1:
  29. solve_quadratic_equation();
  30. break;
  31. case 2:
  32. solve_linear_sustem();
  33. break;
  34. case 3:
  35. solve_factorial();
  36. break;
  37. case 0:
  38. printf("Âûõîä èç ïðîãðàììû \n");
  39. break;
  40. default:
  41. printf("Íåâåðíûé âûáîð! \n");
  42. break;
  43. }
  44. } while (choice != 0);
  45. return 0;
  46. }
  47. //Ôóíêöèÿ äëÿ âàëèäàöèè ââîäà
  48. int validate_input(int min, int max) {
  49. int value;
  50. while (scanf("%d", &value) != 1 || value < min || value > max) {
  51. printf("Îøèáêà! Ââåäèòå ÷èñëî îò %d äî %d: ", min, max);
  52. while (getchar() != '\n'); //Î÷èùàåì áóôåð
  53. }
  54. return value;
  55. }
  56. //Ôóíêöèÿ äëÿ ââîäà ïàðàìåòðîâ
  57. void input_parameters(double* a, double* b, double* c, int task) {
  58. if (task == 1) {
  59. //Ââîä ïàðàìåòðîâ äëÿ êâàäðàòíîãî óðàâíåíèÿ
  60. printf("Ââåäèòå êîýôôèöèåíòû à, b è c äëÿ êâàäðàòíîãî óðàâíåíèÿ: \n");
  61. while (scanf("%lf %lf %lf", a, b, c) != 3) {
  62. printf("Îøèáêà! Ââåäèòå òðè ÷èñëà ÷åðåç ïðîáåë: ");
  63. while (getchar() != '\n');
  64. }
  65. }
  66. else if (task == 2) {
  67. //Ââîä ïàðàìåòðîâ äëÿ ñèñòåìû ëèíåéíûõ óðàâíåíèé
  68. printf("Ââåäèòå êîýôôèöèåíòû äëÿ ñèñòåìû óðàâíåíèé: \n");
  69. while (scanf("%lf %lf %lf", a, b, c) != 3) {
  70. printf("Îøèáêà! Ââåäèòå òðè ÷èñëà ÷åðåç ïðîáåë: ");
  71. while (getchar() != '\n');
  72. }
  73. }
  74. }
  75. //Ôóíêöèÿ äëÿ âûâîäà ðåçóëüòàòîâ
  76. void output_results(double result1, double result2, int task) {
  77. if (task == 1) {
  78. printf("Êîðíè êâàäðàòíîãî óðàâíåíèÿ: %.2lf è %.2lf \n", result1, result2);
  79. }
  80. else if (task == 2) {
  81. printf("Ðåøåíèå ñèñòåìû óðàâíåíèé: x = %.2lf, y = %.2lf \n", result1, result2);
  82. }
  83. else if (task == 3) {
  84. printf("Ôàêòîðèàë ÷èñëà: %.0lf \n", result1);
  85. }
  86. }
  87. void solve_quadratic_equation() {
  88. double a, b, c, discriminant, root1, root2;
  89. input_parameters(&a, &b, &c, 1);
  90. discriminant = b * b - 4 * a * c;
  91. if (discriminant > 0) {
  92. root1 = (-b + sqrt(discriminant)) / (2 * a);
  93. root2 = (-b - sqrt(discriminant)) / (2 * a);
  94. output_results(root1, root2, 1);
  95. }
  96. else if (discriminant == 0) {
  97. root1 = -b / (2 * a);
  98. output_results(root1, root1, 1);
  99. }
  100. else {
  101. printf("Êîðíåé íåò (äèñêðèìèíàíò ìåíüøå íóëÿ). \n");
  102. }
  103. }
  104. //Ðåøåíèå ñèñòåìû äâóõ ëèíåéíûõ óðàâíåíèé
  105. void solve_linear_sustem() {
  106. double a1, b1, c1, a2, b2, c2, determinant, x, y;
  107. printf("Ââåäèòå êîýôôèöèåíòû äëÿ ñèñòåìû óðàâíåíèé \n");
  108. while (scanf("%lf %lf %lf %lf %lf %lf", &a1, &b1, &c1, &a2, &b2, &c2) != 6) {
  109. printf("Îøèáêà! Ââåäèòå øåñòü ÷èñåë ÷åðåç ïðîáåë: ");
  110. while (getchar() != '\n');
  111. }
  112. determinant = a1 * b2 - a2 * b1;
  113. if (determinant != 0) {
  114. x = (c1 * b2 - c2 * b1) / determinant;
  115. y = (a1 * c2 - a2 * c1) / determinant;
  116. output_results(x, y, 2);
  117. }
  118. else {
  119. printf("Ñèñòåìà íå èìååò åäèíñòâåííîãî ðåøåíèÿ. \n");
  120. }
  121. }
  122. //Âû÷èñëåíèå ôàêòîðèàëà
  123. void solve_factorial() {
  124. int n;
  125. double factorial = 1;
  126. printf("Ââåäèòå ÷èñëî äëÿ âû÷èñëåíèÿ ôàêòîðèàëà: ");
  127. n = validate_input(0, 20); // Îãðàíè÷åíèå íà ìàêñèìàëüíûé ââîä äëÿ èçáåãàíèÿ ïåðåïîëíåíèÿ
  128. for (int i = 1; i <= n; i++) {
  129. factorial *= i;
  130. }
  131. output_results(factorial, 0, 3);
  132. }