Advertisement
Guest User

new

a guest
Dec 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. #define MAX 200
  6.  
  7.  
  8. using namespace std;
  9.  
  10. struct MACHINE
  11. {
  12. int firNum, secNum, thiNum, fouNum;
  13. string name;
  14. };
  15.  
  16. bool ReadRecord(MACHINE &a, istream &in);
  17. void PrintRecord(MACHINE &a, ostream &out, bool NameOnly);
  18. void exchange(MACHINE &a, MACHINE &b);
  19. void compareNum(MACHINE &a, MACHINE &b);
  20.  
  21. int main()
  22. {
  23. bool nameOnly = true;
  24. int count = 0;
  25. int group[MAX] = {0};
  26. int groupCounter = 0;
  27. int printed[MAX] = {0};
  28. MACHINE individual[MAX];
  29.  
  30. cout << "What is the name of the input file?" << endl;
  31.  
  32. string iFileName;
  33. cin >> iFileName;
  34. ifstream text;
  35. text.open(iFileName.c_str());
  36. if(!text.is_open())
  37. {
  38. cout << "Error opening file!" << endl;
  39. return -1;
  40. }
  41.  
  42. //READS FILE INTO STRUCT
  43. while(count < MAX && ReadRecord(individual[count], text))
  44. {
  45. count++;
  46. }
  47. text.close();
  48.  
  49. cout << "What is the name of the output file?" << endl;
  50. string oFileName;
  51. cin >> oFileName;
  52. ofstream output;
  53. output.open(oFileName.c_str());
  54. if(!output.is_open())
  55. {
  56. cout << "Error opening file!" << endl;
  57. return -1;
  58. }
  59. //group name of same sub-network UNSORTED
  60. for(int i = 0; i <= count - 1; i++)
  61. {
  62. int addDash = 0;
  63. for(int j = i+1; j <= count; j++)
  64. {
  65. if(individual[i].firNum == individual[j].firNum
  66. && individual[i].secNum == individual[j].secNum)
  67. {
  68. if(printed[j] == 0 && printed[i] == 0)
  69. {
  70. groupCounter++;
  71. printed[j] = 1;
  72. printed[i] = 1;
  73. group[i] = groupCounter;
  74. group[j] = groupCounter;
  75. addDash++;
  76. PrintRecord(individual[i], output, nameOnly);
  77. PrintRecord(individual[j], output, nameOnly);
  78. }
  79. else if (printed[j] == 0)
  80. {
  81. printed[j] = 1;
  82. group [j] = groupCounter;
  83. PrintRecord(individual[j], output, nameOnly);
  84. }
  85. }
  86. }
  87. if(addDash > 0)
  88. {
  89. output << "-------------------------" << endl;
  90. }
  91. }
  92. // file contents UNSORTED
  93. output << "UNSORTED LIST" << endl << endl;
  94. for(int i = 0; i <= count; i++)
  95. {
  96. PrintRecord(individual[i], output, !nameOnly);
  97. }
  98.  
  99. output << "---------------------------------------------" << endl;
  100.  
  101. //sort machine by name
  102.  
  103. for(int i = 0; i <= count - 1; i++)
  104. {
  105. for(int j = i+1; j <= count; j++)
  106. {
  107. if(individual[i].name < individual[j].name)
  108. {
  109.  
  110. }
  111. else
  112. {
  113. exchange(individual[i], individual[j]);
  114. }
  115. }
  116. }
  117.  
  118. output << "SORTED BY MACHINE NAME" << endl << endl;
  119. for(int i = 0; i <= count; i++)
  120. {
  121. PrintRecord(individual[i], output, !nameOnly);
  122. }
  123.  
  124. output << "-----------------------------------------" << endl;
  125.  
  126. //move 0.0.0.0 none to the first position
  127. int i = 0;
  128. while(i <= count)
  129. {
  130. if(individual[i].firNum == 0 && individual[i].secNum == 0
  131. && individual[i].thiNum == 0 && individual[i].fouNum == 0)
  132. {
  133. exchange(individual[0], individual[i]);
  134. i = count + 1;
  135. }
  136. i++;
  137. }
  138.  
  139. // sort machine by IP
  140. for(int i = 1; i <= count - 1; i++)
  141. {
  142. for(int j = i+1; j <= count; j++)
  143. {
  144. compareNum(individual[i], individual[j]);
  145. }
  146. }
  147.  
  148. output << "SORTED BY IP ADDRESS" << endl << endl;
  149. for(int i = 0; i <= count; i++)
  150. {
  151. PrintRecord(individual[i], output, !nameOnly);
  152. }
  153. output << endl;
  154.  
  155. // Number of groups of 2+ machines in the file
  156. output << "There are " << groupCounter << " groups of two or more machines in the file!"
  157. << endl;
  158.  
  159. // Number of machines in the largest group
  160. int largestGroup = 0,test =0;
  161. for(int i = 1; i <= groupCounter; i++)
  162. {
  163. for(int j = 0; j < MAX - 1; j++)
  164. {
  165. if(group[j] == i)
  166. {
  167. test++;
  168. }
  169. }
  170. if(test > largestGroup)
  171. {
  172. largestGroup = test;
  173. }
  174. test = 0;
  175. }
  176.  
  177. output << "There are " << largestGroup << " machines in the largest group!"
  178. << endl;
  179.  
  180. // number of machines in the smallest group
  181. int smallestGroup = MAX + 1;
  182. for(int i = 1; i <= groupCounter; i++)
  183. {
  184. for(int j = 0; j <= count; j++)
  185. {
  186. if(group[j] == i)
  187. {
  188. test++;
  189. }
  190. }
  191. if(test < smallestGroup)
  192. {
  193. smallestGroup = test;
  194. }
  195. test = 0;
  196. }
  197. if(count == 0 || groupCounter == 0)
  198. {
  199. smallestGroup = 0;
  200. }
  201.  
  202. output << "There are " << smallestGroup << " machines in the smallest group!"
  203. << endl;
  204. return 0;
  205. }
  206.  
  207. void compareNum(MACHINE &a, MACHINE &b)
  208. {
  209. if(a.firNum < b.firNum)
  210. {
  211. exchange(a, b);
  212. }
  213. else if(a.firNum == b.firNum)
  214. {
  215. if(a.secNum < b.secNum)
  216. {
  217. exchange(a, b);
  218. }
  219. else if( a.secNum == b.secNum)
  220. {
  221. if(a.thiNum < b.thiNum)
  222. {
  223. exchange(a, b);
  224. }
  225. else if(a.thiNum == b.thiNum)
  226. {
  227. if(a.fouNum < b.fouNum)
  228. {
  229. exchange(a,b);
  230. }
  231. }
  232. }
  233. }
  234. }
  235.  
  236. void exchange(MACHINE &a, MACHINE &b)
  237. {
  238. string temp = a.name;
  239. int tempa = a.firNum, tempb = a.secNum,
  240. tempc = a.thiNum, tempd = a.fouNum;
  241.  
  242. a.name = b.name;
  243. a.firNum = b.firNum;
  244. a.secNum = b.secNum;
  245. a.thiNum = b.thiNum;
  246. a.fouNum = b.fouNum;
  247.  
  248. b.name = temp;
  249. b.firNum = tempa;
  250. b.secNum = tempb;
  251. b.thiNum = tempc;
  252. b.fouNum = tempd;
  253.  
  254. }
  255.  
  256. bool ReadRecord(MACHINE &z, istream &in)
  257. {
  258. char dot;
  259. if(in >> z.firNum >> dot >> z.secNum >> dot >> z.thiNum
  260. >> dot >> z.fouNum >> z.name)
  261. {
  262. if(z.firNum == 0 && z.secNum == 0 && z.thiNum == 0
  263. && z.fouNum == 0)
  264. {
  265. return false;
  266. }
  267. else
  268. {
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274.  
  275. void PrintRecord(MACHINE &a, ostream &out, bool z)
  276. {
  277. if(z)
  278. {
  279. out << a.name << endl;
  280. }
  281. else
  282. {
  283. int i = 0, j = 0, k = 0, l = 0;
  284. if(a.firNum > 99)
  285. {
  286. i = 0;
  287. }
  288. else if (a.firNum > 9)
  289. {
  290. i = 1;
  291. }
  292. else if (a.firNum > 0)
  293. {
  294. i = 2;
  295. }
  296. if(a.secNum > 99)
  297. {
  298. j = 0;
  299. }
  300. else if (a.secNum > 9)
  301. {
  302. j = 1;
  303. }
  304. else if (a.secNum > 0)
  305. {
  306. j = 2;
  307. }
  308. if(a.thiNum > 99)
  309. {
  310. k = 0;
  311. }
  312. else if (a.thiNum > 9)
  313. {
  314. k = 1;
  315. }
  316. else if (a.thiNum > 0)
  317. {
  318. k = 2;
  319. }
  320. if(a.fouNum > 99)
  321. {
  322. l = 0;
  323. }
  324. else if (a.fouNum > 9)
  325. {
  326. l = 1;
  327. }
  328. else if (a.fouNum > 0)
  329. {
  330. l = 2;
  331. }
  332. out << a.firNum << "." << a.secNum << "." << a.thiNum
  333. << "." << a.fouNum;
  334. int m = 4 + i + j + k + l;
  335. for(m; m > 0; m--)
  336. {
  337. out << " ";
  338. }
  339. out << a.name << endl;
  340. }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement