2
This commit is contained in:
parent
6bb9e9ad08
commit
8bac8b96a3
120
du5/a_station.c
120
du5/a_station.c
@ -3,87 +3,91 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "a_station.h"
|
#include "a_station.h"
|
||||||
|
|
||||||
static unsigned long hash_string(const char* t){
|
static unsigned long vytvor_hash(const char* nazov){
|
||||||
unsigned long h=0;int c;
|
unsigned long h=0;int z;
|
||||||
while((c=*t++)){h=h*31+c;}
|
while((z=*nazov++))h=h*31+z;
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct station* create_station(){
|
struct station* create_station(){
|
||||||
struct station* s=malloc(sizeof(struct station));
|
struct station* stanica=malloc(sizeof(struct station));
|
||||||
if(s==NULL)return NULL;
|
if(!stanica)return NULL;
|
||||||
s->tracks=calloc(STATION_SIZE,sizeof(struct car*));
|
stanica->tracks=calloc(STATION_SIZE,sizeof(struct car*));
|
||||||
if(s->tracks==NULL){free(s);return NULL;}
|
if(!stanica->tracks){free(stanica);return NULL;}
|
||||||
s->track_count=STATION_SIZE;
|
stanica->track_count=STATION_SIZE;
|
||||||
return s;
|
return stanica;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_station(struct station* s){
|
void destroy_station(struct station* stanica){
|
||||||
if(s==NULL)return;
|
if(!stanica)return;
|
||||||
for(int i=0;i<s->track_count;i++){
|
for(int i=0;i<stanica->track_count;i++){
|
||||||
struct car* c=s->tracks[i];
|
struct car* a=stanica->tracks[i];
|
||||||
while(c!=NULL){
|
while(a){
|
||||||
struct car* n=c->next;
|
struct car* d=a->next;
|
||||||
free(c);
|
free(a);
|
||||||
c=n;
|
a=d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(s->tracks);
|
free(stanica->tracks);
|
||||||
free(s);
|
free(stanica);
|
||||||
}
|
}
|
||||||
|
|
||||||
int select_track(struct station* s,const char* t){
|
int select_track(struct station* stanica,const char* nazov){
|
||||||
if(s==NULL||t==NULL)return 0;
|
if(!stanica||!nazov)return 0;
|
||||||
unsigned long h=hash_string(t);
|
unsigned long h=vytvor_hash(nazov);
|
||||||
return (int)(h%s->track_count);
|
return (int)(h%stanica->track_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_target_capacity(struct station* s,const char* t,int cap){
|
void add_target_capacity(struct station* stanica,const char* nazov,int pocet){
|
||||||
if(s==NULL||t==NULL)return;
|
if(!stanica||!nazov)return;
|
||||||
int tr=select_track(s,t);
|
int cislo=select_track(stanica,nazov);
|
||||||
struct car* c=s->tracks[tr];
|
struct car* a=stanica->tracks[cislo];
|
||||||
struct car* p=NULL;
|
struct car* p=NULL;
|
||||||
while(c!=NULL){
|
while(a){
|
||||||
if(strcmp(c->value,t)==0){c->capacity+=cap;return;}
|
if(strcmp(a->value,nazov)==0){
|
||||||
p=c;c=c->next;
|
a->capacity+=pocet;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
p=a;a=a->next;
|
||||||
}
|
}
|
||||||
struct car* n=malloc(sizeof(struct car));
|
struct car* novy=malloc(sizeof(struct car));
|
||||||
if(n==NULL)return;
|
if(!novy)return;
|
||||||
strncpy(n->value,t,TARGET_SIZE-1);
|
strncpy(novy->value,nazov,TARGET_SIZE-1);
|
||||||
n->value[TARGET_SIZE-1]='\0';
|
novy->value[TARGET_SIZE-1]='\0';
|
||||||
n->capacity=cap;
|
novy->capacity=pocet;
|
||||||
n->next=NULL;
|
novy->next=NULL;
|
||||||
if(p==NULL)s->tracks[tr]=n;else p->next=n;
|
if(p==NULL)stanica->tracks[cislo]=novy;
|
||||||
|
else p->next=novy;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_target_capacity(struct station* s,const char* t){
|
int get_target_capacity(struct station* stanica,const char* nazov){
|
||||||
if(s==NULL||t==NULL)return 0;
|
if(!stanica||!nazov)return 0;
|
||||||
int tr=select_track(s,t);
|
int cislo=select_track(stanica,nazov);
|
||||||
struct car* c=s->tracks[tr];
|
struct car* a=stanica->tracks[cislo];
|
||||||
while(c!=NULL){
|
while(a){
|
||||||
if(strcmp(c->value,t)==0)return c->capacity;
|
if(strcmp(a->value,nazov)==0)return a->capacity;
|
||||||
c=c->next;
|
a=a->next;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int count_targets(struct station* s){
|
int count_targets(struct station* stanica){
|
||||||
if(s==NULL)return 0;
|
if(!stanica)return 0;
|
||||||
int cnt=0;
|
int pocet=0;
|
||||||
for(int i=0;i<s->track_count;i++){
|
for(int i=0;i<stanica->track_count;i++){
|
||||||
struct car* c=s->tracks[i];
|
struct car* a=stanica->tracks[i];
|
||||||
while(c!=NULL){cnt++;c=c->next;}
|
while(a){pocet++;a=a->next;}
|
||||||
}
|
}
|
||||||
return cnt;
|
return pocet;
|
||||||
}
|
}
|
||||||
|
|
||||||
int count_capacity(struct station* s){
|
int count_capacity(struct station* stanica){
|
||||||
if(s==NULL)return 0;
|
if(!stanica)return 0;
|
||||||
int sum=0;
|
int suma=0;
|
||||||
for(int i=0;i<s->track_count;i++){
|
for(int i=0;i<stanica->track_count;i++){
|
||||||
struct car* c=s->tracks[i];
|
struct car* a=stanica->tracks[i];
|
||||||
while(c!=NULL){sum+=c->capacity;c=c->next;}
|
while(a){suma+=a->capacity;a=a->next;}
|
||||||
}
|
}
|
||||||
return sum;
|
return suma;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user