#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ERR_FILE_WRON_FORMAT 3
#define ERR_FILE_WRON_FORMAT_MSG "IO-Error: File Wrong Format"
#define MAX_STR_INPUT_LEN 128
#define MAX_SPECIAL_STATES 3
#define MAX_STATES 6
#define MIN_STATE 1
#define SPECIAL_STATE_1 1
#define SPECIAL_STATE_2 2
#define SPECIAL_STATE_3 3
#define MAX_INPUTS 2
#define MAX_OUTPUTS 2
#define TRUE 1
#define FALSE 0
#define Z_A 0
#define Z_B 1
void readcsv (int a [], int b [], int y [MAX_OUTPUTS][MAX_STATES]) {
int i;
int j;
int i1, i2;
int x1, x2;
char s [MAX_STR_INPUT_LEN];
//scanf ("Zustand,Eingabe,Ausgabe,Folgezustand\n");
scanf ("%s\n", s);
if (strcmp (s, "Zustand,Eingabe,Ausgabe,Folgezustand") != 0) {
printf ("%s\n", ERR_FILE_WRON_FORMAT_MSG);
exit (ERR_FILE_WRON_FORMAT);
}
for (i = 0; i < MAX_STATES ; i++) {
scanf ("%i,%i,%i,%i\n", &i1, &x1, &y [Z_A][i], &a [i]);
scanf ("%i,%i,%i,%i\n", &i2, &x2, &y [Z_B][i], &b [i]);
if (x1 != Z_A) {
printf ("%s\n", ERR_FILE_WRON_FORMAT_MSG);
exit (ERR_FILE_WRON_FORMAT);
}
if (x2 != Z_B) {
printf ("%s\n", ERR_FILE_WRON_FORMAT_MSG);
exit (ERR_FILE_WRON_FORMAT);
}
if (i1 != (i+1)) {
printf ("%s\n", ERR_FILE_WRON_FORMAT_MSG);
exit (ERR_FILE_WRON_FORMAT);
}
if (i2 != (i+1)) {
printf ("%s\n", ERR_FILE_WRON_FORMAT_MSG);
exit (ERR_FILE_WRON_FORMAT);
}
}
return;
}
void printcsv (int a [], int b [], int y [MAX_OUTPUTS][MAX_STATES]) {
int i;
printf ("Zustand,Eingabe,Ausgabe,Folgezustand\n");
for (i = 0; i < (MAX_STATES); i++) {
printf ("%i,%i,%i,%i\n", i+MIN_STATE, Z_A, y [Z_A][i], a [i]);
printf ("%i,%i,%i,%i\n", i+MIN_STATE, Z_B, y [Z_B][i], b [i]);
}
return;
}
void createnew (int a [], int b [], int y [MAX_OUTPUTS][MAX_STATES]) {
#define RAND_1 32
#define RAND_2 16
#define RAND_3 16
#define RAND_4 8
#define N_SORTS 8
time_t t;
int i, j;
int flag;
int s;
srand ((unsigned)time (&t));
for (i = 0; i < MAX_STATES; i++) {
a [i] = i + MIN_STATE;
b [i] = i + MIN_STATE;
}
flag = 0;
for (s = 0; s < N_SORTS; s++)
while (!flag) {
for (i = 0; i < MAX_STATES; i++) {
for (j = i + 1; j < MAX_STATES; j++) {
if ((rand () % RAND_1) > RAND_2) {
t = a [j];
a [j] = a [i];
a [i] = t;
}
}
}
for (i = 0; i < MAX_STATES; i++) {
for (j = i + 1; j < MAX_STATES; j++) {
if ((rand () % RAND_3) < RAND_4) {
t = b [j];
b [j] = b [i];
b [i] = t;
}
}
}
flag = TRUE;
for (i = 0; i < MAX_STATES; i++)
if ((a [i] == b [i]) && (b [i] == (i+MIN_STATE))) {
flag = FALSE;
}
}
for (i = 0; i < MAX_STATES; i++) {
y [Z_A][i] = rand () % MAX_OUTPUTS;
y [Z_B][i] = rand () % MAX_OUTPUTS;
}
return;
}
void printtex (int a [], int b [], int y [MAX_OUTPUTS][MAX_STATES]) {
int i, j;
int codez = 8;
printf("\\documentclass{article}\n");
printf("\\usepackage[utf8]{inputenc}\n");
printf("\\usepackage{pgf, tikz}\n");
printf("\\usetikzlibrary{arrows , automata , positioning}\n");
printf("\\begin{document}\n\n");
printf("\\begin{center}\n");
printf("\\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=2.5cm]\n");
printf("\%Knoten\n");
printf("\\node (1) [state, thick] {1};\n");
printf("\\node (2) [state, thick, right of= 1] {2};\n");
printf("\\node (3) [state, thick, right of= 2] {3};\n");
printf("\\node (4) [state, thick, right of= 3] {4};\n\n");
printf("\\node (5) [state, thick, right of= 4] {5};\n\n");
printf("\\node (6) [state, thick, right of= 5] {6};\n\n");
printf("\%Verbindungen\n");
printf("\\path[thick,->]\n");
char *leftright [] = {"left", "right"};
char *abovebelow [] = {"above", "below"};
char rorl[2] = {'R', 'L'};
for (i = 0; i < MAX_STATES; i++) {
if ((a [i] - MIN_STATE) == i)
printf ("(%i) edge [loop] node {%i/%i,%c} (%i)\n", i + MIN_STATE, Z_A, y [Z_A][i], rorl [rand () % 2], a [i]);
else
printf ("(%i) edge [bend angle=%i, bend %s,%s] node {%i/%i,%c} (%i)\n", i + MIN_STATE, (i+1)*10+(a[i]-MIN_STATE)*5, leftright [(i>(a [i]-MIN_STATE))&&((a [i]-MIN_STATE) != 3)], abovebelow [((i+1)<=a[i])&&(a[i] != 4)], Z_A, y [Z_A][i], rorl [rand () % 2], a [i]);
if ((b [i] - MIN_STATE) == i)
printf ("(%i) edge [loop] node {%i/%i,%c} (%i)\n", i + MIN_STATE, Z_B, y [Z_B][i], rorl [rand () % 2], b [i]);
else
printf ("(%i) edge [bend angle=%i, bend %s,%s] node {%i/%i,%c} (%i)\n", i + MIN_STATE, (i+1)*25+(b[i]-MIN_STATE)*8, leftright [(i<=(b [i]-MIN_STATE)) &&((b [i]-MIN_STATE) != 3)], abovebelow [((i+MIN_STATE)>b [i])&&(b [i] != 4)], Z_B, y [Z_B][i], rorl [rand () % 2], b [i]);
}
printf(";\n");
printf("\\end{tikzpicture}\n");
printf("\\end{center}\n");
printf ("\\begin{center}\n");
printf ("\\begin{verbatim}\n");
for (i = 0; i < 32; i++) {
printf ("%i ", rand () % 2);
}
printf("\\end{verbatim}\n");
printf("\\end{center}\n");
/*printf ("\\begin{center}\n");
printf ("\\begin{tabular}{|l|l|l|l||l|l|l|l|}\n");
printf ("\\hline\n");
printf ("Zustand & Eingabe & Ausgabe & Folgezustand & \\multicolumn{4}{|l|}{Codefolgezustand}\\\\\n");
printf ("\\hline\n");
printf ("& & & & \\verb\"z3+\" & \\verb\"z2+\" & \\verb\"z1+\" & \\verb\"z0+\"\\\\\n");
printf ("\\hline\n");
for (i = 0; i < MAX_STATES; i++) {
printf ("\\verb\"z%i\" & \\verb\"%i\" & \\verb\"%i\" & \\verb\"z%i\" & \\verb\"%i\" & \\verb\"%i\" & \\verb\"%i\" & \\verb\"%i\"\\\\\n", i + MIN_STATE, Z_A, y [Z_A][i], a [i], ((0x01 << (a [i]-MIN_STATE)) & 0x08) >> (a [i]-MIN_STATE), ((0x01 << (a [i]-MIN_STATE)) & 0x04) >> (a [i]-MIN_STATE), ((0x01 << (a [i]-MIN_STATE)) & 0x02) >> (a [i]-MIN_STATE), ((0x01 << (a [i]-MIN_STATE)) & 0x01) >> (a [i]-MIN_STATE));
printf ("\\verb\"z%i\" & \\verb\"%i\" & \\verb\"%i\" & \\verb\"z%i\" & \\verb\"%i\" & \\verb\"%i\" & \\verb\"%i\" & \\verb\"%i\"\\\\\n", i + MIN_STATE, Z_B, y [Z_B][i], b [i], (((0x01 << (b [i]-MIN_STATE))) & 0x08) >> (b [i]-MIN_STATE), ((0x01 << (b [i]-MIN_STATE)) & 0x04) >> (b [i]-MIN_STATE), ((0x01 << (b [i]-MIN_STATE)) & 0x02) >> (b [i]-MIN_STATE), ((0x01 << (b [i]-MIN_STATE)) & 0x01) >> (b [i]-MIN_STATE));
}
printf ("\\hline\n");
printf ("\\end{tabular}\n");
printf ("\\end{center}\n");
printf ("\\begin{center}\n");
printf ("\\begin{verbatim}\n");
for (i = MIN_STATE; i < (MAX_STATES + MIN_STATE); i++) {
printf ("z%i+ := ", i);
for (j = 0; j < MAX_STATES; j++) {
if ( a [j] == i )
printf ("z%i and x or ", j+MIN_STATE, y [j]);
if ( b [j] == i )
printf ("z%i and not x or ", j+MIN_STATE, y [j]);
}
printf (" true;\n");
}
printf ("\\end{verbatim}\n");
printf ("\\end{center}\n");*/
printf("\\end{document}\n");
}
int main (int argc, char *argv []) {
#define CREATE_NEW 0
#define READ_CSV 1
#define PRINT_TEX 0
#define PRINT_CSV 1
#define PRINT_NO_HELP 0
#define PRINT_HELP 1
int b [MAX_STATES];
int a [MAX_STATES];
int y [MAX_OUTPUTS][MAX_STATES];
int argi;
int createnew_readcsv = CREATE_NEW;
int printraw_printtex_print_csv = PRINT_CSV;
int printhelp = PRINT_NO_HELP;
for (argi = 1; argi < argc; argi++) {
if ((strcmp ("--create-new", argv [argi]) == 0) || (strcmp ("-n", argv [argi]) == 0))
createnew_readcsv = CREATE_NEW;
if ((strcmp ("--read-csv", argv [argi]) == 0) || (strcmp ("-s", argv [argi]) == 0))
createnew_readcsv = READ_CSV;
if ((strcmp ("--print-csv", argv [argi]) == 0) || (strcmp ("-c", argv [argi]) == 0))
printraw_printtex_print_csv = PRINT_CSV;
if ((strcmp ("--print-tex", argv [argi]) == 0) || (strcmp ("-t", argv [argi]) == 0))
printraw_printtex_print_csv = PRINT_TEX;
if ((strcmp ("--help", argv [argi]) == 0) || (strcmp ("-h", argv [argi]) == 0))
printhelp = PRINT_HELP;
}
if (printhelp == PRINT_HELP) {
printf ("--create-new\n-n\n\n");
printf ("--read-csv\n-r\n\n");
printf ("--print-csv\n-c\n\n");
printf ("--print-tex\n-t\n\n");
printf ("--help\n-h\n\n");
return 2;
}
if (createnew_readcsv == CREATE_NEW)
createnew (a, b, y);
else if (createnew_readcsv == READ_CSV)
readcsv (a, b, y);
if (printraw_printtex_print_csv == PRINT_TEX)
printtex (a, b, y);
else if (printraw_printtex_print_csv == PRINT_CSV)
printcsv (a, b, y);
return 0;
}