This commit is contained in:
Anton 2024-12-14 14:51:40 +01:00
commit b3f3cd0526
27 changed files with 1088 additions and 79 deletions

BIN
a1/main Executable file

Binary file not shown.

92
a1/program.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <string.h>
#define MAX_LEN 101
typedef struct {
char bracket;
int position;
} StackItem;
int is_opening(char ch) {
return ch == '(' || ch == '{' || ch == '[' || ch == '<';
}
int is_closing(char ch) {
return ch == ')' || ch == '}' || ch == ']' || ch == '>';
}
char matching_bracket(char ch) {
switch (ch) {
case ')': return '(';
case '}': return '{';
case ']': return '[';
case '>': return '<';
default: return 0;
}
}
char expected_closing(char ch) {
switch (ch) {
case '(': return ')';
case '{': return '}';
case '[': return ']';
case '<': return '>';
default: return 0;
}
}
void check_brackets(const char *line) {
StackItem stack[MAX_LEN];
int stack_top = -1;
printf("Read: %s\n", line);
for (int i = 0; line[i] != '\0'; i++) {
char ch = line[i];
if (is_opening(ch)) {
if (stack_top < MAX_LEN - 1) {
stack[++stack_top].bracket = ch;
stack[stack_top].position = i;
} else {
printf("Error: Stack overflow\n");
return;
}
} else if (is_closing(ch)) {
if (stack_top == -1) {
printf("Unexpected closing bracket %c in %d\n", ch, i);
return;
} else if (stack[stack_top].bracket == matching_bracket(ch)) {
stack_top--;
} else {
printf("Crossed bracket %c in %d, expected %c \n", ch, i,
expected_closing(stack[stack_top].bracket));
return;
}
}
}
if (stack_top >= 0) {
printf("Missing closing brackets:");
printf(" ");
while (stack_top >= 0) {
printf("%c", expected_closing(stack[stack_top].bracket));
stack_top--;
}
printf("\n");
} else {
printf("All brackets OK\n");
}
}
int main() {
char line[MAX_LEN];
if (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\n")] = '\0';
check_brackets(line);
}
return 0;
}

34
a4/program.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdlib.h>
#include <stdio.h>
int is_heap(int* arr, int size ){
for(int i = 0; i < size; i++){
int l = i * 2 + 1;
int r = i * 2 + 2;
}
if (l < size && arr[i] < arr[l]){
return 0;
}
if (r < size && arr[i] < arr[r]){
return 0;
}
return 1;
}
int main() {
int arr[] = 100;
int size = 0;
while (scanf("%d", &num) != EOF){
arr[size++] = num;
}
if (!is_heap(arr, size)){
printf("Nie je kopa.");
}
return 0;
}

92
cv1/program.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define LINESIZE 100
#define MENU_SIZE 100
struct pizza {
char name[LINESIZE];
float price;
};
char hacker_script(char l) {
switch (l) {
case '0': return 'o';
case '1': return 'i';
case '2': return 'z';
case '3': return 'e';
case '4': return 'a';
case '5': return 's';
case '6': return 'b';
case '7': return 't';
case '8': return 'b';
case '9': return 'q';
default: return l;
}
}
void transform_to_hacker_script(const char *src, char *dest) {
while (*src) {
*dest++ = hacker_script(*src++);
}
*dest = '\0';
}
int contains_normalized(const char *name, const char *search) {
char transformed_name[LINESIZE], transformed_search[LINESIZE];
transform_to_hacker_script(name, transformed_name);
transform_to_hacker_script(search, transformed_search);
for (int i = 0; transformed_name[i]; i++) {
transformed_name[i] = tolower(transformed_name[i]);
}
for (int i = 0; transformed_search[i]; i++) {
transformed_search[i] = tolower(transformed_search[i]);
}
return strstr(transformed_name, transformed_search) != NULL;
}
int read_pizza(struct pizza *item) {
char line[LINESIZE];
if (!fgets(item->name, LINESIZE, stdin)) {
return 0;
}
item->name[strcspn(item->name, "\n")] = '\0';
if (!fgets(line, LINESIZE, stdin)) {
return 0;
}
item->price = strtof(line, NULL);
return 1;
}
int main() {
struct pizza menu[MENU_SIZE];
char search[LINESIZE];
int count = 0;
printf("Zadaj hladanu surovinu:\n");
if (!fgets(search, LINESIZE, stdin)) {
return 1;
}
search[strcspn(search, "\n")] = '\0';
printf("Zadaj jedalny listok:\n");
while (count < MENU_SIZE && read_pizza(&menu[count])) {
count++;
}
int found_count = 0;
for (int i = 0; i < count; i++) {
if (contains_normalized(menu[i].name, search)) {
printf("%s\n%.2f\n", menu[i].name, menu[i].price);
found_count++;
}
}
printf("Nacitanych %d poloziek.\n", count);
return 0;
}

61
cv2/program.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 100
#define MAX_ITEMS 100
typedef struct {
char name[MAX_NAME_LENGTH];
float price;
} Pizza;
int compare(const void *a, const void *b) {
Pizza *pizzaA = (Pizza *)a;
Pizza *pizzaB = (Pizza *)b;
if (pizzaA->price != pizzaB->price) {
return (pizzaA->price > pizzaB->price) - (pizzaA->price < pizzaB->price);
}
return strcasecmp(pizzaA->name, pizzaB->name);
}
int read_pizza_list(Pizza *menu) {
int count = 0;
char input[MAX_NAME_LENGTH];
// Nacitame pizza
while (count < MAX_ITEMS && fgets(input, sizeof(input), stdin)) {
input[strcspn(input, "\n")] = 0; // Odstran znak noveho riadku
// Nacitame cenu pizze
char price_str[10];
if (fgets(price_str, sizeof(price_str), stdin) == NULL) {
break;
}
float price = atof(price_str);
if (price <= 0) {
break;
}
strncpy(menu[count].name, input, MAX_NAME_LENGTH - 1);
menu[count].price = price;
count++;
}
return count;
}
int main() {
Pizza menu[MAX_ITEMS];
int item_count = read_pizza_list(menu);
qsort(menu, item_count, sizeof(Pizza), compare);
for (int i = 0; i < item_count; i++) {
printf("%s\n%.6f\n", menu[i].name, menu[i].price);
}
return 0;
}

BIN
cv3/program Executable file

Binary file not shown.

105
cv3/program.c Normal file
View File

@ -0,0 +1,105 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define STACK_SIZE 10
struct stack {
float values[STACK_SIZE];
int size;
};
void print_stack(struct stack* s) {
for (int i = 0; i < s->size; i++) {
printf("%.2f", s->values[i]);
if (i < s->size - 1) {
printf(" ");
} else {
printf(" ");
}
}
printf("\n");
}
void push_stack(struct stack* s, float value) {
if (s->size >= STACK_SIZE) {
printf("full stack\n");
exit(0);
}
s->values[s->size++] = value;
}
float pop_stack(struct stack* s) {
if (s->size == 0) {
printf("empty stack\n");
exit(0);
}
return s->values[--s->size];
}
int is_operator(char* input) {
return (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 ||
strcmp(input, "*") == 0 || strcmp(input, "/") == 0);
}
int is_valid_float(char* input) {
char* endptr;
strtod(input, &endptr);
return *endptr == '\0';
}
void perform_operation(struct stack* s, char* operator) {
if (s->size < 2) {
printf("not enough operands\n");
exit(0);
}
float b = pop_stack(s);
float a = pop_stack(s);
float result;
if (strcmp(operator, "+") == 0) {
result = a + b;
} else if (strcmp(operator, "-") == 0) {
result = a - b;
} else if (strcmp(operator, "*") == 0) {
result = a * b;
} else if (strcmp(operator, "/") == 0) {
if (b == 0) {
printf("division by zero\n");
exit(0);
}
result = a / b;
} else {
printf("Chyba: neplatna operacia\n");
exit(0);
}
push_stack(s, result);
}
int main() {
char input[100];
struct stack mystack;
mystack.size = 0;
while (fgets(input, sizeof(input), stdin) != NULL) {
input[strlen(input) - 1] = '\0';
if (is_valid_float(input)) {
float value = atof(input);
push_stack(&mystack, value);
} else if (is_operator(input)) {
perform_operation(&mystack, input);
} else {
printf("bad input\n");
return 0;
}
print_stack(&mystack);
}
printf("no input\n");
return 0;
}

29
cv4/Makefile Normal file
View File

@ -0,0 +1,29 @@
# Compiler and flags
CC = gcc
CFLAGS = -std=c99 -g -Wall
# Source files and object files
SRCS = main.c a_train.c
OBJS = $(SRCS:.c=.o)
# Target executable
TARGET = main
# Default rule to build the executable
all: $(TARGET)
# Rule to link object files and create the executable
$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $(TARGET)
# Rule to compile .c files into .o files
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
# Clean up generated files
clean:
rm -f $(OBJS) $(TARGET)
# Phony targets (targets that are not actual files)
.PHONY: all clean

70
cv4/a_train.c Normal file
View File

@ -0,0 +1,70 @@
#include "a_train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct car* add_car(struct car* first, const char* target) {
struct car* newcar = calloc(1, sizeof(struct car));
strcpy(newcar->value, target);
newcar->next = NULL;
if (first == NULL) {
first = newcar;
} else {
struct car* current = first;
while (current->next != NULL) {
current = current->next;
}
current->next = newcar;
}
return first;
}
void print_train(struct car* first) {
if(first != NULL){
struct car* current = first;
while(current != NULL){
printf("%s\n", current->value);
current = current->next;
}
}
}
void cancel_train(struct car* first) {
struct car* current = first;
while(first != NULL){
current = current->next;
free(first);
first = current;
}
}
struct car* clear_train(struct car* first, const char* target) {
if (first == NULL) {
return NULL;
}
struct car* current = first;
struct car* previous = NULL;
while (current != NULL && strcmp(current->value, target) == 0) {
struct car* temp = current;
first = current->next;
current = first;
free(temp);
}
while (current != NULL) {
if (strcmp(current->value, target) == 0) {
previous->next = current->next;
free(current);
current = previous->next;
} else {
previous = current;
current = current->next;
}
}
return first;
}

51
cv4/a_train.h Normal file
View File

@ -0,0 +1,51 @@
#ifndef TRAIN_H
#define TRAIN_H
#define SIZE 100
/**
* Jeden vozen vlaku
*/
struct car {
/**
* Nazov cielovej stanice
*/
char value[SIZE];
/**
* Smenik na dalsi vozen
*/
struct car* next;
};
/**
* Prida vozen na koniec vlaku.
*
* @arg nazov cielovej stanice, ktory sa ma priradit novemu voznu.
* @return smernik na zaciatok vlaku.
*/
struct car* add_car(struct car* first,const char* target);
/**
* Vypise vsetky vozne vo vlaku
*
* @arg smernik na prvy vozen
*/
void print_train(struct car* first);
/**
* Zrusenie vsetkych voznov vo vlaku.
* @arg smernik na prvy vozen
*/
void cancel_train(struct car* first);
/**
* Vyradenie vsetkych voznov, ktorych cielova stanica je target
*
* @arg smernik na prvy vozen
* @arg cielova stanica, ktora sa ma vyradit z vlaku.
* @return smernik na novy prvy vozen
*
*/
struct car* clear_train(struct car* first,const char* target);
#endif // TRAIN_H

85
cv4/main.c Normal file
View File

@ -0,0 +1,85 @@
#include "a_train.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
struct car* train = NULL;
int choice;
char station[100];
while (1) {
printf("\nMenu:\n");
printf("1. Add station\n");
printf("2. Remove a station\n");
printf("3. Print route\n");
printf("4. Cancel train\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch(choice) {
case 1:
while (1) {
printf("Enter the station name to add (or press Enter to stop): ");
fgets(station, sizeof(station), stdin);
station[strcspn(station, "\n")] = '\0'; // Remove newline character
if (station[0] == '\0') {
break; // Exit the loop if input is empty
}
train = add_car(train, station);
printf("Station '%s' added to the train.\n", station);
}
break;
case 2:
while (1) {
printf("Enter the station name to clear (or press Enter to stop): ");
fgets(station, sizeof(station), stdin);
station[strcspn(station, "\n")] = '\0'; // Remove newline character
if (station[0] == '\0') {
break; // Exit the loop if input is empty
}
struct car* temp = train;
int found = 0;
while (temp != NULL) {
if (strcmp(temp->value, station) == 0) {
found = 1;
break;
}
temp = temp->next;
}
if (found) {
train = clear_train(train, station);
printf("Station '%s' deleted from the train.\n", station);
} else {
printf("Station '%s' does not exist in the train.\n", station);
}
}
break;
case 3:
printf("\nRoute List:\n");
print_train(train);
break;
case 4:
printf("\nCanceling the train...\n");
cancel_train(train);
train = NULL;
break;
case 5:
printf("Exiting program...\n");
cancel_train(train);
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}

13
cv6/Makefile Normal file
View File

@ -0,0 +1,13 @@
CFLAGS= -std=c99 -g -Wall -Werror
all: station
%.o: %.c
gcc -c -o $@ $< $(CFLAGS)
station: main.o a_station.o
gcc $(CFLAGS) main.o a_station.o -o station
clean:
rm *.o station

93
cv6/a_station.c Normal file
View File

@ -0,0 +1,93 @@
#include <stdio.h>
#include "a_station.h"
#include <stdlib.h>
#include <string.h>
struct station* create_station(){
struct station* station = (struct station*)calloc(1,sizeof(struct station));
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
station->track_count = STATION_SIZE;
return station;
}
void destroy_station(struct station* station){
for (int i = 0; i < station->track_count; i++) {
struct car* current = station->tracks[i];
while (current) {
struct car* temp = current;
current = current->next;
free(temp);
}
}
free(station->tracks);
free(station);
}
int select_track(struct station* station, const char* target){
size_t length = strlen(target);
int sum = 0;
for (size_t i = 0; i < length; i++) {
sum += target[i];
}
return sum % station->track_count;
}
void add_target_capacity(struct station* station,const char* target, int capacity){
int track = select_track(station, target);
struct car* current = station->tracks[track];
while (current) {
if (strcmp(current->value, target) == 0) {
current->capacity += capacity;
return;
}
current = current->next;
}
struct car* new_car = malloc(sizeof(struct car));
strncpy(new_car->value, target, TARGET_SIZE - 1);
new_car->value[TARGET_SIZE - 1] = '\0';
new_car->capacity = capacity;
new_car->next = station->tracks[track];
station->tracks[track] = new_car;
}
int get_target_capacity(struct station* station, const char* target) {
int track = select_track(station, target);
struct car* current = station->tracks[track];
while (current) {
if (strcmp(current->value, target) == 0) {
return current->capacity;
}
current = current->next;
}
return 0;
}
int count_targets(struct station* station) {
int count = 0;
for (int i = 0; i < station->track_count; i++) {
struct car* current = station->tracks[i];
while (current) {
count++;
current = current->next;
}
}
return count;
}
int count_capacity(struct station* station) {
int total_capacity = 0;
for (int i = 0; i < station->track_count; i++) {
struct car* current = station->tracks[i];
while (current) {
total_capacity += current->capacity;
current = current->next;
}
}
return total_capacity;
}

91
cv6/a_station.h Normal file
View File

@ -0,0 +1,91 @@
#ifndef STATION_H
#define STATION_H
// Pocet trati k dispozicii
#define STATION_SIZE 10
// Maximalny pocet znakov pre ulozenie nazvu cielovej stanice
#define TARGET_SIZE 36
/**
* Jeden zaznam o kapacite do cielovej stanice
*/
struct car {
// Cielova stanica / nazov
char value[TARGET_SIZE];
// Pocet cestujuchich
int capacity;
// Smernik na dalsi zaznam
struct car* next;
};
/**
* Cela databaza zaznamov
*/
struct station {
// Dynamicke pole smernikov na zaznamy
// jeden zaznam ma typ struct car*
struct car** tracks;
// Velkost pola tracks
int track_count;
};
/**
* Vytvori prazdnu stanicu.
* Alokuje pole smernikov tracks na pociatocnu kapacitu STATION_SIZE
* nastavi velkost capacity na STATION_SIZE
* @return smernik na prazdnu stanicu
*/
struct station* create_station();
/**
* Uvolni pamat
* @param smernik na databazu
*/
void destroy_station(struct station* station);
/**
* Vyberie poziciu v poli station->tracks pre ulozenie zaznamu target
*
* Proces vyberu by mal splnat kriteria pre hash funkciu:
* - rovnaky retazec by mal vzdy mat rovnaky vysledok
* - pre rozne retazce by mali byt vysledky co najviac rozne
*
* @param smernik na databazu
* @param nazov cielovej stanice
* @return cislo v intervale 0 az N-1, kde N je station->track_count
*/
int select_track(struct station* station, const char* target);
/**
* Zvysi zaznam o pocte cestujucich do danej cielovej stanice.
*
* Najprv sa vyberie cielova trat pomocou select_track(). Ak zaznam neexistuje,
* vytvori sa novy. Ak zaznam na danej trati (spojkovom zozname) existuje, cislo sa pripocita.
* V databaze nesmu byt dva zaznamy s rovnakou cielovou stanicou.
*
* @param smernik na databazu
* @param nazov cielovej stanice
*/
void add_target_capacity(struct station* station,const char* target, int capacity);
/**
* Ziska zaznam o cielovej stanici.
* @param smernik na databazu
* @param nazov cielovej stanice
*
* @return kapacitu do cielovej stanice. Ak sa zaznam nenachedza, vrati nula.
*/
int get_target_capacity(struct station* station,const char* target);
/**
* Spocita pocet cielovych stanic
* @param smernik na databazu
*/
int count_targets(struct station* station);
/**
* Spocita kapacitu vo vsetkych zaznamoch
* @param smernik na databazu
*/
int count_capacity(struct station* station);
#endif

53
cv6/main.c Normal file
View File

@ -0,0 +1,53 @@
#include "a_station.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT 100
void test_station(struct station* station) {
char station_name[MAX_INPUT];
int capacity;
while (1) {
printf("Enter station name and capacity (or 'exit' to stop):\n");
printf("Station name: ");
if (!fgets(station_name, MAX_INPUT, stdin)) break;
// Remove trailing newline
station_name[strcspn(station_name, "\n")] = '\0';
if (strcmp(station_name, "exit") == 0) break;
printf("Capacity: ");
if (scanf("%d", &capacity) != 1) {
printf("Invalid input. Try again.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}
// Clear input buffer
while (getchar() != '\n');
add_target_capacity(station, station_name, capacity);
int track = select_track(station, station_name);
printf("Track for %s: %d\n", station_name, track);
int current_capacity = get_target_capacity(station, station_name);
printf("Current capacity to %s: %d\n\n", station_name, current_capacity);
}
printf("Final Statistics:\n");
printf("Total number of targets: %d\n", count_targets(station));
printf("Total capacity: %d\n", count_capacity(station));
}
int main() {
struct station* station = create_station();
test_station(station);
destroy_station(station);
return 0;
}

BIN
cv7/main Executable file

Binary file not shown.

109
cv7/program.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 100
struct tree {
char value[SIZE];
struct tree* left;
struct tree* right;
};
struct tree* read_tree() {
char buffer[SIZE];
memset(buffer, 0, SIZE);
char* r = fgets(buffer, SIZE, stdin);
if (!r || buffer[0] == '\n') {
return NULL;
}
buffer[strcspn(buffer, "\n")] = 0;
struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1);
return node;
}
struct tree* load_tree() {
struct tree* node = read_tree();
if (!node) {
return NULL;
}
if (node->value[0] != '*') {
node->left = load_tree();
node->right = load_tree();
} else if (node->left != NULL || node->right != NULL) {
printf("Chybna databaza\n");
free(node);
return NULL;
}
return node;
}
void run_tree(struct tree* tree) {
if (!tree) {
return;
}
if (tree->value[0] == '*') {
printf("*%s\nKoniec\n", tree->value + 1);
return;
}
printf("%s\n", tree->value);
char response;
if (scanf(" %c", &response) != 1) {
printf("Koniec vstupu\n");
return;
} else if (response != 'a' && response != 'n') {
printf("Nerozumiem\n");
return;
}
if (response == 'a') {
run_tree(tree->left);
} else {
run_tree(tree->right);
}
}
void destroy_tree(struct tree* tree) {
if (tree) {
destroy_tree(tree->left);
destroy_tree(tree->right);
free(tree);
}
}
void count_items(struct tree* tree, int* count) {
if (tree) {
if (tree->left == NULL && tree->right == NULL) {
(*count)++;
} else {
count_items(tree->left, count);
count_items(tree->right, count);
}
}
}
int main() {
struct tree* root = load_tree();
printf("Expert z bufetu to vie.\n");
if (!root) {
printf("Chybna databaza\n");
return 0;
}
int count = 0;
count_items(root, &count);
printf("Pozna %d druhov ovocia a zeleniny.\n", count);
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
run_tree(root);
destroy_tree(root);
return 0;
}

18
sk1/Makefile Normal file
View File

@ -0,0 +1,18 @@
CC = gcc
CFLAGS = -Wall -Wextra -std=c11
TARGET = compressor
all: $(TARGET)
$(TARGET): main.o compressor.o
$(CC) $(CFLAGS) -o $(TARGET) main.o compressor.o
main.o: main.c compressor.h
$(CC) $(CFLAGS) -c main.c
compressor.o: compressor.c compressor.h
$(CC) $(CFLAGS) -c compressor.c
clean:
rm -f *.o $(TARGET)

11
sk1/compressed.txt Normal file
View File

@ -0,0 +1,11 @@
0-A
0-n
0-t
0-o
2-
0-K
4-z
0-a
0-r
0-

BIN
sk1/compressor Executable file

Binary file not shown.

View File

@ -1,43 +1,64 @@
#include "compressor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#define BUFFER_SIZE 1024
#define LZ78_DICT_SIZE 256
// Trie (prefix tree) structure for LZ78 compression
struct dict {
int index;
struct dict* characters[LZ78_DICT_SIZE];
};
// Function prototypes
int compress(const char* input_file_name, const char* output_file_name);
int decompress(const char* input_file_name, const char* output_file_name);
void lz78_compress(FILE* in, FILE* out);
void lz78_decompress(FILE* in, FILE* out);
void print_usage() {
printf("Usage:\n");
printf(" ./compressor -c infile outfile Compress infile into outfile\n");
printf(" ./compressor -d compressed uncompressed Decompress compressed into uncompressed\n");
// Free the dictionary trie
void delete_dict(struct dict* tree) {
if (!tree) {
return;
}
for (int i = 0; i < LZ78_DICT_SIZE; i++) {
delete_dict(tree->characters[i]);
}
free(tree);
}
int main(int argc, char* argv[]) {
if (argc != 4) {
print_usage();
return 1;
}
// LZ78 Compression
void lz78_compress(FILE* in, FILE* out) {
struct dict* root = calloc(1, sizeof(struct dict));
struct dict* current = root;
int index = 1;
int c;
const char* mode = argv[1];
const char* infile = argv[2];
const char* outfile = argv[3];
if (strcmp(mode, "-c") == 0) {
return compress(infile, outfile);
} else if (strcmp(mode, "-d") == 0) {
return decompress(infile, outfile);
while ((c = fgetc(in)) != EOF) {
if (current->characters[c]) {
current = current->characters[c];
} else {
print_usage();
return 1;
current->characters[c] = calloc(1, sizeof(struct dict));
current->characters[c]->index = index++;
fprintf(out, "%d-%c\n", current->index, c);
current = root;
}
}
delete_dict(root);
}
// LZ78 Decompression
void lz78_decompress(FILE* in, FILE* out) {
char buffer[LZ78_DICT_SIZE][BUFFER_SIZE] = {""};
int index, dict_index = 1;
char c;
while (fscanf(in, "%d-%c\n", &index, &c) == 2) {
if (index > 0) {
fprintf(out, "%s", buffer[index]);
}
fputc(c, out);
snprintf(buffer[dict_index], BUFFER_SIZE, "%s%c", buffer[index], c);
dict_index++;
}
}
// Compress function
int compress(const char* input_file_name, const char* output_file_name) {
FILE* input_file = fopen(input_file_name, "rb");
if (!input_file) {
@ -61,6 +82,7 @@ int compress(const char* input_file_name, const char* output_file_name) {
return 0;
}
// Decompress function
int decompress(const char* input_file_name, const char* output_file_name) {
FILE* input_file = fopen(input_file_name, "rb");
if (!input_file) {
@ -84,54 +106,3 @@ int decompress(const char* input_file_name, const char* output_file_name) {
return 0;
}
// LZ78 compression
struct dict {
int index;
struct dict* characters[LZ78_DICT_SIZE];
};
void delete_dict(struct dict* tree) {
if (!tree) {
return;
}
for (int i = 0; i < LZ78_DICT_SIZE; i++) {
delete_dict(tree->characters[i]);
}
free(tree);
}
void lz78_compress(FILE* in, FILE* out) {
struct dict* root = calloc(1, sizeof(struct dict));
struct dict* current = root;
int index = 1;
int c;
while ((c = fgetc(in)) != EOF) {
if (current->characters[c]) {
current = current->characters[c];
} else {
current->characters[c] = calloc(1, sizeof(struct dict));
current->characters[c]->index = index++;
fprintf(out, "%d-%c\n", current->index, c);
current = root;
}
}
delete_dict(root);
}
// LZ78 decompression
void lz78_decompress(FILE* in, FILE* out) {
char buffer[LZ78_DICT_SIZE][BUFFER_SIZE] = {""};
int index;
char c;
while (fscanf(in, "%d-%c\n", &index, &c) == 2) {
if (index > 0) {
fprintf(out, "%s", buffer[index]);
}
fputc(c, out);
snprintf(buffer[index], BUFFER_SIZE, "%s%c", buffer[index], c);
}
}

View File

@ -8,4 +8,3 @@ int compress(const char* input_file_name, const char* output_file_name);
int decompress(const char* input_file_name, const char* output_file_name);
#endif // COMPRESSOR_H

BIN
sk1/compressor.o Normal file

Binary file not shown.

1
sk1/input.txt Normal file
View File

@ -0,0 +1 @@
Anton Kozar

30
sk1/main.c Normal file
View File

@ -0,0 +1,30 @@
#include "compressor.h"
#include <stdio.h>
#include <string.h>
void print_usage() {
printf("Usage:\n");
printf(" ./compressor -c infile outfile Compress infile into outfile\n");
printf(" ./compressor -d compressed uncompressed Decompress compressed into uncompressed\n");
}
int main(int argc, char* argv[]) {
if (argc != 4) {
print_usage();
return 1;
}
const char* mode = argv[1];
const char* infile = argv[2];
const char* outfile = argv[3];
if (strcmp(mode, "-c") == 0) {
return compress(infile, outfile);
} else if (strcmp(mode, "-d") == 0) {
return decompress(infile, outfile);
} else {
print_usage();
return 1;
}
}

BIN
sk1/main.o Normal file

Binary file not shown.

1
sk1/output.txt Normal file
View File

@ -0,0 +1 @@
Anton Kozar