MyTestInC

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
4.1 kB
0
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define M 7
#define N1 5
#define N2 4

typedef struct {
    int mark1, mark2;
}marks;

typedef struct {
    char name[20];
    int id;
    marks m;
}talmid;

void print_talmid(talmid t[], int n) {
    for (int i = 0; i < n; i++) {
        //I wrote : printf("Name: %s, ID: %d, avg: %lf, : \n", t[i].name, t[i].id, ((t[i].m.mark1+t[i].m.mark2)/2));
        printf("Name: %s, ID: %d, avg: %lf, : \n", t[i].name, t[i].id, ((t[i].m.mark1+t[i].m.mark2)/2.0));
    }
}

void print_talmid_ptr(talmid* t[], int n) {
    for (int i = 0; i < n; i++) {
        //I wrote : printf("Name: %s, ID: %d, avg: %lf, : \n", t[i]->name, t[i]->id, ((t[i]->m.mark1 + t[i]->m.mark2)/2));
        printf("Name: %s, ID: %d, avg: %lf, : \n", t[i]->name, t[i]->id, ((t[i]->m.mark1 + t[i]->m.mark2)/2.0));
    }
}

talmid** mergeTalmid(talmid a[], int n1, talmid b[], int n2)
{
    talmid** t;
    int i = 0, j = 0, k = 0;
    t =(talmid**) malloc((n1 + n2) * sizeof(talmid*));
    assert(t != NULL);
    while (i < n1 && j < n2) 
    {
        if (strcmp(a[i].name, b[j].name) < 0) {
            t[k++] = a + (i++);
        } 
        else if (strcmp(a[i].name, b[j].name) > 0)
        {
            t[k++] = b + j;
            j++;
        } 
        else
        {
            t[k++] = a + (i++);
            t[k++] = b + j;
            j++;
        }
    }

    while (i < n1) {
        t[k++] = a + (i++);
    }

    while (j < n2) {
        t[k++] = b + j;
        j++;
    }

    return t;
}


char* Q2(char** A){
    int i = 0;
    char *p;  
    p = (char *)malloc(sizeof(char)*( strlen(*A) +1 )); 
    assert(p != NULL);
    strcpy(p,A[i++]); 
    while(i < M) 
    { 
        if(strstr(*A, *(A+i))) 
        {
            p = (char*)realloc(p, sizeof(char)*(strlen(p) + strlen(*(A+i))+1)); 
            assert(p != NULL);
            strcat(p, *(A+i)); 
        }
        i++; 
    }
    return p;
}


char* Q2Correct(char** A) {
    int i = 0;
    char *p;  
    p = (char *)malloc(sizeof(char)*(strlen(*(A+M-1)) + 1));
    assert(p != NULL);
    strcpy(p, *(A+M-1-(i++))); 
    while (i < M) { 
        if (strstr(*(A+M-1), *(A+M-1-i))) { 
            p = (char*)realloc(p, sizeof(char)*(strlen(p) + strlen(*(A+M-1-i)) + 1));
            assert(p != NULL);
            strcat(p, *(A+M-1-i)); 
        }
        i++;
    }
    return p;
}

typedef struct s1 
{
    int code;
    char* color;
    struct s1* p;
}s1;

int main() {
    int aa[10] = {10,20,30,40,50,60,90,100};
    char* clr[5]={"white", "pink", "black", "red", "blue"};
    s1 arr1[4] = {
        {*(aa+6)+1, clr[3], arr1 + 3},
        {aa[9], *clr+1, arr1},
        {*aa+5, *(clr + 4), arr1 + 1},
        {*aa * 5, *(clr + 3) + 1, arr1 +2}
    };

    s1* pa[4] = {arr1+1, arr1+2, arr1+3, arr1};
    s1** pp = pa + 1;

    printf("1. %s\n", pa[1]->p->color);
    printf("2. %s\n", ((*(pp+2))->p->p->color));
    printf("3. %d\n", --(pa[2]->code));
    pp++;
    printf("4. %c\n", (*(**(pa)).color));
    printf("5. %s\n", (*(pa+3))->p->color);
    printf("6. %d\n", (pp[-2]->p->code));
    printf("7. %s\n", pp[-1]->p->color);
    printf("8. %c\n\n", (*(*pp)->p->color));



    char* s[M] = {"12abc", "1523", "aaa", "4124", "112" , "aaa2" , "12"};
    char* ptr1 = Q2(s);
    if (ptr1) {
        puts(ptr1);
        free(ptr1);
    }
      
    printf("\n\n");

    talmid a[N1] = {
    {"Alice", 2, {75, 80}},
    {"Bob", 3, {88, 90}},
    {"Charlie", 5, {70, 75}},
    {"Eve", 4, {95, 100}},
    {"John", 1, {90, 85}}
    };

    talmid b[N2] = {
    {"David", 6, {85, 90}},
    {"Frank", 8, {90, 95}},
    {"Grace", 7, {80, 85}},
    {"Helen", 9, {78, 82}}
    };


    printf("Array a:\n");
    print_talmid(a, N1);
    printf("\nArray b:\n");
    print_talmid(b, N2);


    talmid** merged = mergeTalmid(a, N1, b, N2);


    printf("\nMerged Array:\n");
    print_talmid_ptr(merged, N1 + N2);


    free(merged);
    return 0;
}

Leave a Comment