Advertisement
Guest User

Untitled

a guest
May 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <error.h>
  4. #include <memory.h>
  5.  
  6. #define NAMELEN  255
  7. #define STATIONS 5
  8.  
  9. struct station {
  10.  int id;
  11.  char  name[NAMELEN];
  12.  struct edge * edge;
  13. };
  14.  
  15. struct station* stations;//Ãëîáàëüíûé êîíòåéíåð äëÿ ñòàíöèé
  16.  
  17. struct edge {
  18.  struct station * to;
  19.  int weight;
  20.  struct edge * next;
  21. };
  22.  
  23.  
  24.  
  25.  
  26. struct station * getStationById(int id)//TODO: refactor
  27. {
  28.  int i;
  29.  for(i = 0; i < STATIONS; i++){
  30.   if(&stations[i] != NULL){
  31.    if(stations[i].id == id)
  32.     return &stations[i];
  33.   }
  34.  }
  35.  return NULL;
  36. }
  37.  
  38.  
  39. void createEdge(struct station * source, int to, int weight) {
  40.  struct edge *newEdge, *lastEdge;
  41.  struct station *destStation;
  42.  
  43.  newEdge = (struct edge*)malloc(sizeof(struct edge));
  44.  if(!newEdge) {
  45.   perror("Memory allocation failude while edge creation");
  46.   exit(1);
  47.  }
  48.  destStation = getStationById(to);
  49.  if(!destStation){
  50.   perror("Failed to find station while creation");
  51.   exit(1);
  52.  }
  53.  
  54.  newEdge->weight = weight;
  55.  newEdge->to = destStation;
  56.  newEdge->next = NULL;
  57.  
  58.  if(source->edge == NULL){//Ó ñòàíöèè åù¸  íåò ñâÿçåé
  59.   source->edge = newEdge;
  60.  } else {//Ó ñòàíöèè óæå åñòü ñâÿçè
  61.   lastEdge = source->edge;
  62.   while (lastEdge->next){
  63.    lastEdge = lastEdge->next;
  64.   }
  65.   lastEdge->next = newEdge;
  66.  }
  67.  
  68. }
  69.  
  70. void readEdges()
  71. {
  72.  struct {
  73.   int from;
  74.   int to;
  75.   int weight;
  76.  } edgeData;
  77.  char filename = "edges.dat";
  78.  
  79.  //Ñ÷èòûâàíèå äàííûõ è ñîçäàíèå ãðàíåé â ïàìÿòè
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87. int main(int argc, char * argv[])
  88. {
  89.     FILE *stream;
  90.  int n;
  91.  stations = (struct station*)calloc(STATIONS,sizeof(struct station));//Âûäåëåíèå ïàìÿòè ïîä ìàññèâ ñòàíöèé
  92.  memset(stations,0,sizeof(struct station)*STATIONS);
  93.  
  94.  if(!stations){
  95.   perror("Memory allocation failure.");
  96.   exit(1);
  97.  }
  98.  
  99.  stream = fopen("stations.dat", "wb");
  100.  if(stream == NULL){
  101.   perror("Failed to open stations file\n");
  102.   exit(1);
  103.  }
  104.  
  105.  stations[1].id = 10;
  106.  stations[2].id = 11;
  107.  
  108.  createEdge(&stations[1],11,5);
  109.  
  110.  
  111.  n = fwrite(stations,sizeof(struct station), STATIONS, stream);
  112.  
  113.  printf("Hello, world %d\n",n);
  114.  
  115.  fclose(stream);
  116.  
  117.  stations[1].id = 0;
  118.  
  119.  stream = fopen("stations.dat","rb");
  120.  if(stream == NULL){
  121.   perror("Failed to open stations file\n");
  122.   exit(1);
  123.  }
  124.  
  125.  n = fread(stations,sizeof(struct station),10,stream);
  126.  
  127.  printf("Hello, world %d; value is =%d \n",n,stations[1].id);
  128.  
  129.  
  130.  
  131.  getchar();
  132.  return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement