aboutsummaryrefslogtreecommitdiffstats
path: root/qm.c
blob: 9c66f8a51515da3843d9251637e909c06c3fe99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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");
}