#include #include #include #include #define DELKA_RETEZCE 128 #define DELKA_CISLA 12 typedef char CISLO_RET[DELKA_CISLA]; void precti_retezec_a_vyprazdni_buffer(char *s, int max_delka) { int delka; printf("Zadej retezec kratsi nez %d znaku: ", max_delka); fgets(s, max_delka, stdin); delka = strlen(s) - 1; if (s[delka] == '\n') s[delka] = '\0'; else { while (getchar() != '\n') ; printf("Retezec byl oriznut.\n"); } } int zjisti_pocet_retezcu_a_jejich_max_delku(const char *s, int *max_delka, const char *delimiters) { char retezec[DELKA_RETEZCE]; char *p_c; int pocet_retezcu = 0, delka_retezce; strcpy(retezec, s); p_c = strtok(retezec, delimiters); if (p_c != NULL) { printf("%s\n", p_c); pocet_retezcu++; *max_delka = strlen(p_c); } while ((p_c = strtok(NULL, delimiters)) != NULL) { printf("%s\n", p_c); pocet_retezcu++; delka_retezce = strlen(p_c); if (delka_retezce > *max_delka) { *max_delka = delka_retezce; } } return pocet_retezcu; } int zjisti_delku_cisla(int cislo) { char retezec[DELKA_RETEZCE]; sprintf(retezec, "%d", cislo); printf("%d\n%d\n", INT_MAX, INT_MIN); return strlen(retezec); } void alokuj_pole_retezcu(int pocet_retezcu, int delka_retezce, char ***pole_retezcu, char **p_pole_retezcu) { int i; if ((*p_pole_retezcu = (char *) malloc(pocet_retezcu * delka_retezce * sizeof(char))) == NULL) { printf("Malo pameti!\n"); exit(1); } if ((*pole_retezcu = (char **) malloc(pocet_retezcu * sizeof(char *))) == NULL) { printf("Malo pameti!\n"); exit(1); } for (i = 0; i < pocet_retezcu; i++) { (*pole_retezcu)[i] = *p_pole_retezcu + i * delka_retezce; } } void alokuj_pole_retezcu2(int pocet_retezcu, char ***pole_retezcu) { if ((*pole_retezcu = (char **) malloc(pocet_retezcu * sizeof(char *))) == NULL) { printf("Malo pameti!\n"); exit(1); } } void alokuj_pole_retezcu_stat(int pocet_retezcu, CISLO_RET **pole_retezcu_stat) { if ((*pole_retezcu_stat = (CISLO_RET *) malloc(pocet_retezcu * sizeof(CISLO_RET))) == NULL) { printf("Malo pameti!\n"); exit(1); } } void vypis_pole_retezcu(char **pole_retezcu, int pocet_retezcu) { int i; for (i = 0; i < pocet_retezcu; i++) { printf("%s\n", pole_retezcu[i]); } printf("\n"); } void vypis_pole_retezcu_stat(CISLO_RET *pole_retezcu, int pocet_retezcu) { int i; for (i = 0; i < pocet_retezcu; i++) { printf("%s\n", pole_retezcu[i]); } printf("\n"); } void zapis_do_pole_retezcu(const char *s, char **pole_retezcu, const char *delimiters) { char retezec[DELKA_RETEZCE]; char *p_c; int i = 0; strcpy(retezec, s); p_c = strtok(retezec, delimiters); if (p_c != NULL) { strcpy(pole_retezcu[0], p_c); i++; } while ((p_c = strtok(NULL, delimiters)) != NULL) { strcpy(pole_retezcu[i], p_c); i++; } } void zapis_do_pole_retezcu2(const char *s, char **pole_retezcu, const char *delimiters) { char retezec[DELKA_RETEZCE]; char *p_c; int i = 0; strcpy(retezec, s); p_c = strtok(retezec, delimiters); if (p_c != NULL) { pole_retezcu[0] = (char *) malloc(strlen(p_c) + 1); strcpy(pole_retezcu[0], p_c); i++; } while ((p_c = strtok(NULL, delimiters)) != NULL) { pole_retezcu[i] = (char *) malloc(strlen(p_c) + 1); strcpy(pole_retezcu[i], p_c); i++; } } void zapis_do_pole_retezcu_stat(const char *s, CISLO_RET *pole_retezcu, const char *delimiters) { char retezec[DELKA_RETEZCE]; char *p_c; int i = 0; strcpy(retezec, s); p_c = strtok(retezec, delimiters); if (p_c != NULL) { strcpy(pole_retezcu[0], p_c); i++; } while ((p_c = strtok(NULL, delimiters)) != NULL) { strcpy(pole_retezcu[i], p_c); i++; } } void uvolni_pamet(char ***pole_retezcu, char **p_pole_retezcu) { free((void *) *p_pole_retezcu); *p_pole_retezcu = NULL; free((void *) *pole_retezcu); *pole_retezcu = NULL; } int porovnej_cislo_ret(const void *p_a, const void *p_b) { /*return (strcmp(*(char **)p_a, *(char **)p_b));*/ const char *a = *(char **)p_a; const char *b = *(char **)p_b; return (strcmp(a, b)); } int porovnej_cislo_ret_stat(const void *p_a, const void *p_b) { return (strcmp(*(CISLO_RET *)p_a, *(CISLO_RET *)p_b)); } int main(void) { const char *delimiters_for_numbers = " ;\t+-*/="; const char *delimiters_for_operators = " ;\t0123456789"; char retezec[DELKA_RETEZCE]; int pocet_cisel, pocet_operatoru, delka_cisla, delka_operatoru, delka_int; char **cisla, *p_cisla, **operatory, *p_operatory; precti_retezec_a_vyprazdni_buffer(retezec, DELKA_RETEZCE); printf("Bylo nacteno: %s\n", retezec); pocet_cisel = zjisti_pocet_retezcu_a_jejich_max_delku(retezec, &delka_cisla, delimiters_for_numbers); printf("Pocet cisel: %d\n", pocet_cisel); delka_int = zjisti_delku_cisla(INT_MIN); if (delka_cisla > delka_int) { printf("Cislo v retezci je delsi nez datovy typ int."); exit(1); } else { delka_cisla = delka_int; } printf("Delka cisla: %d\n", delka_cisla); pocet_operatoru = zjisti_pocet_retezcu_a_jejich_max_delku(retezec, &delka_operatoru, delimiters_for_operators); printf("Pocet operatoru: %d\n", pocet_operatoru); printf("Delka operatoru: %d\n", delka_operatoru); alokuj_pole_retezcu(pocet_cisel, delka_cisla + 1, &cisla, &p_cisla); alokuj_pole_retezcu(pocet_operatoru, delka_operatoru + 1, &operatory, &p_operatory); zapis_do_pole_retezcu(retezec, cisla, delimiters_for_numbers); zapis_do_pole_retezcu(retezec, operatory, delimiters_for_operators); vypis_pole_retezcu(cisla, pocet_cisel); vypis_pole_retezcu(operatory, pocet_operatoru); qsort((void *)cisla, pocet_cisel, sizeof(char *), porovnej_cislo_ret); vypis_pole_retezcu(cisla, pocet_cisel); uvolni_pamet(&cisla, &p_cisla); uvolni_pamet(&operatory, &p_operatory); printf("Pole pointeru\n"); char **cisla2; alokuj_pole_retezcu2(pocet_cisel, &cisla2); zapis_do_pole_retezcu2(retezec, cisla2, delimiters_for_numbers); qsort((void *)cisla2, pocet_cisel, sizeof(char *), porovnej_cislo_ret); vypis_pole_retezcu(cisla2, pocet_cisel); printf("Pole statickych retezcu\n"); CISLO_RET *cisla_stat; alokuj_pole_retezcu_stat(pocet_cisel, &cisla_stat); zapis_do_pole_retezcu_stat(retezec, cisla_stat, delimiters_for_numbers); qsort((void *)cisla_stat, pocet_cisel, sizeof(CISLO_RET), porovnej_cislo_ret_stat); vypis_pole_retezcu_stat(cisla_stat, pocet_cisel); return 0; }