Xavistian

Ejercicio 1 semana 1

Aug 16th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. // Semana 1.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <conio.h>
  6. using namespace std;
  7. using namespace System;
  8.  
  9. void Imprimir_menu()
  10. {
  11.  
  12.     cout << "1. Ingrese un dato al final"; cout << endl;
  13.     cout << "2. Imprimir"; cout << endl;
  14.     cout << "3. Elminar un dato al final"; cout << endl;
  15.     cout << "4. Insertar un dato en la posicion deseada" << endl;
  16.     cout << "5. Salir"; cout << endl;
  17. }
  18. int * Inserta_final(int*arreglo, int &n, int dato)
  19. {
  20.     int *temp = new int[n + 1];
  21.     for (int i = 0; i < n; i++)
  22.         temp[i] = arreglo[i];
  23.     temp[n] = dato;
  24.     n++;
  25.     if (arreglo != NULL)
  26.         delete[]arreglo;
  27.     return temp;
  28. }
  29. int *Elimina_final(int *arreglo, int &n)
  30. {
  31.     int *temp = NULL;
  32.     if (n > 0)
  33.     {
  34.         temp = new int[n - 1];
  35.         for (int i = 0; i < n - 1; i++)
  36.             temp[i] = arreglo[i];
  37.         n--;
  38.         if (arreglo != NULL)
  39.             delete[]arreglo;
  40.         return temp;
  41.     }
  42. }
  43. int *Insertar_en_posicion(int *arreglo, int &n, int pos, int dato)
  44. {
  45.     int *temp = new int[n + 1];
  46.     cout << "Ingrese el dato" << endl;
  47.     cin >> dato;
  48.     cout << "Ingrese la posicion" << endl;
  49.     cin >> pos;
  50.     for (int i = 0; i < n; i++)
  51.         if (i<pos)
  52.         temp[i] = arreglo[i];
  53.         else temp[i + 1] = arreglo[i];
  54.     temp[pos] = dato;
  55.     n++;
  56.     if (arreglo != NULL)
  57.         delete[]arreglo;
  58.     return temp;
  59. }
  60. int main()
  61. {
  62.     int *arreglo=NULL;
  63.     int n = 0;
  64.     int opc;
  65.     int *temp;
  66.     do {
  67.         Imprimir_menu();
  68.         cin >> opc;
  69.         switch (opc)
  70.         {
  71.         case 1:
  72.             int dato;
  73.             cout << "Ingrese el dato:";
  74.             cin >> dato;
  75.             arreglo = Inserta_final(arreglo, n, dato);
  76.             break;
  77.         case 2: for (int i = 0; i < n; i++)
  78.             cout << arreglo[i] << " ";
  79.             cout << endl; break;
  80.         case 3:
  81.             arreglo = Elimina_final(arreglo, n); break;
  82.         case 4:
  83.             int pos;
  84.             arreglo = Insertar_en_posicion(arreglo, n, pos, dato); break;
  85.  
  86.         }
  87.     }
  88.     while (opc != 5);
  89.     _getch();
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment