Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SEMESTRALNI PROJEKT
- Program pro vypocet soustavy linearnich rovnic
- - pouzita Gaussova eliminacni metoda
- Milan Bubniak, FEKT 2017 */
- #include "stdafx.h"
- #include "operace.h"
- #include <windows.h>
- #define TRUE 1
- #define FALSE 0
- #define MAX 10 // maximalni pocet neznamych v soustave
- static const char cesta[] = "./rovnice.txt"; // cesta k souboru se soustavou rovnic
- enum menu { zadavani = 1, soubor, konec };
- typedef double matice[MAX - 1][MAX];
- void plneni(int q, matice b);
- void vypis(int q, matice b);
- void nula(int q, int m, matice b);
- void plneniSoubor(int q, matice b, FILE *fileta);
- void vypocet(int q, matice a);
- void onZadat();
- void onSoubor();
- void splitter();
- void plneni(int q, matice b)
- {
- for (char m = 0; m < q; m++)
- {
- for (char n = 0; n < q + 1; n++)
- {
- b[m][n] = 0;
- }
- }
- for (char m = 0; m < q; m++)
- {
- printf(" Zadej koeficienty A,B,... (atd. dle poctu neznamych) a hodnotu Z dle tvaru: A*x + B*y + ... = Z v %.d. rovnici soustavy:\n", m + 1);
- for (char n = 0; n < q + 1; n++)
- {
- if (n == q)
- printf("\tZ: ");
- else
- printf("\t%c: ", n + 65);
- scanf_s("%lf", &b[m][n]);
- }
- }
- }
- void vypis(int q, matice b)
- {
- printf("\n");
- for (char m = 0; m < q; m++)
- {
- for (char n = 0; n < q + 1; n++)
- {
- if (n == q)
- printf(" | %9.3lf", b[m][n]);
- else
- printf("%10.3lf", b[m][n]);
- }
- printf("\n");
- }
- printf("\n");
- }
- void nula(int q, int m, matice b)
- {
- int n = m; int k = m;
- double z;
- while ((b[m][n] == 0) && (m <= (q-1)))
- {
- m += 1;
- if (b[m][n] != 0)
- for (n = 0; n <= q; n++)
- {
- z = b[m][n];
- b[m][n] = b[k][n];
- b[k][n] = z;
- }
- }
- }
- void plneniSoubor(int q, matice b, FILE *fileta)
- {
- for (char m = 0; m < q; m++)
- {
- for (char n = 0; n < q + 1; n++)
- {
- b[m][n] = 0;
- }
- }
- for (char m = 0; m < q; m++)
- {
- for (char n = 0; n < q + 1; n++)
- {
- if (!feof(fileta))
- fscanf_s(fileta, "%lf;", &b[m][n]);
- }
- printf(" %.d. rovnice nactena.\n", m + 1);
- }
- }
- void vypocet(int s, matice a)
- {
- int i, j, f, g, e, c, d;
- double y;
- d = FALSE;
- printf(" Sestavena matice koeficientu:\n");
- vypis(s, a);
- printf(" Nuluji pod diagonalou...\n");
- for (i = 0; i < s - 1; i++)
- {
- nula(s, i, a);
- for (e = 0; e < s - i; e++)
- {
- if (a[i + e][i] != 0)
- {
- y = a[i + e][i] / a[i][i] * (-1.0);
- for (j = 0; j < s + 1; j++)
- {
- a[i][j] *= y;
- a[i + e][j] += a[i][j];
- }
- }
- }
- vypis(s, a);
- Sleep(500);
- for (f = i; f < s; f++)
- {
- c = FALSE;
- for (g = 0; g < s; g++)
- c = (a[f][g] != 0) || c;
- if (!c)
- {
- d = a[f][s + 1] == 0;
- f = s;
- }
- }
- if (!c)
- i = s - 1;
- }
- if (c)
- {
- for (j = (s - 1); j >= 0; j--)
- {
- for (e = (j + 1); e <= (s - 1); e++)
- a[j][s] -= a[j][e];
- a[s][j] = a[j][s] / a[j][j];
- for (i = 0; i <= j; i++)
- a[i][j] *= a[s][j];
- }
- printf(" Vypoctene koreny soustavy:\n\n");
- for (j = 0; j <= (s - 1); j++)
- printf("\tX%d = %lf\n", j + 1, a[s][j]);
- splitter();
- }
- else
- {
- if (d)
- printf("\n Tato soustava rovnic ma nekonecne mnoho reseni!\n");
- else
- printf("\n Tato soustava rovnic nema reseni!\n");
- splitter();
- }
- }
- void onZadat()
- {
- matice ax;
- int sx, cx;
- printf(" Kolik bude v rovnicich neznamych? (Takovy pak jest i pocet rovnic!)\n Poznamka: Maximalni pocet neznamych jest %.d.\n", MAX);
- printf(" Zadej pocet neznamych: ");
- scanf_s("%d", &sx);
- while ((sx < 1) || (sx > MAX))
- {
- printf(" Prosim, nezadavejte nesmysly.\n");
- while ((cx = getchar()) != '\n' && cx != EOF);
- printf(" Zadej pocet neznamych: ");
- scanf_s("%d", &sx);
- }
- splitter();
- plneni(sx, ax);
- splitter();
- vypocet(sx, ax);
- }
- void onSoubor()
- {
- matice ax;
- int sx;
- FILE *soubor;
- if (fopen_s(&soubor, cesta, "r") != 0)
- {
- printf(" Nepodarilo se otevrit soubor: %s\n", cesta);
- splitter();
- return;
- }
- fscanf_s(soubor, "%d;\n", &sx);
- if ((sx < 1) || (sx > MAX))
- {
- printf(" Nepodarilo se v souboru nacist rozmer matice.\n");
- return;
- }
- splitter();
- plneniSoubor(sx, ax, soubor);
- fclose(soubor);
- splitter();
- vypocet(sx, ax);
- }
- void splitter()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- int columns = 0;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
- columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
- printf(" ");
- for (int i = 0; i < columns - 2; i++)
- {
- printf("~");
- }
- printf("\n");
- }
- int main()
- {
- menu volba;
- printf(" Vitejte v programu pro vypocet soustavy linearnich rovnic! Prosim vyberte cislo pozadovane operace:\n\n");
- do
- {
- printf(" 1: Zadat rovnice rucne\n 2: Cist rovnice ze souboru\n 3: Ukoncit program\n");
- splitter();
- printf("\n Volba: ");
- if (scanf_s("%d", &volba) != 1)
- {
- printf(" Neplatna operace!\n");
- splitter();
- while ((getchar()) != '\n');
- continue;
- }
- switch (volba)
- {
- case zadavani:
- {
- system("cls");
- onZadat();
- break;
- }
- case soubor:
- {
- system("cls");
- onSoubor();
- break;
- }
- case konec:
- {
- system("cls");
- printf("Sbohem!\n");
- Sleep(1000);
- return 1;
- break;
- }
- default:
- {
- printf(" Neznama volba!\n");
- splitter();
- break;
- }
- }
- } while (true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement