Untitled

mail@pastecode.io avatar
unknown
plain_text
7 days ago
2.6 kB
8
Indexable
Never
#include <stdio.h>
#include <string.h>

#define NUM_STUDENTS 5

struct Student {
    char vards[50];
    char uzvards[50];
    int vecums;
    char grupa[10];
};

int salidzinat_vardus(const char *vards1, const char *vards2) {
    return strcmp(vards1, vards2);
}

int salidzinat_grupas(const char *grupa1, const char *grupa2) {
    return strcmp(grupa1, grupa2);
}

void kartotPecVecuma(struct Student studenti[], int n) {
    struct Student temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (studenti[j].vecums > studenti[j + 1].vecums) {
                temp = studenti[j];
                studenti[j] = studenti[j + 1];
                studenti[j + 1] = temp;
            }
        }
    }
}

void kartotPecVarda(struct Student studenti[], int n) {
    struct Student temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (salidzinat_vardus(studenti[j].vards, studenti[j + 1].vards) > 0) {
                temp = studenti[j];
                studenti[j] = studenti[j + 1];
                studenti[j + 1] = temp;
            }
        }
    }
}

void kartotPecGrupas(struct Student studenti[], int n) {
    struct Student temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (salidzinat_grupas(studenti[j].grupa, studenti[j + 1].grupa) > 0) {
                temp = studenti[j];
                studenti[j] = studenti[j + 1];
                studenti[j + 1] = temp;
            }
        }
    }
}

void paraditStudentus(struct Student studenti[], int n) {
    for (int i = 0; i < n; i++) {
        printf("Vārds: %s, Uzvārds: %s, Vecums: %d, Grupa: %s\n", 
               studenti[i].vards, studenti[i].uzvards, studenti[i].vecums, studenti[i].grupa);
    }
}

int main() {
    struct Student studenti[NUM_STUDENTS] = {
        {"Ainars", "Banchella", 22, "px22"},
        {"Arnis", "Kaposts", 19, "px23"},
        {"Aldis", "Kilograms", 21, "px24"},
        {"Andris", "Svaigais", 20, "px22"},
        {"Alfreds", "Stivais", 18, "px23"}
    };

    int izvele;

    printf("Izvēlieties kārtošanu:\n1 - Kārtot pēc vārda\n2 - Kārtot pēc vecuma\n3 - Kārtot pēc grupas\n");
    scanf("%d", &izvele);

    if (izvele == 1) {
        kartotPecVarda(studenti, NUM_STUDENTS);
    } else if (izvele == 2) {
        kartotPecVecuma(studenti, NUM_STUDENTS);
    } else if (izvele == 3) {
        kartotPecGrupas(studenti, NUM_STUDENTS);
    } else {
        printf("Nederīga izvēle!\n");
        return 1;
    }

    paraditStudentus(studenti, NUM_STUDENTS);

    return 0;
}
Leave a Comment