1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
...
12 typedef struct{
13 char *Name;
14 int Age;
15 double W;
16 double H;
17 }User;
...
66 while(fgets(line,200,fp)){
67 //Scan line and put each space seprated chars into vars.
68 sscanf(line,"%s %d %lf %lf",nameH,&ageH,&wH,&hH);
69 //Allocate memory
70 user = (User *) malloc(sizeof(User));
71 //Store vars into user structs.
72 user->Name = strdup(nameH);
73 user->Age = ageH;
74 user->W = wH;
75 user->H = hH;
76
...
89 //Insert into data structures
90 userV.push_back(user->Name);
91 hName.insert(pair<double,char*>(user->H,user->Name));
92 wName.insert(pair<double,char*>(user->W,user->Name));
93 ageName.insert(pair<int,char*>(user->Age,user->Name));
94 }
95 //--------------------------------------------------------------------
96 //Output Data in specified format.
97
98 //Declare Iterator variables
99 multimap<double,char*>::iterator iterhName;
100 multimap<double,char*>::iterator iterwName;
101 multimap<int,char*>::reverse_iterator iterAgeName;
102
103 //List of names in the same order as the input file.
104 printf("%s\n","====order====");
105 for(int i = 0; i<userV.size(); i++){
106 printf("%s\n",userV[i]);
...
150 //free up all memory allocated
151 char* e;
152 for(int i = 0; i < userV.size(); i++){
153 e = userV[i];
154 free(e);
155 }
156
157 free(user->Name);
158
159 for(iterhName = hName.begin(); iterhName != hName.end(); iterhName++){
160 delete((*iterhName).second);
161 }
162
163
164 for(iterwName = wName.begin(); iterwName != wName.end(); iterwName++){
165 delete((*iterwName).second);
166 }
167
168 for(iterAgeName = ageName.begin(); iterAgeName != ageName.end(); iterAgeName++){
169 delete((*iterwName).second);
170 }
|