Advertisement
Jonas_3k

//Matriz unidimensional @ 6 functions //

Mar 29th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3. #define MAX 1000
  4. using namespace std;
  5. int vetor[MAX];
  6. inline void iniciar(void)
  7. {
  8.     for ( int i = 0 ; i < MAX ; i++)
  9.     {
  10.         vetor[MAX];
  11.     }
  12.     return;
  13. }
  14. void Adicionar(void)
  15. {
  16.     bool cond;
  17.     cond = false;
  18.     do
  19.     {
  20.         int p,v;
  21.         cout << "Informe a posicao eo valor: ";
  22.         cin  >> p >> v;
  23.         if( ( p < 0 ) || ( p > MAX ) )
  24.         {
  25.             cout << "Posicao invalida tente novamente\n";
  26.             cond = true;
  27.         }
  28.         else
  29.         {
  30.             vetor[p] = v;
  31.         }
  32.     }while(cond);
  33.     return;
  34. }
  35.  
  36. void Remover(void)
  37. {
  38.     int v;
  39.     cout << "Informe o valor que deseja remover: ";
  40.     cin >> v;
  41.     for(int i = 0 ; i < MAX ; i++)
  42.     {
  43.         if(vetor[i] == v)
  44.         {
  45.             vetor[i] = 0;
  46.             break;
  47.         }
  48.         else if( ( vetor[i] = 0 ) && ( ( v > -1 ) && ( v < MAX ) ) )
  49.         {
  50.             char opc;
  51.             cout << "Hum.. o valor informado nao esta contido no vetor, mas o valor e uma posicao valida"
  52.             << "\nQuer remover o valor " << vetor[v]
  53.             << "[S]im/[N]ao: ";
  54.             cin >> opc;
  55.             if ((opc == 's') || (opc == 'S'))
  56.             {
  57.                 vetor[v] = 0;
  58.                 return;
  59.             }
  60.             else return;
  61.  
  62.         }
  63.     }
  64. }
  65. void Exibir(void)
  66. {
  67.     for( int i = 0; i < MAX ; i++)
  68.     {
  69.         if(vetor[i]) cout << vetor[i] << "\t|";
  70.     }
  71. }
  72.  
  73. void Aleatorio(void)
  74. {
  75.     time(NULL);
  76.     for (int i = 0; i < MAX ; i++)
  77.     {
  78.         vetor[i] = rand() % MAX;
  79.     }
  80.     return;
  81. }
  82.  
  83. void Ordena(void)
  84. {
  85.      for( int i = 0 ; i < MAX ; i++)
  86.      {
  87.          for(int j = 0; j < MAX; j++)
  88.          {
  89.              int aux = vetor[j + 1];
  90.              if(aux < vetor[j])
  91.              {
  92.                  vetor[j + 1] = vetor[j];
  93.                  vetor[j] = aux;
  94.              }
  95.          }
  96.      }
  97.     return;
  98. }
  99. int main()
  100. {
  101.  
  102.     if(MAX > 32766)
  103.     {
  104.         cout << "Redefina MAX para um valor menor ";
  105.         return 0;
  106.     }
  107.     else iniciar();
  108.     do
  109.     {
  110.         int opc;
  111.         cout << "\nOque deseja fazer: "
  112.         <<"\n1- Adicionar um valor\n2 - Exibir\n3 - Zerar\n4 - Ordenar\n5 - Aleatorio\n6 - Sair\n\tDigite: ";
  113.         cin >> opc;
  114.         switch(opc)
  115.         {
  116.             case 1: Adicionar(); break;
  117.             case 2: Exibir(); break;
  118.             case 3: Remover(); break;
  119.             case 4: Ordena(); break;
  120.             case 5: Aleatorio(); break;
  121.             case 6: return 0;
  122.             default : cout << "\nOpcao invalida\n";
  123.         }
  124.  
  125.     }while(true);
  126.     return -1;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement