Compare commits

...

No commits in common. "master" and "main" have entirely different histories.
master ... main

11 changed files with 219 additions and 424 deletions

116
du1/program.c Normal file
View File

@ -0,0 +1,116 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define LINESIZE 100
#define MAX_ITEMS 100
struct pizza {
float prize;
char name[LINESIZE];
};
char hacker_script(char c) {
if (isupper(c)) {
return tolower(c);
}
char numbers[] = "0123456789";
char letters[] = "oizeasbtbq";
for (int i = 0; i < 10; i++) {
if (c == numbers[i]) {
return letters[i];
}
}
return c;
}
int search_string(const char* heap, const char* needle) {
int heap_len = strlen(heap);
int needle_len = strlen(needle);
if (needle_len > heap_len) {
return -1;
}
for (int i = 0; i <= heap_len - needle_len; i++) {
int found = 1;
for (int j = 0; j < needle_len; j++) {
char h_char = hacker_script(heap[i + j]);
char n_char = hacker_script(needle[j]);
if (h_char != n_char) {
found = 0;
break;
}
}
if (found) {
return i;
}
}
return -1;
}
int read_pizza(struct pizza* item, FILE* stream) {
char line[LINESIZE];
char price_line[LINESIZE];
if (fgets(line, LINESIZE, stream) == NULL) {
return 0;
}
line[strcspn(line, "\n")] = 0;
if (fgets(price_line, LINESIZE, stream) == NULL) {
return 0;
}
char* endptr;
float value = strtof(price_line, &endptr);
if (endptr == price_line || value == 0.0f) {
return 0;
}
strcpy(item->name, line);
item->prize = value;
return 1;
}
int main() {
char search_term[LINESIZE];
struct pizza items[MAX_ITEMS];
int item_count = 0;
printf("Zadaj hladanu surovinu:\n");
if (fgets(search_term, LINESIZE, stdin) == NULL) {
printf("Chyba pri čítaní hľadaného reťazca\n");
return 1;
}
search_term[strcspn(search_term, "\n")] = 0;
printf("Zadaj jedalny listok:\n");
while (item_count < MAX_ITEMS && read_pizza(&items[item_count], stdin)) {
item_count++;
}
for (int i = 0; i < item_count; i++) {
if (search_string(items[i].name, search_term) != -1) {
printf("%s\n", items[i].name);
printf("%.2f\n", items[i].prize);
}
}
printf("Nacitanych %d poloziek.\n", item_count);
return 0;
}

View File

@ -1,52 +1,55 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 1000
#define SIZE 101
#define MAX_PIZZ 1000
#define MAX_NAZOV 101
struct pizza {
char name[SIZE];
float price;
};
typedef struct {
char nazov[MAX_NAZOV];
double cena;
} Pizza;
int porovnaj(const void *a, const void *b) {
const Pizza *p1 = (const Pizza *)a;
const Pizza *p2 = (const Pizza *)b;
if (p1->cena < p2->cena) return -1;
if (p1->cena > p2->cena) return 1;
return strcasecmp(p1->nazov, p2->nazov);
}
int main() {
struct pizza list[MAX];
int count = 0;
char name[SIZE];
char price_str[SIZE];
float price;
Pizza zoznam[MAX_PIZZ];
int n = 0;
while (1) {
if (fgets(name, SIZE, stdin) == NULL) break;
name[strcspn(name, "\n")] = 0;
if (strlen(name) == 0) break;
if (fgets(price_str, SIZE, stdin) == NULL) break;
price_str[strcspn(price_str, "\n")] = 0;
if (sscanf(price_str, "%f", &price) != 1) break;
if (price <= 0) break;
if (!fgets(zoznam[n].nazov, MAX_NAZOV, stdin))
break;
strcpy(list[count].name, name);
list[count].price = price;
count++;
if (count >= MAX) break;
zoznam[n].nazov[strcspn(zoznam[n].nazov, "\n")] = '\0';
if (strlen(zoznam[n].nazov) == 0)
break;
char riadok[50];
if (!fgets(riadok, sizeof(riadok), stdin))
break;
if (sscanf(riadok, "%lf", &zoznam[n].cena) != 1)
break;
n++;
if (n >= MAX_PIZZ) break;
}
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (list[j].price > list[j + 1].price ||
(list[j].price == list[j + 1].price && strcmp(list[j].name, list[j + 1].name) > 0)) {
struct pizza temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
for (int i = 0; i < count; i++) {
printf("%s\n%.6f\n", list[i].name, list[i].price);
qsort(zoznam, n, sizeof(Pizza), porovnaj);
for (int i = 0; i < n; i++) {
printf("%s\n%.6f\n", zoznam[i].nazov, zoznam[i].cena);
}
return 0;
}

View File

@ -1,6 +1,6 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_SIZE 10
@ -9,10 +9,17 @@ struct stack {
int size;
};
void push_stack(float value, struck stack *s){
void print_stack(struct stack *s) {
for (int i = 0; i < s->size; i++) {
printf("%.2f ", s->values[i]);
}
printf("\n");
}
void push_stack(struct stack *s, float value) {
if (s->size >= STACK_SIZE) {
printf("Zapsinik je plny\n")
exit (1);
printf("full stack\n");
exit(0);
}
s->values[s->size] = value;
s->size++;
@ -20,10 +27,57 @@ s->size++;
float pop_stack(struct stack *s) {
if (s->size <= 0) {
printf("zasobnik je prazdny \n");
exit(1);
printf("no input\n");
exit(0);
}
s->size--;
return s->values[s->size];
}
int main() {
struct stack s;
s.size = 0;
char input[100];
while (scanf("%s", input) == 1) {
if (strcmp(input, "+") == 0 || strcmp(input, "-") == 0 ||
strcmp(input, "*") == 0 || strcmp(input, "/") == 0) {
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(input, "/") == 0 && b == 0) {
printf("division by zero\n");
exit(0);
}
if (strcmp(input, "+") == 0) result = a + b;
else if (strcmp(input, "-") == 0) result = a - b;
else if (strcmp(input, "*") == 0) result = a * b;
else result = a / b;
push_stack(&s, result);
print_stack(&s);
}
else {
char *endptr;
float val = strtof(input, &endptr);
if (*endptr != '\0') {
printf("bad input\n");
exit(0);
}
push_stack(&s, val);
print_stack(&s);
}
}
printf("no input\n");
return 0;
}

View File

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

View File

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

View File

@ -1,51 +0,0 @@
#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

View File

@ -1,17 +0,0 @@
#include "a_train.h"
#include <stdio.h>
// Testovaci subor pre vlak
int main(){
struct car* train = NULL;
train = add_car(train,"Presov");
train = add_car(train,"Bratislava");
train = add_car(train,"Levoca");
train = add_car(train,"Spiska Nova Ves");
print_train(train);
clear_train(train,"Levoca");
print_train(train);
cancel_train(train);
return 0;
}

View File

@ -1,13 +0,0 @@
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

View File

@ -1,93 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "a_station.h"
static unsigned long vytvor_hash(const char* nazov){
unsigned long h=0;int z;
while((z=*nazov++))h=h*31+z;
return h;
}
struct station* create_station(){
struct station* stanica=malloc(sizeof(struct station));
if(!stanica)return NULL;
stanica->tracks=calloc(STATION_SIZE,sizeof(struct car*));
if(!stanica->tracks){free(stanica);return NULL;}
stanica->track_count=STATION_SIZE;
return stanica;
}
void destroy_station(struct station* stanica){
if(!stanica)return;
for(int i=0;i<stanica->track_count;i++){
struct car* a=stanica->tracks[i];
while(a){
struct car* d=a->next;
free(a);
a=d;
}
}
free(stanica->tracks);
free(stanica);
}
int select_track(struct station* stanica,const char* nazov){
if(!stanica||!nazov)return 0;
unsigned long h=vytvor_hash(nazov);
return (int)(h%stanica->track_count);
}
void add_target_capacity(struct station* stanica,const char* nazov,int pocet){
if(!stanica||!nazov)return;
int cislo=select_track(stanica,nazov);
struct car* a=stanica->tracks[cislo];
struct car* p=NULL;
while(a){
if(strcmp(a->value,nazov)==0){
a->capacity+=pocet;
return;
}
p=a;a=a->next;
}
struct car* novy=malloc(sizeof(struct car));
if(!novy)return;
strncpy(novy->value,nazov,TARGET_SIZE-1);
novy->value[TARGET_SIZE-1]='\0';
novy->capacity=pocet;
novy->next=NULL;
if(p==NULL)stanica->tracks[cislo]=novy;
else p->next=novy;
}
int get_target_capacity(struct station* stanica,const char* nazov){
if(!stanica||!nazov)return 0;
int cislo=select_track(stanica,nazov);
struct car* a=stanica->tracks[cislo];
while(a){
if(strcmp(a->value,nazov)==0)return a->capacity;
a=a->next;
}
return 0;
}
int count_targets(struct station* stanica){
if(!stanica)return 0;
int pocet=0;
for(int i=0;i<stanica->track_count;i++){
struct car* a=stanica->tracks[i];
while(a){pocet++;a=a->next;}
}
return pocet;
}
int count_capacity(struct station* stanica){
if(!stanica)return 0;
int suma=0;
for(int i=0;i<stanica->track_count;i++){
struct car* a=stanica->tracks[i];
while(a){suma+=a->capacity;a=a->next;}
}
return suma;
}

View File

@ -1,91 +0,0 @@
#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

View File

@ -1,41 +0,0 @@
#include "a_station.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void print_station(struct station* station){
// Vypise celu stanicu
printf("station>>>\n");
// Prechadza vsetky trate
for (int i = 0 ; i< station->track_count; i++){
struct car* start = station->tracks[i];
struct car* this = start;
// Prechadza vsetky vozne
while(this != NULL){
printf("%s %d -> ",this->value,this->capacity);
this=this->next;
}
printf("NULL\n");
}
printf("<<<station\n");
}
// Pre overenie ci to ide v poriadku
void test_station(struct station* station){
const char* stations[] = {"Presov","Kosice","Banska Bystrica","Banska Stiavnica","Povazska Bystrica","Snina","Humenne","Bratislava","Pezinok","Skalica","Ruzomberok","Bidovce","Michalovce","Poprad","Krompachy","Smizany","Vajkovce","Budimir","Modra","Myslava","Roznava","Gemerska Poloma","Stratena","Dobsina","Michalany","Kostolany"};
int size=26;
for (int i = 0; i < 50; i++){
int s = rand() % size;
int c = rand() % 20;
printf("%s %d\n",stations[s],c);
add_target_capacity(station,stations[s],c);
print_station(station);
}
}
int main(){
srand(time(NULL));
struct station* station = create_station();
test_station(station);
destroy_station(station);
return 0;
}