Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- using namespace std;
- /*
- Nicolas Lazo
- Francisco Montes
- Tiago Tadstaldt
- */
- struct QueueNode {
- unsigned clientCode, zoneCode, problemCode;
- QueueNode * next;
- };
- void pushToQueue(QueueNode*& head, QueueNode*& tail) {
- QueueNode * newItem = new QueueNode;
- cout << "Codigo de cliente? "; cin >> newItem->clientCode;
- cout << "Codigo de zona? "; cin >> newItem->zoneCode;
- cout << "Codigo de problema? "; cin >> newItem->problemCode;
- if ( head == NULL && tail == NULL ) {
- newItem->next = NULL;
- head = newItem; tail = newItem;
- }
- else {
- newItem->next = tail;
- tail = newItem;
- }
- }
- void popFromQueue(QueueNode*& head, QueueNode*& tail) {
- if ( head == tail ) {
- head == NULL;
- tail == NULL;
- return;
- }
- QueueNode * inspector = tail;
- while ( inspector->next != head )
- inspector = inspector->next;
- delete inspector->next;
- inspector->next = NULL;
- head = inspector;
- }
- struct ProblemListNode {
- unsigned clientCode, problemCode;
- ProblemListNode * next;
- };
- void appendToList(ProblemListNode*& existingList, ProblemListNode*& toInsert) {
- ProblemListNode * previous = NULL,
- * inspector = existingList;
- while ( inspector != NULL && toInsert->clientCode > inspector->clientCode ) {
- previous = inspector;
- inspector = inspector->next;
- }
- if ( toInsert->clientCode == inspector->clientCode ) {
- while ( inspector != NULL && inspector->problemCode < toInsert->problemCode ) {
- previous = inspector;
- inspector = inspector->next;
- }
- if ( inspector->problemCode == toInsert->problemCode )
- return;
- }
- toInsert->next = inspector;
- previous->next = toInsert;
- }
- struct ZoneListNode {
- unsigned zoneCode;
- ProblemListNode * problems;
- ZoneListNode * next;
- };
- int insertToZoneList(ZoneListNode*& zones, QueueNode*& head, QueueNode*& tail) {
- if ( head == NULL ) {
- return 1; // Cola vacia, no hay mas problemas para arreglar
- }
- unsigned clientCode = head->clientCode,
- zoneCode = head->zoneCode,
- problemCode = head->problemCode;
- popFromQueue(head, tail);
- ZoneListNode * previous,
- * inspector = zones;
- ZoneListNode * newZone = new ZoneListNode;
- newZone->zoneCode = zoneCode;
- ProblemListNode * problems = new ProblemListNode;
- problems->clientCode = clientCode;
- problems->problemCode = problemCode;
- newZone->problems = problems;
- if ( inspector == NULL ) {
- newZone->next = NULL;
- problems->next = NULL;
- }
- while ( inspector != NULL && inspector->zoneCode < zoneCode ) {
- previous = inspector;
- inspector = inspector->next;
- }
- if ( inspector == NULL ) {
- previous->next = newZone;
- }
- else if ( inspector->zoneCode == zoneCode ) {
- appendToList(inspector->problems, problems);
- }
- else { // Insercion entre un codigo de zona menor y otro mayor
- newZone->next = inspector;
- previous->next = newZone;
- }
- return 0;
- }
- int main() {
- QueueNode * head = NULL,
- * tail = NULL;
- ZoneListNode * zones = new ZoneListNode;
- int in, justRepared;
- cout << "1- Ingresar un nuevo pedido de reparacion" << endl;
- cout << "2- Actualizar las estructuras" << endl;
- cout << "0- Salir" << endl;
- cout << "? "; cin >> in;
- while ( in ) {
- switch ( in ) {
- case 1:
- pushToQueue(head, tail);
- break;
- case 2:
- cout << "Reparaciones hechas? "; cin >> justRepared;
- for (int i = 0; i < justRepared; i++) {
- if ( insertToZoneList(zones, head, tail) )
- break;
- }
- }
- cout << "1- Ingresar un nuevo pedido de reparacion" << endl;
- cout << "2- Actualizar las estructuras" << endl;
- cout << "0- Salir" << endl;
- cout << "? "; cin >> in;
- }
- FILE * resultsFile = fopen("problems.bin", "wb");
- ProblemListNode * problemInspector;
- ZoneListNode * zoneInspector = zones;
- while ( zoneInspector != NULL ) {
- fwrite(&(zoneInspector->zoneCode), sizeof(unsigned), 1, resultsFile);
- problemInspector = zoneInspector->problems;
- while ( problemInspector != NULL ) {
- fwrite(&(problemInspector->clientCode), sizeof(unsigned), 1, resultsFile);
- fwrite(&(problemInspector->problemCode), sizeof(unsigned), 1, resultsFile);
- problemInspector = problemInspector->next;
- }
- zoneInspector = zoneInspector->next;
- }
- if ( head == NULL && tail == NULL ) {
- cout << "No quedaron pedidos sin distribuir" << endl;
- }
- else {
- cout << "Los pedidos sin distrubuir fueron: " << endl;
- while ( tail != NULL ) {
- cout << "Codigo de zona: " << tail->zoneCode << endl;
- cout << "Codigo de cliente: " << tail->clientCode << endl;
- cout << "Codigo de problema: " << tail->problemCode << endl;
- cout << endl;
- tail = tail->next;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement