Commit 58d71dc7 by erfeld

Lihtimplikantide leidmise koodi lisandused

parent e94f9aff
Showing with 158 additions and 82 deletions
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
// kood kommenteerida, muutujate nimed, funktsioonide lisamine
typedef struct{ typedef struct{
char kood[5]; char kood[5];
int indeks; int indeks;
}implikandid; }implikandid; // kleepimata implikandid
typedef struct{ typedef struct{
char kood[5]; char kood[5];
int indeks; int indeks;
}implikandid2; }implikandid2; // 1.kleepimise tulemus
typedef struct{
char kood[5];
int indeks;
}implikandid3; // 2.kleepimise tulemus
// Kasutaja sisestatu kontrollimine:
int kontroll(char a[16]){ int kontroll(char a[16]){
int i; int i;
for (i = 0; i < strlen(a); i++){ for (i = 0; i < strlen(a); i++){
...@@ -26,6 +34,8 @@ int kontroll(char a[16]){ ...@@ -26,6 +34,8 @@ int kontroll(char a[16]){
return 0; return 0;
} }
// Kasutaja loogikaväärtuste sisestus:
int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16]){ int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16]){
int i; int i;
printf("%s", tekst); printf("%s", tekst);
...@@ -36,12 +46,12 @@ int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16 ...@@ -36,12 +46,12 @@ int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16
return 1; return 1;
} }
} }
if (strlen(a) == 4){ if (strlen(a) == 4){
do{ do{
printf("\n%s\n", a); printf("\n%s\n", a);
printf("Sisestage K-kaardi TEINE rida: \n"); printf("Sisestage K-kaardi TEINE rida: \n");
scanf("%s", b); scanf("%s", b);
}while(kontroll(b) == 1); }while(kontroll(b) == 1);
do{ do{
printf("\n%s\n", a); printf("\n%s\n", a);
printf("%s\n", b); printf("%s\n", b);
...@@ -74,7 +84,7 @@ int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16 ...@@ -74,7 +84,7 @@ int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16
} }
printf("\n"); printf("\n");
} }
else if (strlen(a) >= 16){ else if (strlen(a) >= 16){
for (i = 0; i < 16; i++){ for (i = 0; i < 16; i++){
if (a[i] != '0' && a[i] != '1'){ if (a[i] != '0' && a[i] != '1'){
...@@ -84,44 +94,30 @@ int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16 ...@@ -84,44 +94,30 @@ int sisestus(const char tekst[40], char a[16], char b[16], char c[16], char d[16
} }
printf("\n"); printf("\n");
printf("\nK-kaardi kujul:\n"); printf("\nK-kaardi kujul:\n");
printf("%c %c %c %c\n", a[0], a[1], a[3], a[2]); printf("%c %c %c %c\n", a[0], a[1], a[3], a[2]);
printf("%c %c %c %c\n", a[4], a[5], a[7], a[6]); printf("%c %c %c %c\n", a[4], a[5], a[7], a[6]);
printf("%c %c %c %c\n", a[12], a[13], a[15], a[14]); printf("%c %c %c %c\n", a[12], a[13], a[15], a[14]);
printf("%c %c %c %c\n", a[8], a[9], a[11], a[10]); printf("%c %c %c %c\n", a[8], a[9], a[11], a[10]);
} }
else{ // Kui kasutaja sisestab mittesobiva märkide arvu, siis programm teeb exit. else{ // Kui kasutaja sisestab mittesobiva märkide arvu, siis programm teeb exit.
printf("Lubamatu sisend: %s\n", a); printf("Lubamatu sisend: %s\n", a);
return 1; return 1;
} }
return 0; return 0;
} }
void info(char a[16], char b[16], char c[16], char d[16]){
char f0; // Reed-Mulleri polünoomi kujul oleva loogikafunktsiooni kohta info andmine:
char f1; void info(char a[16], char b[16], char c[16], char d[16], char f0, char f1, char f2, char f3, char f4, char f5, char f6, char f7, char f8, char f9, char fA, char fB, char fC, char fD, char fE, char fF){
char f2; //char f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, fA, fB, fC, fD, fE, fF;
char f3;
char f4;
char f5;
char f6;
char f7;
char f8;
char f9;
char fA;
char fB;
char fC;
char fD;
char fE;
char fF;
if (strlen(a) >= 16){ if (strlen(a) >= 16){
f0 = a[0]; f0 = a[0];
f1 = a[1]; f1 = a[1];
f2 = a[2]; f2 = a[2];
f3 = a[3]; f3 = a[3];
f4 = a[4]; f4 = a[4];
f5 = a[5]; f5 = a[5];
...@@ -139,19 +135,19 @@ void info(char a[16], char b[16], char c[16], char d[16]){ ...@@ -139,19 +135,19 @@ void info(char a[16], char b[16], char c[16], char d[16]){
else{ else{
f0 = a[0]; f0 = a[0];
f1 = a[1]; f1 = a[1];
f2 = a[3]; f2 = a[3];
f3 = a[2]; f3 = a[2];
f4 = b[0]; f4 = b[0];
f5 = b[1]; f5 = b[1];
f6 = b[3]; f6 = b[3];
f7 = b[2]; f7 = b[2];
f8 = d[0]; f8 = d[0];
f9 = d[1]; f9 = d[1];
fA = d[3]; fA = d[3];
fB = d[2]; fB = d[2];
fC = c[0]; fC = c[0];
fD = c[1]; fD = c[1];
fE = c[3]; fE = c[3];
...@@ -164,28 +160,28 @@ void info(char a[16], char b[16], char c[16], char d[16]){ ...@@ -164,28 +160,28 @@ void info(char a[16], char b[16], char c[16], char d[16]){
else{ else{
printf("Konstant 1 puudub\n"); printf("Konstant 1 puudub\n");
} }
if (fF == '1'){ if (fF == '1'){
printf("PAARITU arv liikmeid\n"); printf("PAARITU arv liikmeid\n");
} }
else{ else{
printf("PAARISarv liikmeid\n"); printf("PAARISarv liikmeid\n");
} }
if (f0 == f8){ if (f0 == f8){
printf("x1 puudub\n"); printf("x1 puudub\n");
} }
else{ else{
printf("x1 olemas\n"); printf("x1 olemas\n");
} }
if (f0 == f4){ if (f0 == f4){
printf("x2 puudub\n"); printf("x2 puudub\n");
} }
else{ else{
printf("x2 olemas\n"); printf("x2 olemas\n");
} }
if (f0 == f2){ if (f0 == f2){
printf("x3 puudub\n"); printf("x3 puudub\n");
} }
...@@ -199,46 +195,44 @@ void info(char a[16], char b[16], char c[16], char d[16]){ ...@@ -199,46 +195,44 @@ void info(char a[16], char b[16], char c[16], char d[16]){
else{ else{
printf("x4 olemas\n"); printf("x4 olemas\n");
} }
if (f7 == '1'){ if (f7 == '1'){
printf("ilma [x1] PAARITU\n"); printf("ilma [x1] PAARITU\n");
} }
else{ else{
printf("ilma [x1] PAARIS\n"); printf("ilma [x1] PAARIS\n");
} }
if (fB == '1'){ if (fB == '1'){
printf("ilma [x2] PAARITU\n"); printf("ilma [x2] PAARITU\n");
} }
else{ else{
printf("ilma [x2] PAARIS\n"); printf("ilma [x2] PAARIS\n");
} }
if (fD == '1'){ if (fD == '1'){
printf("ilma [x3] PAARITU\n"); printf("ilma [x3] PAARITU\n");
} }
else{ else{
printf("ilma [x3] PAARIS\n"); printf("ilma [x3] PAARIS\n");
} }
if (fE == '1'){ if (fE == '1'){
printf("ilma [x4] PAARITU\n"); printf("ilma [x4] PAARITU\n");
} }
else{ else{
printf("ilma [x4] PAARIS\n"); printf("ilma [x4] PAARIS\n");
} } // info funktsiooni lõpp siin
}
// Kontuuride leidmine
void kontuur(char f0, char f1, char f2, char f3, char f4, char f5, char f6, char f7, char f8, char f9, char fA, char fB, char fC, char fD, char fE, char fF){
implikandid implikant[16]; implikandid implikant[16];
int i = 0; int i = 0;
int arv = 0;
int j, k; int j, k;
/*char array[16] = {f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, fA, fB, fC, fD, fE, fF};
for(i = 0; i<16; i++){
if(array[i] == '1'){
arv++;
}
}*/
if(f0 == '1'){ if(f0 == '1'){
strcpy(implikant[i].kood, "0000"); strcpy(implikant[i].kood, "0000");
...@@ -336,69 +330,151 @@ void info(char a[16], char b[16], char c[16], char d[16]){ ...@@ -336,69 +330,151 @@ void info(char a[16], char b[16], char c[16], char d[16]){
implikant[i].indeks = 4; implikant[i].indeks = 4;
i++; i++;
} }
printf("\n"); printf("Indeksid:"); // indeksite test
printf("%s\n", implikant[0].kood);
for(j = 0; j<i; j++){ for(j = 0; j<i; j++){
printf("%d\n", implikant[j].indeks); printf("%d\n", implikant[j].indeks);
} }
int eri = 0; int eri = 0;
implikandid2 implikant2[16]; implikandid2 implikant2[16];
int q; int q;
char arr[5];
int t = 0; int t = 0;
for(j = 0; j<i; j++){
for(k = 0; k<i; k++){ for (j = 0; j < i; j++){
if(implikant[i].indeks - implikant[k].indeks == 1 || implikant[i].indeks - implikant[k].indeks == -1){ for (k = 0; k < i; k++){
if(implikant[i].kood[0] != implikant[k].kood[0]){ if (implikant[j].indeks - implikant[k].indeks == 1 || implikant[j].indeks - implikant[k].indeks == -1){
if(implikant[j].kood[0] != implikant[k].kood[0]){
eri++; eri++;
q = 0; q = 0;
} }
if(implikant[i].kood[1] != implikant[k].kood[1]){ if (implikant[j].kood[1] != implikant[k].kood[1]){
eri++; eri++;
q = 1; q = 1;
} }
if(implikant[i].kood[2] != implikant[k].kood[2]){ if (implikant[j].kood[2] != implikant[k].kood[2]){
eri++; eri++;
q = 2; q = 2;
} }
if(implikant[i].kood[3] != implikant[k].kood[3]){ if (implikant[j].kood[3] != implikant[k].kood[3]){
eri++; eri++;
q = 3; q = 3;
} }
if(eri == 1){ if (eri == 1){
strcpy(implikant2[0].kood, implikant[i].kood); if (q == 0){
printf("%s", implikant2[0].kood); strcpy(implikant2[t].kood, implikant[j].kood); // des, source
/*printf("\n"); implikant2[t].kood[q] = '-';
arr[0] = '-'; printf("%s\n", implikant2[t].kood);
arr[1] = implikant[i].kood[1]; }
arr[2] = implikant[i].kood[2]; if (q == 1){
arr[3] = implikant[i].kood[3]; strcpy(implikant2[t].kood, implikant[j].kood);
printf("%s", arr);*/ implikant2[t].kood[q] = '-';
/*if(q == 0){ printf("%s\n", implikant2[t].kood);
implikant2[t].kood[0] = '-'; }
} if (q == 2){
t++;*/ strcpy(implikant2[t].kood, implikant[j].kood);
} implikant2[t].kood[q] = '-';
printf("%s\n", implikant2[t].kood);
}
if (q == 3){
strcpy(implikant2[t].kood, implikant[j].kood);
implikant2[t].kood[q] = '-';
printf("%s\n", implikant2[t].kood);
}
t++;
}
eri = 0;
} }
} }
} }
int uhtedeArv;
for(j = 0; j<t; j++){
uhtedeArv = 0;
if(implikant2[j].kood[0] == '1'){
uhtedeArv++;
}
if(implikant2[j].kood[1] == '1'){
uhtedeArv++;
}
if(implikant2[j].kood[2] == '2'){
uhtedeArv++;
}
if(implikant2[j].kood[3] == '3'){
uhtedeArv++;
}
implikant2[j].indeks = uhtedeArv;
}
printf("\n");
printf("\n");
printf("\n");
implikandid3 implikant3[16];
t = 0;
for (j = 0; j < i; j++){
for (k = 0; k < i; k++){
if (implikant2[j].indeks - implikant2[k].indeks == 1 || implikant2[j].indeks - implikant2[k].indeks == -1){
if (implikant2[j].kood[0] != implikant2[k].kood[0]){
eri++;
q = 0;
}
if (implikant2[j].kood[1] != implikant2[k].kood[1]){
eri++;
q = 1;
}
if (implikant2[j].kood[2] != implikant2[k].kood[2]){
eri++;
q = 2;
}
if (implikant2[j].kood[3] != implikant2[k].kood[3]){
eri++;
q = 3;
}
if (eri == 1){
if (q == 0){
strcpy(implikant3[t].kood, implikant2[j].kood); // des, source
implikant3[t].kood[q] = '-';
printf("%s\n", implikant3[t].kood);
}
if (q == 1){
strcpy(implikant3[t].kood, implikant2[j].kood);
implikant3[t].kood[q] = '-';
printf("%s\n", implikant3[t].kood);
}
if (q == 2){
strcpy(implikant3[t].kood, implikant2[j].kood);
implikant3[t].kood[q] = '-';
printf("%s\n", implikant3[t].kood);
}
if (q == 3){
strcpy(implikant3[t].kood, implikant2[j].kood);
implikant3[t].kood[q] = '-';
printf("%s\n", implikant3[t].kood);
}
t++;
}
eri = 0;
}
}
}
} }
int main(void){ int main(void){
char a[16], b[16], c[16], d[16]; char a[16], b[16], c[16], d[16];
char f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, fA, fB, fC, fD, fE, fF;
int input; int input;
input = sisestus("Sisestage 4x4 Karnaugh kaart voi 16 loogikavaartust: \n", a, b, c, d); input = sisestus("Sisestage 4x4 Karnaugh kaart voi 16 loogikavaartust: \n", a, b, c, d);
if (input == 1){ if (input == 1){
return 1; return 1;
} }
info(a, b, c, d); info(a, b, c, d, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, fA, fB, fC, fD, fE, fF);
kontuur(f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, fA, fB, fC, fD, fE, fF);
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment