123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdint.h>
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- //Ïðîòîòèïû ôóíêöèé
- void input_parameters(double *a, double *b, double *c, int task);
- void output_results(double result1, double result2, int task);
- void solve_quadratic_equation();
- void solve_linear_sustem();
- void solve_factorial();
- int validate_input(int min, int max);
- int main()
- {
- system("chcp 1251>null");
- int choice;
- do {
- //Âûâîä ìåíþ
- printf("Âûáåðèòå çàäà÷ó \n");
- printf("1. Ðåøåíèå êâàäðàòíîãî óðàâíåíèÿ\n");
- printf("2. Ðåøåíèå ñèñòåìû äâóõ ëèíåéíûõ óðàâíåíèé\n");
- printf("3. Ïåðåñòàíîâêè (Ôàêòîðèàë\n");
- printf("0. Âûõîä\n");
- printf("Ââåäèòå íîìåð çàäà÷è:");
- choice = validate_input(0, 3);
- //Âåòâëåíèå ïî âûáîðó
- switch (choice) {
- case 1:
- solve_quadratic_equation();
- break;
- case 2:
- solve_linear_sustem();
- break;
- case 3:
- solve_factorial();
- break;
- case 0:
- printf("Âûõîä èç ïðîãðàììû \n");
- break;
- default:
- printf("Íåâåðíûé âûáîð! \n");
- break;
- }
- } while (choice != 0);
- return 0;
- }
- //Ôóíêöèÿ äëÿ âàëèäàöèè ââîäà
- int validate_input(int min, int max) {
- int value;
- while (scanf("%d", &value) != 1 || value < min || value > max) {
- printf("Îøèáêà! Ââåäèòå ÷èñëî îò %d äî %d: ", min, max);
- while (getchar() != '\n'); //Î÷èùàåì áóôåð
- }
- return value;
- }
- //Ôóíêöèÿ äëÿ ââîäà ïàðàìåòðîâ
- void input_parameters(double* a, double* b, double* c, int task) {
- if (task == 1) {
- //Ââîä ïàðàìåòðîâ äëÿ êâàäðàòíîãî óðàâíåíèÿ
- printf("Ââåäèòå êîýôôèöèåíòû à, b è c äëÿ êâàäðàòíîãî óðàâíåíèÿ: \n");
- while (scanf("%lf %lf %lf", a, b, c) != 3) {
- printf("Îøèáêà! Ââåäèòå òðè ÷èñëà ÷åðåç ïðîáåë: ");
- while (getchar() != '\n');
- }
- }
- else if (task == 2) {
- //Ââîä ïàðàìåòðîâ äëÿ ñèñòåìû ëèíåéíûõ óðàâíåíèé
- printf("Ââåäèòå êîýôôèöèåíòû äëÿ ñèñòåìû óðàâíåíèé: \n");
- while (scanf("%lf %lf %lf", a, b, c) != 3) {
- printf("Îøèáêà! Ââåäèòå òðè ÷èñëà ÷åðåç ïðîáåë: ");
- while (getchar() != '\n');
- }
- }
- }
- //Ôóíêöèÿ äëÿ âûâîäà ðåçóëüòàòîâ
- void output_results(double result1, double result2, int task) {
- if (task == 1) {
- printf("Êîðíè êâàäðàòíîãî óðàâíåíèÿ: %.2lf è %.2lf \n", result1, result2);
- }
- else if (task == 2) {
- printf("Ðåøåíèå ñèñòåìû óðàâíåíèé: x = %.2lf, y = %.2lf \n", result1, result2);
- }
- else if (task == 3) {
- printf("Ôàêòîðèàë ÷èñëà: %.0lf \n", result1);
- }
- }
- void solve_quadratic_equation() {
- double a, b, c, discriminant, root1, root2;
- input_parameters(&a, &b, &c, 1);
- discriminant = b * b - 4 * a * c;
- if (discriminant > 0) {
- root1 = (-b + sqrt(discriminant)) / (2 * a);
- root2 = (-b - sqrt(discriminant)) / (2 * a);
- output_results(root1, root2, 1);
- }
- else if (discriminant == 0) {
- root1 = -b / (2 * a);
- output_results(root1, root1, 1);
- }
- else {
- printf("Êîðíåé íåò (äèñêðèìèíàíò ìåíüøå íóëÿ). \n");
- }
- }
- //Ðåøåíèå ñèñòåìû äâóõ ëèíåéíûõ óðàâíåíèé
- void solve_linear_sustem() {
- double a1, b1, c1, a2, b2, c2, determinant, x, y;
- printf("Ââåäèòå êîýôôèöèåíòû äëÿ ñèñòåìû óðàâíåíèé \n");
- while (scanf("%lf %lf %lf %lf %lf %lf", &a1, &b1, &c1, &a2, &b2, &c2) != 6) {
- printf("Îøèáêà! Ââåäèòå øåñòü ÷èñåë ÷åðåç ïðîáåë: ");
- while (getchar() != '\n');
- }
- determinant = a1 * b2 - a2 * b1;
- if (determinant != 0) {
- x = (c1 * b2 - c2 * b1) / determinant;
- y = (a1 * c2 - a2 * c1) / determinant;
- output_results(x, y, 2);
- }
- else {
- printf("Ñèñòåìà íå èìååò åäèíñòâåííîãî ðåøåíèÿ. \n");
- }
- }
- //Âû÷èñëåíèå ôàêòîðèàëà
- void solve_factorial() {
- int n;
- double factorial = 1;
- printf("Ââåäèòå ÷èñëî äëÿ âû÷èñëåíèÿ ôàêòîðèàëà: ");
- n = validate_input(0, 20); // Îãðàíè÷åíèå íà ìàêñèìàëüíûé ââîä äëÿ èçáåãàíèÿ ïåðåïîëíåíèÿ
- for (int i = 1; i <= n; i++) {
- factorial *= i;
- }
- output_results(factorial, 0, 3);
- }
|