Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.7 kB
1
Indexable
Never
#include<stdlib.h>
#include <stdio.h>
#include <string.h>

int este_palindrom(int x)
{
    int inv=0,c=x;

    while(c)
    {
        inv=inv*10+c%10;
        c/=10;
    }

    if(inv==x)
        return 1;
    else
        return 0;
}

int este_prim(int x)
{
    int nr=0;

    for(int i=1; i<=x; i++)
    {
        if(x%i==0)
            nr++;
    }

    if(nr==2)
        return 1;
    else
        return 0;
}
int main()
{
    int n,m;
    int **matrice;
    char *text;
    char *cuvant;

    scanf("%d",&n);
    scanf("%d",&m);

    char buffer;
    scanf("%c",&buffer);

    matrice=(int **)calloc(n,sizeof (int *));

    for(int i=0; i<n; i++)
    {
        matrice[i]=(int *)calloc(m,sizeof (int *));
    }

    int contor_citire = 0, dim_citire = 1;
    text=calloc(1,sizeof (char *));

    while(1)
    {
        if (dim_citire == contor_citire)
        {
            char *a_test;
            a_test = realloc(text, dim_citire * 2);

            if(a_test==NULL)
            {
                printf("eroare la realocarea dim");
            }

            text = a_test;

            dim_citire = dim_citire * 2;

        }

         if (scanf("%c", &text[contor_citire]) == EOF)
                break;

            contor_citire++;


            printf("%s\n",text); /// trb testat

    }


    matrice[0][0]=1;

    int parcurgere_matrice=2,coord_i=0,coord_j=0;

    cuvant = strtok(text, " ");
    while (cuvant != NULL)
    {

        if(cuvant[0]=='a')
        {
            int x1=0,x2=0,x3=0,x4=0;

            x1=cuvant[1]-'0';
            x2=cuvant[2]-'0';
            x3=cuvant[3]-'0';
            x4=cuvant[4]-'0';

            int max12,max34,maxfinal;

            if(x1>x2)
            {
                max12=x1;
            }
            else
            {
                max12=x2;
            }
            if(x3>x4)
            {
                max34=x3;
            }
            else
            {
                max34=x4;
            }

            if(max12>max34)
            {
                maxfinal=max12;
            }
            else
            {
                maxfinal=max34;
            }

            if(maxfinal==x1)
            {
                coord_j++;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }
            if(maxfinal==x2)
            {
                coord_i--;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }
            if(maxfinal==x3)
            {
                coord_j--;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }
            if(maxfinal==x4)
            {
                coord_i++;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }
        }

        if(cuvant[0]=='b')
        {
            int k=0,x=0;

            for(int i=1; i<strlen(cuvant) && cuvant[i]!='\n'; i++)
            {
                k=k*10+(cuvant[i]-'0');

                printf("k este:%d\n",k);
            }
            printf("final:%d\n",k);

            x=k%100;

            printf("%d\n",x);

            if(este_palindrom(k)==1 && este_prim(x)==1)
            {
                coord_j--;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }

            if(este_palindrom(k)==1 && este_prim(x)==0)
            {
                coord_j++;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }

            if(este_palindrom(k)==0 && este_prim(x)==1)
            {
                coord_i--;  /// in sus
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }

            if(este_palindrom(k)==0 && este_prim(x)==0)
            {
                coord_i++;  /// in jos
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }

        }

        if(cuvant[0]=='c')
        {
            int v[1000]= {0};
            int dim=cuvant[1]-'0';
            int circulare=cuvant[2]-'0'; //k
            int contor=0;
            int s=0;

            for(int i=3; i<strlen(cuvant); i++)
            {
                v[contor]=cuvant[i]-'0';
                contor++;
            }

            contor--; // a rezolvat problema

            int local_circulare=0;

            for(int i=1; i<=circulare; i++)
            {
                printf("La inceput local_circulare : %d\n",local_circulare);
                if(local_circulare<dim)
                {
                    s+=v[local_circulare];
                    printf("Am adaugat la suma if: %d\n",v[local_circulare]);
                }
                else
                {
                    local_circulare=local_circulare%dim;
                    s+=v[local_circulare];
                    printf("Am adaugat la suma else: %d , local_circulare:%d\n",v[local_circulare],local_circulare);
                }
                local_circulare+=circulare;
                printf("local_circulare : %d\n",local_circulare);


                printf("suma este: %d\n",s);
            }
            printf("suma este %d\n",s);

            if(s%4==0)  //stanga
            {
                coord_j--;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }
            if(s%4==1)  //sus
            {
                coord_i--;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }

            if(s%4==2)  //dreapta
            {
                coord_j++;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }

            if(s%4==3)  //jos
            {
                coord_i++;
                matrice[coord_i][coord_j]=parcurgere_matrice;
                parcurgere_matrice++;
            }

            for(int i=0; i<=contor; i++)
            {
                printf("%d ",v[i]);
            }
            printf("\n");
        }
        cuvant = strtok(NULL, " ");
    }

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            printf("%d ",matrice[i][j]);
        }
        printf("\n");
    }

}