113 lines
3.2 KiB
C
113 lines
3.2 KiB
C
#include "a_station.h"
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <string.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");
|
|
printf("%d", count_capacity(station));
|
|
}
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
// 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(){
|
|
//srand(time(NULL));
|
|
//Vytvorime stanicu
|
|
struct station* station = create_station();
|
|
//Nazov a capacita pre overenie spravnosti
|
|
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;
|
|
}
|
|
|