|
@@ -0,0 +1,270 @@
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <string.h>
|
|
|
|
+#include <math.h>
|
|
|
|
+
|
|
|
|
+void calculateSquareEquation(float a, float b, float c, float* x1, float* x2) {
|
|
|
|
+ float D;
|
|
|
|
+ D = b * b - 4 * a * c;
|
|
|
|
+ if (a == 0) {
|
|
|
|
+ *x1 = -c / b;
|
|
|
|
+ }
|
|
|
|
+ else if (D > 0) {
|
|
|
|
+ *x1 = (-b + sqrt(D)) / (2 * a);
|
|
|
|
+ *x2 = (-b - sqrt(D)) / (2 * a);
|
|
|
|
+ }
|
|
|
|
+ else if (D == 0) {
|
|
|
|
+ *x1 = -b / (2 * a);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void SLU(float a, float b, float c, float a1, float b1, float c1, float* x1, float* x2) {
|
|
|
|
+
|
|
|
|
+ float d;
|
|
|
|
+ d = a * b1 - a1 * b;
|
|
|
|
+ if (d)
|
|
|
|
+ {
|
|
|
|
+ *x1 = (c * b1 - c1 * b) / d;
|
|
|
|
+ *x2 = (a * c1 - a1 * c) / d;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void inputData(int a,int* f, float* a1, float* b1, float* c1, float* a2, float* b2, float* c2)
|
|
|
|
+{
|
|
|
|
+ char buf[100];
|
|
|
|
+ switch (a) {
|
|
|
|
+ case 1:
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå a: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", a1, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå b: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", b1, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå c: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", c1, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case 2:
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå a1: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", a1, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå b1: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", b1, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå c1: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", c1, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå a2: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", a2, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå b2: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", b2, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå c2: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%f %n", c2, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå 1 ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case 3:
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ printf("Ââåäèòå a: ");
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%d %n", f, &n) == 1 && buf[n] == '\0')
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå öåëîå ÷èñëî. Ïîïðîáóé åùå ðàç\n", buf);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void GetData(int a,int f, float x1, float x2)
|
|
|
|
+{
|
|
|
|
+ switch (a) {
|
|
|
|
+ case 1:
|
|
|
|
+ if (x1 != 0 && x2 != 0)
|
|
|
|
+ {
|
|
|
|
+ printf("Êîðíè óðàâíåíèÿ x1 = %f x2 = %f", x1,x2);
|
|
|
|
+ }
|
|
|
|
+ else if(x1 != 0)
|
|
|
|
+ {
|
|
|
|
+ printf("Êîðåíü óðàâíåíèÿ x = %f", x1);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ printf("Êîðíåé íåò");
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case 2:
|
|
|
|
+ if (x1 == 0 && x2 == 0)
|
|
|
|
+ {
|
|
|
|
+ printf("Ñóùåñòâóåò áåñêîíå÷íîå ìíîæåñòâî ðåøåíèé");
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ printf("X ðàâåí %f Y ðàâåí %f", x1, x2);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ case 3:
|
|
|
|
+ printf("Ôàêòîðèàëà ðàâåí %d", f);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Factorial(int* f)
|
|
|
|
+{
|
|
|
|
+ int x, xF, d;
|
|
|
|
+ xF = *f;
|
|
|
|
+ x = xF;
|
|
|
|
+ for (d = 1;d < x;d++)
|
|
|
|
+ {
|
|
|
|
+ xF = xF * (x - d);
|
|
|
|
+ }
|
|
|
|
+ *f = xF;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void main()
|
|
|
|
+{
|
|
|
|
+ system("chcp 1251");
|
|
|
|
+ float a1, b1, c1, a2, b2, c2, x1 = 0, x2 = 0;
|
|
|
|
+ int a,f = 0;
|
|
|
|
+ char buf[100];
|
|
|
|
+ printf("Âûáåðåòå íîìåð çàäàíèÿ: \n");
|
|
|
|
+ printf("1. Êâàäðàòíîå óðàâíåíèå. \n");
|
|
|
|
+ printf("2. Ñèñòåìà äâóõ ëèíåéíûõ óðàâíåíèé. \n");
|
|
|
|
+ printf("3. Ïåðåñòàíîâêè (ôàêòîðèàë). \n");
|
|
|
|
+ for (;;)
|
|
|
|
+ {
|
|
|
|
+ if (fgets(buf, sizeof buf, stdin) == NULL) {
|
|
|
|
+ printf("Input closed\n");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ int n;
|
|
|
|
+ if (sscanf_s(buf, "%d %n", &a, &n) == 1 && buf[n] == '\0' && a >= 1 && a <=3)
|
|
|
|
+ {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ printf("<%s> ýòî íå íîìåð çàäàíèÿ. Ïîïðîáóé åùå ðàç.\n", buf);
|
|
|
|
+ }
|
|
|
|
+ switch (a) {
|
|
|
|
+ case 1:
|
|
|
|
+ inputData(a,&f, &a1, &b1, &c1, &a2, &b2, &c2);
|
|
|
|
+ calculateSquareEquation(a1, b1, c1, &x1, &x2);
|
|
|
|
+ GetData(a,f, x1, x2);
|
|
|
|
+ break;
|
|
|
|
+ case 2:
|
|
|
|
+ inputData(a, &f, &a1, &b1, &c1, &a2, &b2, &c2);
|
|
|
|
+ SLU(a1, b1, c1, a2, b2, c2, &x1, &x2);
|
|
|
|
+ GetData(a, f, x1, x2);
|
|
|
|
+ break;
|
|
|
|
+ case 3:
|
|
|
|
+ inputData(a, &f, &a1, &b1, &c1, &a2, &b2, &c2);
|
|
|
|
+ Factorial(&f);
|
|
|
|
+ GetData(a,f, x1, x2);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+}
|