Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <chrono>
- #include <locale.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <string>
- #define SIZE 4
- #define A_SIZE 4
- #define Y_SIZE 11
- #define S_SIZE 3
- #define R_SIZE 3
- #define _CRT_SECURE_NO_WARNINGS
- using namespace std;
- using namespace std::chrono;
- bool mnoj(int arr[], int len, int res) {
- for (int i = 0; i < len; i++) {
- if (arr[i] == res) return true;
- }
- return false;
- }
- int toDec(bool a[]) {
- int num = 0;
- for (int i = SIZE - 1; i >= 0; i--)
- num += (1 << (SIZE - 1) - i) * a[i];
- return num;
- }
- void updateY(bool a[], bool x[], bool y[]) {
- y[1] = !a[1] * !a[2] * !a[3];
- y[2] = !a[1] * !a[2] * a[3];
- y[3] = !a[1] * !a[2] * a[3];
- y[4] = !a[1] * a[2] * !a[3] * x[0];
- y[5] = !a[1] * a[2] * a[3];
- y[6] = !a[1] * a[2] * a[3];
- y[7] = !a[1] * a[2] * a[3];
- y[8] = a[1] * !a[2] * !a[3] * x[1] * x[2];
- y[9] = a[1] * !a[2] * a[3] * x[3];
- y[10] = a[1] * !a[2] * !a[3] * x[1] * !x[2];
- }
- void updateS(bool a[], bool s[], bool x[]) {
- s[1] = !a[1] * a[2] * a[3];
- s[2] = !a[1] * !a[2] * a[3] + a[1] * !a[2] * !a[3] * !x[1];
- s[3] = !a[1] * !a[2] * !a[3] + !a[1] * a[2] * !a[3] * !x[0] + !a[1] * a[2] * !a[3] * x[0] + a[1] * !a[2] * !a[3] * x[1] * x[2];
- }
- void updateR(bool a[], bool x[], bool r[]) {
- r[1] = a[1] * !a[2] * !a[3] * x[1] * !x[2] + a[1] * !a[2] * !a[3] * !x[1] + a[1] * !a[2] * a[3] * x[3] + a[1] * !a[2] * a[3] * !x[3];
- r[2] = !a[1] * a[2] * a[3];
- r[3] = !a[1] * !a[2] * a[3] + !a[1] * a[2] * a[3] + a[1] * !a[2] * a[3] * x[3] + a[1] * !a[2] * a[3] * !x[3];
- }
- void changeA(bool a[], bool s[], bool r[]) {
- for (int i = 1; i <= A_SIZE; i++) {
- if (s[i])
- a[i] = true;
- if (r[i])
- a[i] = false;
- }
- }
- void printVector(bool v[]) {
- for (int i = 1; i < Y_SIZE; i++)
- printf("%d ", v[i]);
- }
- void printVectorA(bool v[]) {
- for (int i = 1; i < A_SIZE; i++)
- printf("%d ", v[i]);
- }
- void addState(int S, bool a[], bool y[]) {
- printf("Я сейчас в S%d\n", S);
- printf("Вывод состояний a: ");
- printVectorA(a);
- printf("\n");
- printf("Вывод состояний y: ");
- printVector(y);
- /*for (int i = 1; i <= Y_SIZE; i++) {
- if (y[i])
- printf("%d ", i);
- }*/
- printf("\n\n");
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- printf("1. Логические выражения. 2. Множество единичных наборов.");
- int check;
- scanf_s("%d", &check);
- if (check == 1) {
- bool x[SIZE] = { false };
- for (int i = 0; i < SIZE; i++) {
- printf("Введите x: ");
- scanf_s("%hhu", &x[i]);
- }
- auto start = high_resolution_clock::now();
- printf("\n\n");
- bool a[A_SIZE + 1] = { false };
- bool y[Y_SIZE + 1] = { false };
- bool s[S_SIZE + 1] = { false };
- bool r[R_SIZE + 1] = { false };
- bool checkFirst[1 << SIZE] = { false };
- bool checkSecond[1 << SIZE] = { false };
- int S;
- do {
- S = toDec(a);
- if (checkFirst[S])
- checkSecond[S] = true;
- else
- checkFirst[S] = true;
- if (checkSecond[S])
- break;
- addState(S, a, y);
- updateY(a, x, y);
- updateR(a, x, r);
- updateS(a, s, x);
- changeA(a, s, r);
- } while (!checkSecond[S]);
- if (S == 0) {
- addState(S, a, y);
- auto stop = high_resolution_clock::now();
- auto elapsed_time = duration_cast<duration<double, milli>>(stop - start);
- printf_s("\nВремя работы: %f секунд\n", elapsed_time.count() / 1000);
- printf("ЗАВЕРШИЛ\n");
- }
- else {
- addState(S, a, y);
- auto stop = high_resolution_clock::now();
- auto elapsed_time = duration_cast<duration<double, milli>>(stop - start);
- printf_s("\nВремя работы: %f секунд\n", elapsed_time.count() / 1000);
- printf("ЦИКЛ\n");
- }
- }
- else {
- int y1[16] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
- };
- int y2[16] = {
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
- };
- int y3[16] = {
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
- };
- int y4[8] = {
- 40, 41, 42, 43, 44, 45, 46, 47
- };
- int y5[16] = {
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
- };
- int y6[16] = {
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
- };
- int y7[16] = {
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
- };
- int y8[4] = {
- 70, 71, 78, 79
- };
- int y9[8] = {
- 81, 83, 85, 87, 89, 91, 93, 95
- };
- int y10[4] = {
- 68, 69, 76, 77
- };
- int w1[16] = {
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
- };
- int w2[24] = {
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 64, 65, 66, 67, 73, 74, 75, 72
- };
- int w3[36] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 40, 41, 42, 43, 44, 45, 46, 47, 32,33,34,35,36,37,38,39, 70, 71, 78, 79
- };
- int u1[28] = {
- 68, 69, 77, 76, 64, 65, 66, 67, 73, 74, 75, 72, 81, 83, 85, 87, 89, 91, 93, 95, 80, 82, 84, 86, 88, 90, 92, 94
- };
- int u2[16] = {
- 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
- };
- int u3[48] = {
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 81, 83, 85, 87, 89, 91, 93, 95, 80, 82, 84, 86, 88, 90, 92, 94, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
- };
- int X[4];
- int Y[10];
- int S[] = { 0, 0, 0 };
- for (int i = 0; i < 4; i++) scanf_s("%d", &X[i]);
- auto start = high_resolution_clock::now();
- int CS[7];
- auto stop = high_resolution_clock::now();
- auto elapsed_time = duration_cast<duration<double, milli>>(stop - start);
- printf_s("\nВремя работы: %f секунд\n", elapsed_time.count() / 1000);
- auto start2 = high_resolution_clock::now();
- for (int i = 0; i < 3; i++) CS[i] = S[i];
- for (int i = 0; i < 4; i++) CS[i + 3] = X[i];
- int res;
- for (int i = 0; i < 8; i++) {
- res = 0;
- for (int j = 6; j >= 0; j--) {
- res += pow(2, j) * CS[6 - j];
- }
- printf_s("\nS(");
- for (int j = 0; j < 3; j++) printf_s("%d", S[j]);
- if (mnoj(y1, 16, res)) Y[0] = 1;
- else Y[0] = 0;
- if (mnoj(y2, 16, res)) Y[1] = 1;
- else Y[1] = 0;
- if (mnoj(y3, 16, res)) Y[2] = 1;
- else Y[2] = 0;
- if (mnoj(y4, 8, res)) Y[3] = 1;
- else Y[3] = 0;
- if (mnoj(y5, 16, res)) Y[4] = 1;
- else Y[4] = 0;
- if (mnoj(y6, 16, res)) Y[5] = 1;
- else Y[5] = 0;
- if (mnoj(y7, 16, res)) Y[6] = 1;
- else Y[6] = 0;
- if (mnoj(y8, 4, res)) Y[7] = 1;
- else Y[7] = 0;
- if (mnoj(y9, 8, res)) Y[8] = 1;
- else Y[8] = 0;
- if (mnoj(y10, 4, res)) Y[9] = 1;
- else Y[9] = 0;
- if (mnoj(w1, 16, res)) S[0] = 1;
- if (mnoj(w2, 24, res)) S[1] = 1;
- if (mnoj(w3, 36, res)) S[2] = 1;
- if (mnoj(u1, 28, res)) S[0] = 0;
- if (mnoj(u2, 16, res)) S[1] = 0;
- if (mnoj(u3, 48, res)) S[2] = 0;
- printf_s(") -> S(");
- for (int j = 0; j < 3; j++) printf_s("%d", S[j]);
- printf_s(") Y(1-10):");
- for (int j = 0; j < 10; j++) printf_s(" %d", Y[j]);
- for (int j = 0; j < 3; j++) CS[j] = S[j];
- if (S[0] + S[1] + S[2] == 0) {
- printf_s("\nПрограмма завершена");
- break;
- }
- }
- auto stop2 = high_resolution_clock::now();
- auto elapsed_time2 = duration_cast<duration<double, milli>>(stop2 - start2);
- printf_s("\nВремя работы: %f секунд\n", elapsed_time2.count() / 1000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement