#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#define AND 0
#define OR 1
#define XOR 2
#define NOT 3
struct Logic_Element {
int ID;
int *input_ids;
int *input_types;
int kind;
int nr_inputs;
bool output;
};
struct Sub_System {
int nr_elements;
int nr_inputs;
struct Logic_Element *elements_pointer;
};
typedef struct Logic_Element Logic_Element;
typedef struct Sub_System Sub_System;
bool test(int nr_inputs,int kind)
{
bool return_value;
switch(kind)
{
case 0:
{
return_value = true;
break;
}
}
return return_value;
}
void show_help(int code)
{
switch (code)
{
case 0:
{
printf("supported gate types:\n"
"AND (return True only if all inputs are True)\n"
"OR (returns False only if all inputs are False)\n"
"NOT(returns True if input is False, "
"and returns False if input is True)\n"
"XOR (returns True only is exactly 1 input is True)\n");
break;
}
}
}
bool check_element_type_input(char* str, Logic_Element * element_pointer)
{
if(!strcmp(str,"help"))
{
show_help(0);
return false;
}
else if(!strcmp(str,"AND"))
{
element_pointer->kind = AND;
return true;
}
else if(!strcmp(str,"OR"))
{
element_pointer->kind = OR;
return true;
}
else if(!strcmp(str,"NOT"))
{
element_pointer->kind = NOT;
return true;
}
else if(!strcmp(str,"XOR"))
{
element_pointer->kind = XOR;
return true;
}
else {
printf("Invalid input");
return false;
}
}
Sub_System* build_ssystem()
{
unsigned int nr;
Sub_System *ssystem = (Sub_System*)malloc( sizeof(Sub_System) );
char str[10];
printf("enter number of inputs needed for sub system: ");
fgets(str, sizeof(str), stdin);
scanf("%d",&(*ssystem).nr_inputs);
printf("enter number of elements needed for sub system: ");
scanf("%d",&(*ssystem).nr_elements);
nr = ssystem->nr_elements;
ssystem->elements_pointer = (Logic_Element*)malloc(sizeof(Logic_Element)*nr);
for(unsigned int i = 0; i < ssystem->nr_elements; i++)
{
bool done = false;
while(!done) {
printf("enter kind of element, type 'help' for info\n");
fgets(str, sizeof(str), stdin);
done = check_element_type_input(str, &(ssystem->elements_pointer[i]) );
}
if(ssystem->elements_pointer[i].kind != NOT)
{
printf("enter number of inputs this element has\n");
scanf("%d", &(ssystem->elements_pointer[i].nr_inputs));
int num_inputs = ssystem->elements_pointer[i].nr_inputs;
int* ids = (int*)malloc( sizeof(int)*num_inputs );
int* types = (int*)malloc( sizeof(int)*num_inputs );
ssystem->elements_pointer[i].input_ids = ids;
ssystem->elements_pointer[i].input_types = types;
printf("For each of the inputs, enter the ID of the element it is"
"connected to. The ID equal the order in which the elements "
"are added.\n to connect the gate input to a sub system "
"input, type 'o' followed by the nr of the input.\n for "
" example o1 would connect the gate input to sub system "
" input 1.\n");
for(int j = 0; j < num_inputs; j++)
{
printf("input %d: ",j);
fgets(str, sizeof(str), stdin);
ssystem->elements_pointer[i].input_ids[0] = 1;
ssystem->elements_pointer[i].input_types[0] = str[0];
}
}
}
for(int i = 0; i < ssystem->nr_elements; i++)
{
Logic_Element element = ssystem->elements_pointer[i];
printf("element %d:\t kind: %d \t nr inputs: %d\n",
i, element.kind, element.nr_inputs);
for(int j = 0; j < element.nr_inputs; j++)
{
printf("%d\t%d\t",
ssystem->elements_pointer[j].input_types[j],
ssystem->elements_pointer[j].input_types[j]);
}
printf("\n");
}
return ssystem;
}
int main()
{
printf("Welkom \n");
Sub_System *testt=build_ssystem();
printf("%d",test(0,0));
}