Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.2 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define varfuri 9
// #define arce 13

int rows, columns;

int **initializareMatrice()
{
    int **matrice = (int **)malloc(rows * sizeof(int *));

    for (int i = 0; i < rows; i++)
    {
        matrice[i] = (int *)malloc(columns * sizeof(int));
    }

    return matrice;
}

int **readMatriceAdiacenta(int **matrice)
{

    for (int i = 0; i < rows; i++)
    {
        printf("X%d ", i + 1);
        for (int j = 0; j < columns; j++)
        {
            scanf("%d", &matrice[i][j]);
        }
    }

    return matrice;
}

int binVal(int i, int j)
{
    if (i == 1 && j == 1)
        return 1;
    else if (i == 1 && j == 0)
        return 1;
    else if (i == 0 && j == 1)
        return 1;
    else
        return 0;
}

int main()
{

    int **matriceL; // L mean that it could be also a list de adiacenta
    int **matriceDrumurilor;
    int option = 1;

    while (option != 0)
    {
        /* code */

        printf("1. Matrice de adiacenta\n");
        printf("2. Corectia datelor\n");
        printf("3. Gasirea drumurilor\n");
        printf("4. Afisarea matricei simple\n");
        printf("5. Afisarea matricea drumurilor\n");
        printf("98. automation matrice de adiacenta\n");
        printf("0. Exit\n");
        printf("Alegeti optiunea: ");
        scanf("%d", &option);

        switch (option)
        {
        case 1:
        {
            printf("Citire Matrice de adiacenta\n");
            rows = varfuri;
            columns = varfuri;
            matriceL = initializareMatrice();
            readMatriceAdiacenta(matriceL);
        }

        break;
        case 2:
        {
            printf("Corectia datelor\n");

            int m, n;
            printf("m = ");
            scanf("%d", &m);
            printf("\n");

            printf("n = ");
            scanf("%d", &n);
            printf("\n");

            printf("Valoarea noua");
            scanf("%d", &matriceL[m][n]);
            printf("\n");

            break;
        }
        break;
        case 3:
        {
            printf("Gasirea drumurilor\n");
            // initializare matrice drumurilor
            matriceDrumurilor = initializareMatrice();
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    matriceDrumurilor[i][j] = matriceL[i][j];
                }
            }

            // apply Floyd-Warshall algorithm
            for (int k = 0; k < rows; k++)
            {
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < rows; j++)
                    {

                        if (i != j){
                        matriceDrumurilor[i][j] = binVal(matriceDrumurilor[i][j],
                         matriceDrumurilor[i][k] && matriceDrumurilor[k][j]);
                        }
                    }
                }
            }
        }

        break;

        case 4:
        {
            printf("Afisarea matricei simple\n");

            for (int i = 0; i < rows; i++)
            {

                for (int j = 0; j < columns; j++)
                {
                    printf("| ");
                    printf("%d", matriceL[i][j]);
                }
                printf("\n");
            }
            break;
        }

        case 5:
        {
            printf("Afisarea matricea drumurilor\n");
            printf("     \n");

            for (int i = 0; i < rows; i++)
            {

                for (int j = 0; j < columns; j++)
                {
                    if (matriceDrumurilor[i][j] == INT_MAX)
                    {
                        printf("INF ");
                    }
                    else
                    {
                        printf("%d   ", matriceDrumurilor[i][j]);
                    }
                }
                printf("\n");
            }
        }
        break;

        case 98:
        {

            printf("automation matrice de adiacenta\n");
            rows = varfuri;
            columns = varfuri;
            matriceL = initializareMatrice();
            int matrice[varfuri][varfuri] = {
                {0, 1, 0, 0, 0, 0, 0, 1, 1},
                {0, 0, 1, 0, 0, 0, 0, 0, 0},
                {1, 1, 0, 1, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 1, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 1, 1, 0, 0},
                {0, 0, 1, 0, 0, 0, 1, 0, 0},
                {0, 0, 0, 0, 0, 1, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0}};

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    matriceL[i][j] = matrice[i][j];
                }
            }
        }
        break;

        case 0:
            printf("Exit\n");
            break;
        }
    }
    return 0;
}