Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. /*
  6. Nicolas Lazo
  7. Francisco Montes
  8. Tiago Tadstaldt
  9. */
  10.  
  11. struct QueueNode {
  12. unsigned clientCode, zoneCode, problemCode;
  13. QueueNode * next;
  14. };
  15.  
  16. void pushToQueue(QueueNode*& head, QueueNode*& tail) {
  17. QueueNode * newItem = new QueueNode;
  18.  
  19. cout << "Codigo de cliente? "; cin >> newItem->clientCode;
  20. cout << "Codigo de zona? "; cin >> newItem->zoneCode;
  21. cout << "Codigo de problema? "; cin >> newItem->problemCode;
  22.  
  23. if ( head == NULL && tail == NULL ) {
  24. newItem->next = NULL;
  25. head = newItem; tail = newItem;
  26. }
  27.  
  28. else {
  29. newItem->next = tail;
  30. tail = newItem;
  31. }
  32. }
  33.  
  34. void popFromQueue(QueueNode*& head, QueueNode*& tail) {
  35. if ( head == tail ) {
  36. head == NULL;
  37. tail == NULL;
  38.  
  39. return;
  40. }
  41.  
  42. QueueNode * inspector = tail;
  43.  
  44. while ( inspector->next != head )
  45. inspector = inspector->next;
  46.  
  47. delete inspector->next;
  48. inspector->next = NULL;
  49. head = inspector;
  50. }
  51.  
  52. struct ProblemListNode {
  53. unsigned clientCode, problemCode;
  54.  
  55. ProblemListNode * next;
  56. };
  57.  
  58. void appendToList(ProblemListNode*& existingList, ProblemListNode*& toInsert) {
  59. ProblemListNode * previous = NULL,
  60. * inspector = existingList;
  61.  
  62. while ( inspector != NULL && toInsert->clientCode > inspector->clientCode ) {
  63. previous = inspector;
  64. inspector = inspector->next;
  65. }
  66.  
  67. if ( toInsert->clientCode == inspector->clientCode ) {
  68. while ( inspector != NULL && inspector->problemCode < toInsert->problemCode ) {
  69. previous = inspector;
  70. inspector = inspector->next;
  71. }
  72.  
  73. if ( inspector->problemCode == toInsert->problemCode )
  74. return;
  75. }
  76.  
  77. toInsert->next = inspector;
  78. previous->next = toInsert;
  79. }
  80.  
  81. struct ZoneListNode {
  82. unsigned zoneCode;
  83. ProblemListNode * problems;
  84.  
  85. ZoneListNode * next;
  86. };
  87.  
  88. int insertToZoneList(ZoneListNode*& zones, QueueNode*& head, QueueNode*& tail) {
  89. if ( head == NULL ) {
  90. return 1; // Cola vacia, no hay mas problemas para arreglar
  91. }
  92.  
  93. unsigned clientCode = head->clientCode,
  94. zoneCode = head->zoneCode,
  95. problemCode = head->problemCode;
  96.  
  97. popFromQueue(head, tail);
  98.  
  99. ZoneListNode * previous,
  100. * inspector = zones;
  101.  
  102. ZoneListNode * newZone = new ZoneListNode;
  103. newZone->zoneCode = zoneCode;
  104. ProblemListNode * problems = new ProblemListNode;
  105. problems->clientCode = clientCode;
  106. problems->problemCode = problemCode;
  107. newZone->problems = problems;
  108.  
  109. if ( inspector == NULL ) {
  110. newZone->next = NULL;
  111. problems->next = NULL;
  112. }
  113.  
  114. while ( inspector != NULL && inspector->zoneCode < zoneCode ) {
  115. previous = inspector;
  116. inspector = inspector->next;
  117. }
  118.  
  119. if ( inspector == NULL ) {
  120. previous->next = newZone;
  121. }
  122.  
  123. else if ( inspector->zoneCode == zoneCode ) {
  124. appendToList(inspector->problems, problems);
  125. }
  126.  
  127. else { // Insercion entre un codigo de zona menor y otro mayor
  128. newZone->next = inspector;
  129. previous->next = newZone;
  130. }
  131.  
  132. return 0;
  133. }
  134.  
  135. int main() {
  136. QueueNode * head = NULL,
  137. * tail = NULL;
  138. ZoneListNode * zones = new ZoneListNode;
  139. int in, justRepared;
  140.  
  141. cout << "1- Ingresar un nuevo pedido de reparacion" << endl;
  142. cout << "2- Actualizar las estructuras" << endl;
  143. cout << "0- Salir" << endl;
  144. cout << "? "; cin >> in;
  145.  
  146. while ( in ) {
  147. switch ( in ) {
  148. case 1:
  149. pushToQueue(head, tail);
  150. break;
  151. case 2:
  152. cout << "Reparaciones hechas? "; cin >> justRepared;
  153.  
  154. for (int i = 0; i < justRepared; i++) {
  155. if ( insertToZoneList(zones, head, tail) )
  156. break;
  157. }
  158. }
  159.  
  160. cout << "1- Ingresar un nuevo pedido de reparacion" << endl;
  161. cout << "2- Actualizar las estructuras" << endl;
  162. cout << "0- Salir" << endl;
  163. cout << "? "; cin >> in;
  164. }
  165.  
  166. FILE * resultsFile = fopen("problems.bin", "wb");
  167.  
  168. ProblemListNode * problemInspector;
  169. ZoneListNode * zoneInspector = zones;
  170.  
  171. while ( zoneInspector != NULL ) {
  172. fwrite(&(zoneInspector->zoneCode), sizeof(unsigned), 1, resultsFile);
  173.  
  174. problemInspector = zoneInspector->problems;
  175.  
  176. while ( problemInspector != NULL ) {
  177. fwrite(&(problemInspector->clientCode), sizeof(unsigned), 1, resultsFile);
  178. fwrite(&(problemInspector->problemCode), sizeof(unsigned), 1, resultsFile);
  179.  
  180. problemInspector = problemInspector->next;
  181. }
  182.  
  183. zoneInspector = zoneInspector->next;
  184. }
  185.  
  186. if ( head == NULL && tail == NULL ) {
  187. cout << "No quedaron pedidos sin distribuir" << endl;
  188. }
  189.  
  190. else {
  191. cout << "Los pedidos sin distrubuir fueron: " << endl;
  192.  
  193. while ( tail != NULL ) {
  194. cout << "Codigo de zona: " << tail->zoneCode << endl;
  195. cout << "Codigo de cliente: " << tail->clientCode << endl;
  196. cout << "Codigo de problema: " << tail->problemCode << endl;
  197. cout << endl;
  198.  
  199. tail = tail->next;
  200. }
  201. }
  202.  
  203. return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement