finished commenting everything
This commit is contained in:
parent
d5ec73c414
commit
ff659f8685
@ -3,81 +3,117 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// funkcia pre vytvorenie stanice
|
||||||
struct station* create_station(){
|
struct station* create_station(){
|
||||||
|
// allokujeme dynamicke v pamati (na heape) pomocou calloc; vsetky byty su inticializovane na 0
|
||||||
|
// tak tiez, allokujeme 1 krat
|
||||||
struct station* station = (struct station*)calloc(1,sizeof(struct station));
|
struct station* station = (struct station*)calloc(1,sizeof(struct station));
|
||||||
|
// double pointer: potrebujeme menit hodnotu; to je pointer na adresu ineho pointeru, a tot - na hodnotu.
|
||||||
|
// Defaultne referencia je na prvy pointer pol'a. Allokacia 10 krat (makros)
|
||||||
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
|
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
|
||||||
station->track_count = STATION_SIZE;
|
station->track_count = STATION_SIZE;
|
||||||
return station;
|
return station;
|
||||||
}
|
}
|
||||||
|
/// funkcia pre uvolnenie pamati
|
||||||
void destroy_station(struct station* station){
|
void destroy_station(struct station* station){
|
||||||
|
// Mozeme uvolnit' array jednoducho, pretoze allokovali sme ho ako jeden 'kus'
|
||||||
free(station->tracks);
|
free(station->tracks);
|
||||||
|
// uvolnenie stanice
|
||||||
free(station);
|
free(station);
|
||||||
}
|
}
|
||||||
|
// Heshovacia funckica
|
||||||
int select_track(struct station* station, const char* target){
|
int select_track(struct station* station, const char* target){
|
||||||
|
/// Pouzivame unsigned int pretoze cislo nesmie byt' zapornym
|
||||||
unsigned int hash = 0;
|
unsigned int hash = 0;
|
||||||
|
///iteracia po prvkoch kluca
|
||||||
for (int i = 0; i < strlen(target); i++)
|
for (int i = 0; i < strlen(target); i++)
|
||||||
|
/// Operator << je bitovy posun vl'avo. To iste ako vynasobit' nieco na 2^n.
|
||||||
|
///Napriklad <<6 je to iste ako * 2^6
|
||||||
{ hash += target[i] + (hash<<6) + (hash<<16) - hash;
|
{ hash += target[i] + (hash<<6) + (hash<<16) - hash;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/// Budeme mat' obrovske cislo na e vysledku, aj tak musime pouzit' mod velkos'ti pol'a.
|
||||||
|
/// Index musi byt' v intervale od 0 do n-1.
|
||||||
hash = hash %station->track_count;
|
hash = hash %station->track_count;
|
||||||
|
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Funkica pre vytvorenie noveho nodu v poli alebo zvacsenie capacity
|
||||||
void add_target_capacity(struct station* station,const char* target, int capacity){
|
void add_target_capacity(struct station* station,const char* target, int capacity){
|
||||||
|
// Dostaneme index
|
||||||
int res = select_track(station, target);
|
int res = select_track(station, target);
|
||||||
|
/// Pomocny pointer pre predchadzajucy
|
||||||
struct car* previous = NULL;
|
struct car* previous = NULL;
|
||||||
|
// adresa hlavy pre iteraciu. Prvok pod indexom. Vlasne, potrebuejeme vziat' adresu nodu, co robime.
|
||||||
struct car** ptr = &station->tracks[res];
|
struct car** ptr = &station->tracks[res];
|
||||||
|
//Berieme hodnotu - adresu pointeru pre iteraciu.
|
||||||
struct car* head = *ptr;
|
struct car* head = *ptr;
|
||||||
|
/// Prechadzame po linked listu
|
||||||
while(head){
|
while(head){
|
||||||
|
//Ak destinacia nodu a hl'adane su rovnake, tak zvacsime capacitu nodu a prerusime program.
|
||||||
if(strcmp(head->value, target) == 0){
|
if(strcmp(head->value, target) == 0){
|
||||||
head->capacity += capacity;
|
head->capacity += capacity;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
/// Prechadzame linked list
|
||||||
previous = head;
|
previous = head;
|
||||||
|
|
||||||
head = head->next;
|
head = head->next;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// Ak sme vysli z cyklusu, to znamena ze polozka z prislusnym kl'ucom neexistuje a potrebuejeme vytvorit' nod.
|
||||||
|
|
||||||
|
//Vytvorame nod a prerdaime hodnoty
|
||||||
struct car* next = (struct car*)calloc(1, sizeof(struct car));
|
struct car* next = (struct car*)calloc(1, sizeof(struct car));
|
||||||
strcpy(next->value, target);
|
strcpy(next->value, target);
|
||||||
next->capacity = capacity;
|
next->capacity = capacity;
|
||||||
|
//Ak posledny nod nie je NULL, ho nasledujucy bude vytvoreny
|
||||||
if(previous){
|
if(previous){
|
||||||
previous->next = next;
|
previous->next = next;
|
||||||
}
|
}
|
||||||
|
//Inak, zoznam prazdny a pointer pojde na adresu v pamati vytvoreneho
|
||||||
else{
|
else{
|
||||||
*ptr = next;
|
*ptr = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Funkcia na dostanie capacity(hodnoty) kl'uca
|
||||||
|
|
||||||
int get_target_capacity(struct station* station,const char* target){
|
int get_target_capacity(struct station* station,const char* target){
|
||||||
|
/// Index res podl'a kl'uca
|
||||||
int res = select_track(station, target);
|
int res = select_track(station, target);
|
||||||
|
/// Pointer na jednotlivy linked list
|
||||||
struct car** ptr = &station->tracks[res];
|
struct car** ptr = &station->tracks[res];
|
||||||
|
/// Berieme prvy nod v nom pre prechadzanie, cize berieme hodnotu double pointeru;
|
||||||
struct car* head = *ptr;
|
struct car* head = *ptr;
|
||||||
|
/// pokial' nie jie NULL
|
||||||
while (head)
|
while (head)
|
||||||
|
// Ak najdeme v linked liste s prislusnym indexom nod z kl'com hl'adanym, tak vratime hodnotu tho nodu (capacitu)
|
||||||
{if(strcmp(head->value, target) == 0){
|
{if(strcmp(head->value, target) == 0){
|
||||||
return head->capacity;
|
return head->capacity;
|
||||||
}
|
}
|
||||||
|
/// prechadzanie
|
||||||
head = head->next;
|
head = head->next;
|
||||||
}
|
}
|
||||||
|
//Inak - nenajsli a vratime 0
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
// Funkcia pre pocet vsetkych destinacii
|
||||||
int count_targets(struct station* station){
|
int count_targets(struct station* station){
|
||||||
|
/// Ak je stanica NULL alebo pole NULL, vratime 0
|
||||||
|
/// || - logicky operator OR v C
|
||||||
if(station == NULL || station->tracks == NULL){
|
if(station == NULL || station->tracks == NULL){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
/// Iteracia po linked listoch pol'a
|
||||||
for(int i = 0; i<station->track_count; i++){
|
for(int i = 0; i<station->track_count; i++){
|
||||||
|
/// Pomocny pointer pre prechadzanie po jednotliyvych polozkach linked lista
|
||||||
struct car* ptr = station->tracks[i];
|
struct car* ptr = station->tracks[i];
|
||||||
|
/// Incrementujeme pocet ak nie je to NULL, respektive je nejaka destinacia a prechadzame d'alej
|
||||||
while(ptr){
|
while(ptr){
|
||||||
total++;
|
total++;
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
@ -85,22 +121,28 @@ int count_targets(struct station* station){
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/// Vratime pocet
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
/// Funkcia pre spocitanie vsetkych capacit (hodnot)
|
||||||
int count_capacity(struct station* station){
|
int count_capacity(struct station* station){
|
||||||
|
// Ak stanica je NULL alebo pole je NULL - vratime 0
|
||||||
if(station == NULL || station->tracks == NULL){
|
if(station == NULL || station->tracks == NULL){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
/// Iteracia po linked listoch v pole
|
||||||
for(int i = 0; i<station->track_count; i++){
|
for(int i = 0; i<station->track_count; i++){
|
||||||
|
/// Pomocny pointer pre prechadzanie
|
||||||
struct car* ptr = station->tracks[i];
|
struct car* ptr = station->tracks[i];
|
||||||
while(ptr){
|
while(ptr){
|
||||||
|
/// Pripocitame capacitu nodu a pojdeme d'alej
|
||||||
total += ptr->capacity;
|
total += ptr->capacity;
|
||||||
ptr = ptr->next;
|
ptr = ptr->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/// Vratime sucet
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
du5/a_station.o
BIN
du5/a_station.o
Binary file not shown.
10
du5/input.txt
Normal file
10
du5/input.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
Presov
|
||||||
|
26
|
||||||
|
Bratislava
|
||||||
|
100
|
||||||
|
Povazska Stanica
|
||||||
|
10
|
||||||
|
Povazska Bystrica
|
||||||
|
6
|
||||||
|
Povaszky Les
|
||||||
|
10
|
||||||
0
du5/input_2.txt
Normal file
0
du5/input_2.txt
Normal file
77
du5/main.c
77
du5/main.c
@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
void print_station(struct station* station){
|
void print_station(struct station* station){
|
||||||
// Vypise celu stanicu
|
// Vypise celu stanicu
|
||||||
@ -18,6 +19,7 @@ void print_station(struct station* station){
|
|||||||
printf("NULL\n");
|
printf("NULL\n");
|
||||||
}
|
}
|
||||||
printf("<<<station\n");
|
printf("<<<station\n");
|
||||||
|
printf("%d", count_capacity(station));
|
||||||
}
|
}
|
||||||
// Pre overenie ci to ide v poriadku
|
// Pre overenie ci to ide v poriadku
|
||||||
void test_station(struct station* station){
|
void test_station(struct station* station){
|
||||||
@ -32,10 +34,79 @@ void test_station(struct station* station){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Funkcia pre nacitanie od pouzivatel'a
|
||||||
|
void read_from_user(struct station* station){
|
||||||
|
/// Ak stanica je NULL, vytrvorime ju
|
||||||
|
if (station == NULL){
|
||||||
|
station = create_station();
|
||||||
|
}
|
||||||
|
/// Pokial' mame o nacitovat'. Respektive standartny vstup, subor napriklad, nie je NULL
|
||||||
|
while (stdin)
|
||||||
|
{// buffer pre nacitanie
|
||||||
|
char buff[36];
|
||||||
|
/// Pomocny pointer, kam pojdu vsetci znaky, ktore nie je vhodne pre nacitanie celeho cisla.
|
||||||
|
/// Obycajne je to '\0' - null terminator, koniec.
|
||||||
|
char* endptr;
|
||||||
|
/// Buffer pre nacitanie cisla.
|
||||||
|
char line[36];
|
||||||
|
|
||||||
|
/// Zrusime program ak to nepodarilo nacitat
|
||||||
|
if(! fgets(buff, 35, stdin)){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(! fgets(line, 35, stdin)){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/// Musime premenit newline "\n" na terminator pre spravne nacitanie cisla a zapis
|
||||||
|
buff[strcspn(buff, "\n")] = '\0';
|
||||||
|
line[strcspn(buff, "\n")] = '\0';
|
||||||
|
// Nacitanie celeho cisla. V podstate je analog strof ale pre cele cisla
|
||||||
|
int val = strtod(line, &endptr);
|
||||||
|
// Zapiseme v pole
|
||||||
|
add_target_capacity(station, buff, val);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
srand(time(NULL));
|
//srand(time(NULL));
|
||||||
|
//Vytvorime stanicu
|
||||||
struct station* station = create_station();
|
struct station* station = create_station();
|
||||||
test_station(station);
|
//Nazov a capacita pre overenie spravnosti
|
||||||
destroy_station(station);
|
char* nazov = "Bratislava";
|
||||||
|
int cap = 40;
|
||||||
|
// test_station(station);
|
||||||
|
// Nacitame zo suboru a vypiseme stanicu
|
||||||
|
read_from_user(station);
|
||||||
|
print_station(station);
|
||||||
|
add_target_capacity(station, "Bratislava", 100);
|
||||||
|
add_target_capacity(station, "Presov", 30);
|
||||||
|
add_target_capacity(station, "Michalovce", 9);
|
||||||
|
print_station(station);
|
||||||
|
printf("\nZadajte destinaciu: \n");
|
||||||
|
printf("%s\n", nazov);
|
||||||
|
printf("Zadajte pocet cestujucich: \n");
|
||||||
|
printf("%d\n", cap);
|
||||||
|
//Zvacsime capacitu danej stanice a vypiseme
|
||||||
|
add_target_capacity(station, nazov, cap);
|
||||||
|
printf("Capacita je %d\n", get_target_capacity(station, nazov));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// Uvolnime v pamati
|
||||||
|
destroy_station(station);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
du5/main.o
BIN
du5/main.o
Binary file not shown.
BIN
du5/station
BIN
du5/station
Binary file not shown.
Loading…
Reference in New Issue
Block a user