source: proiecte/GAIIA/Graf.cpp @ 122

Last change on this file since 122 was 122, checked in by (none), 14 years ago
File size: 2.4 KB
Line 
1#include "Graf.h"
2#include <stdio.h>
3#include <stdlib.h>
4
5/**
6* the constructor
7*
8* @param n the number of nodes
9* @param m the number of edges
10*/
11Graf::Graf(int n, int m)
12{
13        nr_noduri = n;
14        nr_muchii = m;
15        noduri = (Nod *)calloc (n, sizeof(Nod));
16        muchii = (Muchie *)calloc (m, sizeof (Muchie));
17}
18
19/**
20* the constructor
21*
22* @param filename the input filename
23*/
24Graf::Graf(char * filename)
25{
26        FILE *file = fopen(filename, "r");
27        int s, d;
28        double cost;
29
30        if (file == NULL)
31        { 
32                perror("Error reading file");
33                exit(1);
34        }
35        else
36        {
37                fscanf(file, "%i %i", &nr_noduri, &nr_muchii);
38                noduri = (Nod * )calloc(nr_noduri, sizeof(Nod));
39                muchii = (Muchie * )calloc(nr_muchii, sizeof(Muchie));
40                for(int i = 0 ; i < nr_noduri ; i ++)
41                {
42                        fscanf(file, "%lf", &cost);
43                        noduri[i] = Nod(i, cost);       
44                }
45                for(int i = 0 ; i < nr_muchii ; i ++)
46                {
47                        fscanf(file, "%i %i %lf", &s, &d, &cost);
48                        int gasit = 0, sursa = -1, destin = -1;
49                        for(int j = 0 ; j < nr_noduri ; j ++)
50                        {       
51                                if(gasit == 2) break;
52                                if(noduri[j].id == s) { sursa = j; gasit ++;} 
53                                if(noduri[j].id == d) { destin = j; gasit ++;}
54                        }
55                        muchii[i] = Muchie(&noduri[sursa], &noduri[destin], cost);
56                        noduri[destin].addPredecesor(&muchii[i]);       
57                        noduri[sursa].addSuccesor(&muchii[i]);
58                }
59        }
60        fclose (file);
61}
62
63/**
64* method printing the graph's information
65*/
66void Graf::print()
67{
68        printf("### graf\n");
69        printf("nr_noduri = %i, nr_muchii = %i\n", nr_noduri, nr_muchii);
70        for(int i = 0 ; i < nr_noduri ; i ++)
71                noduri[i].print();
72        for(int i = 0 ; i < nr_muchii ; i ++)   
73                muchii[i].print();
74        printf("###\n");
75}
76
77/**
78* copy operator
79*/
80Graf& Graf::operator=(const Graf& clone)
81{
82        //printf("!!!\n");
83        nr_noduri = clone.nr_noduri;
84        nr_muchii = clone.nr_muchii;
85        noduri = (Nod *)calloc(nr_noduri, sizeof(Nod));
86        muchii = (Muchie *)calloc(nr_muchii, sizeof(Muchie));
87        for(int i = 0 ; i < nr_noduri ; i ++)
88                noduri[i] = Task(clone.noduri[i].id, clone.noduri[i].cost);
89        for(int i = 0 ; i < nr_muchii ; i ++)
90        {
91                int gasit = 0, sursa = -1, destin = -1;
92                for(int j = 0 ; j < nr_noduri ; j ++)
93                {       
94                        if(gasit == 2) break;
95                        if(noduri[j].id == clone.muchii[i].predecesor->id) { sursa = j; gasit ++;} 
96                        if(noduri[j].id == clone.muchii[i].succesor->id) { destin = j; gasit ++;}
97                }
98                muchii[i] = Muchie(&noduri[sursa], &noduri[destin], clone.muchii[i].cost);
99                noduri[destin].addPredecesor(&muchii[i]);       
100                noduri[sursa].addSuccesor(&muchii[i]);
101        }
102        return *this;
103}
Note: See TracBrowser for help on using the repository browser.