Touring-Maschine

Image 1knu_iLhjtElum-mBY7-NLM4dMvGhbt5i

Image 1Cqgmah8k1fmzX-sZdpwnqyfKI91P_GMN

Image 1_WjEW2GmcUoE69CO72uSGq4O76Em0hbU

Image 14XHM-eA-v-YweY2p8OH45W8DBdjTgdCV

Image 1EHDij57VIp4IydDQgQT3e59tQPK6FisW

Image 1aPLmN7_PLzOkrrAWDcFdvmNUjsDDS1kA

#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,Folgezustandn");
    scanf ("%sn", s);
    if (strcmp (s, "Zustand,Eingabe,Ausgabe,Folgezustand") != 0) {
            printf ("%sn", ERR_FILE_WRON_FORMAT_MSG);
            exit (ERR_FILE_WRON_FORMAT);
    }


    for (i = 0;  i < MAX_STATES ;  i++) {
        scanf ("%i,%i,%i,%in", \&amp;i1, \&amp;x1, \&amp;y [Z_A][i], \&amp;a [i]);
        scanf ("%i,%i,%i,%in", \&amp;i2, \&amp;x2, \&amp;y [Z_B][i], \&amp;b [i]);

        if (x1 != Z_A) {
            printf ("%sn", ERR_FILE_WRON_FORMAT_MSG);
            exit (ERR_FILE_WRON_FORMAT);
        }
        if (x2 != Z_B) {
            printf ("%sn", ERR_FILE_WRON_FORMAT_MSG);
            exit (ERR_FILE_WRON_FORMAT);
        }
        if (i1 != (i+1)) {
            printf ("%sn", ERR_FILE_WRON_FORMAT_MSG);
            exit (ERR_FILE_WRON_FORMAT);
        }
        if (i2 != (i+1)) {
            printf ("%sn", 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,Folgezustandn");

    for (i = 0;  i < (MAX_STATES);  i++) {
        printf ("%i,%i,%i,%in", i+MIN_STATE, Z_A, y [Z_A][i], a [i]);
        printf ("%i,%i,%i,%in", 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 (\&amp;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]) \&amp;\&amp; (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}nn");


    printf("\begin{center}n");
    printf("\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=2.5cm]n");
    printf("%Knotenn");

    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};nn");
    printf("\node (5) [state, thick, right of= 4] {5};nn");
    printf("\node (6) [state, thick, right of= 5] {6};nn");

    printf("%Verbindungenn");
    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))\&amp;\&amp;((a [i]-MIN_STATE) != 3)], abovebelow    [((i+1)<=a[i])\&amp;\&amp;(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)) \&amp;\&amp;((b [i]-MIN_STATE) != 3)], abovebelow [((i+MIN_STATE)>b [i])\&amp;\&amp;(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");
    n");
    printf ("Zustand \&amp; Eingabe \&amp; Ausgabe \&amp; Folgezustand \&amp; \multicolumn{4}{|l|}{Codefolgezustand}n");
    n");
    printf BITTE IM PDF NACHGUCKEN
    n");
    for (i = 0;  i < MAX_STATES;  i++) {
        printf BITTE IM PDF NACHGUCKEN
        printf BITTE IM PDF NACHGUCKEN

    }
    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-newn-nnn");
        printf ("--read-csvn-rnn");
        printf ("--print-csvn-cnn");
        printf ("--print-texn-tnn");
        printf ("--helpn-hnn");
        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;
}