diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | qm.c | 122 | ||||
-rw-r--r-- | qm.h | 27 |
4 files changed, 156 insertions, 0 deletions
@@ -1,3 +1,5 @@ +quineMcCluskey + # Prerequisites *.d diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd56f43 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +./quineMcCluskey : qm.c + gcc qm.c -o ./quineMcCluskey + +clean : + rm -f ./quineMcCluskey @@ -0,0 +1,122 @@ +#include <stdio.h> +#include <stdlib.h> +#include <limits.h> +#include <stdbool.h> +#include <string.h> +#include "qm.h" + +#define VALID_VARIABLES "ABCDEFGHIJKLMNOPQRSTUVXYZ" + +int main(int argc, char* argv[]) { + if (argc != 2) { + fprintf(stderr, "Incorrect number of arguments provided.\n"); + } + + parseDNFStr(argv[1]); +} + +DNF parseDNFStr(const char* inpStr) { + //remove whitespace + char inpBfr[strlen(inpStr)]; + int i = 0, j = 0; + while (inpStr[i] != '\0') { + if (inpStr[i] != ' ') { + inpBfr[j++] = inpStr[i]; + } + i++; + } + inpBfr[j] = '\0'; + + DNF dnf; + dnf.numTerms = strcnt(inpBfr, '+') + 1; + + //split up, by '+'s + const char* delim = "+"; + char* subStr = strtok(inpBfr, delim); + i = 0; + while (subStr != NULL) { + dnf.terms[i++] = parseDNFTerm(subStr); + // printf("term %i: '%s'\n", i, subStr); + subStr = strtok(NULL, delim); + } + + // printf("%i terms\n", dnf.numTerms); + printForm(dnf); + printForm2(dnf); + return dnf; +} + +DNFTerm parseDNFTerm(const char* termStr) { + DNFTerm term; + term.numVars = 0; + char c; + for (int i = 0; i < INT_MAX; i++) { + c = termStr[i]; + if (c == '\0') { + break; + } + if (strcnt(VALID_VARIABLES, c) == 1) { + term.vars[term.numVars] = (Variable){c, termStr[i - 1] == '-'}; + // printf("%i %c\n", term.numVars, term.vars[term.numVars]); + term.numVars++; + } + } + return term; +} + +// returns an integer of how many times a char was found in a char +// array +unsigned int strcnt(const char* str, const char cnt) { + int count = 0; + for (int i = 0; i < INT_MAX; i++) { + char c = str[i]; + if (c == '\0') { + break; + } + if (c == cnt) { + count++; + } + } + return count; +} + +void printForm(const DNF form) { + for (int i = 0; i < form.numTerms; i++) { + printf("("); + DNFTerm term = form.terms[i]; + + for (int j = 0; j < term.numVars; j++) { + if (term.vars[j].not_) { + printf("¬"); + } + printf("%c", term.vars[j].var); + if (term.numVars - 1 != j) { + printf("∧"); + } + } + + if (form.numTerms - 1 != i) { + printf(") ∨ "); + } + } + printf(")\n"); +} + +void printForm2(const DNF form) { + for (int i = 0; i < form.numTerms; i++) { + DNFTerm term = form.terms[i]; + + for (int j = 0; j < term.numVars; j++) { + printf("%c", term.vars[j].var); + if (term.vars[j].not_) { + printf("'"); + } + } + + if (form.numTerms - 1 != i) { + printf(" + "); + } + } + printf("\n"); +} + @@ -0,0 +1,27 @@ +#ifndef QM_C +#define QM_C + +#include <string.h> + +typedef struct { + char var; + bool not_; +} Variable; + +typedef struct { + unsigned int numVars; + Variable vars[26]; +} DNFTerm; + +typedef struct { + unsigned int numTerms; + DNFTerm terms[100]; +} DNF; + +unsigned int strcnt(const char* str, const char cnt); +DNF parseDNFStr(const char* inpStr); +DNFTerm parseDNFTerm(const char* termStr); +void printForm(const DNF form); +void printForm2(const DNF form); + +#endif
\ No newline at end of file |