Advertisement
Guest User

Menu

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. int menu();
  9. void deletar();
  10. void inclusao();
  11. void buscaBinaria();
  12. void buscaSequencial();
  13.  
  14. int main(){
  15.     setlocale(0,"Portuguese");
  16.     menu();
  17.     system("pause");
  18. }
  19.  
  20. int menu(){
  21.  
  22.     system("COLOR 1F");
  23.  
  24.     //Variaveis do Menu
  25.     double opcao;
  26.  
  27.     //Menu
  28.     cout << "\n\t\tBusca Sequencial - Busca Binária - Inclusão - Deletar\n";
  29.     cout << "\n\t0 - Sair";
  30.     cout << "\n\t1 - Busca Sequencial";
  31.     cout << "\n\t2 - Busca Binária";
  32.     cout << "\n\t3 - Inclusão de Arquivos";
  33.     cout << "\n\t4 - Deletando Arquivos" << endl;
  34.     cout << "\nInforme a Opção: "; cin >> opcao;
  35.  
  36.     //Chamar o menu
  37.     if(opcao == 0){
  38.         return -1;
  39.     }else if(opcao == 1){
  40.         buscaSequencial();
  41.     }else if(opcao == 2){
  42.         buscaBinaria();
  43.     }else if (opcao == 3){
  44.         inclusao();
  45.     }else if(opcao == 4)
  46.         deletar();
  47.     else   
  48.         cout << "\n>>>>>>>>>> Opção Inválida. Tente Novamente.." << endl; system("pause"); system("cls"); menu();
  49.    
  50.     return 0;
  51. }
  52.  
  53. void deletar(){
  54.  
  55.     system("cls");
  56.  
  57.     const int tamanhoVetorS = 10;
  58.     const int tamanhoVetorT = 6;
  59.     const int tamanhoVetorA = tamanhoVetorS - tamanhoVetorT;
  60.  
  61.     int S[tamanhoVetorS] = {0,2,4,6,8,10,12,14,20,22};
  62.     int T[tamanhoVetorT] = {4,6,8,10,12,20};
  63.     int A[tamanhoVetorA];
  64.  
  65.     int i = 0;
  66.     int j = 0;
  67.     int z = 0;
  68.  
  69.     while(j < tamanhoVetorT){
  70.         if(S[i] != T[j]){
  71.             A[z] = S[i];
  72.             z++;
  73.          }else{
  74.             j++;
  75.          }
  76.         i++;
  77.     }
  78.    
  79.     while(i < tamanhoVetorS){
  80.         A[z] = S[i];
  81.         i++;
  82.         j++;
  83.     }
  84.  
  85.     cout << "\n>>>>> Impressão do Vetor S";
  86.     for (i=0; i<tamanhoVetorS;i++){
  87.         cout << "\nPosição [" << i+1 << "]: "<< S[i];
  88.     }
  89.  
  90.     cout << "\n\n\n>>>>> Impressão do Vetor T";
  91.     for (i=0; i<tamanhoVetorT;i++){
  92.         cout << "\nPosição [" << i+1 << "]: "<< T[i];
  93.     }
  94.  
  95.     cout << "\n\n\n>>>>> Impressão do Vetor A";
  96.    for(i=0; i<tamanhoVetorA; i++){
  97.        cout << "\nPosição [" << i+1 << "]: "<< A[i];
  98.    }
  99.  
  100.    cout << endl << endl;
  101. }
  102.  
  103. void inclusao(){
  104.     system("cls");
  105.    
  106.     const int vetorS = 4;
  107.     const int vetorT = 2;
  108.     const int vetorA = vetorS + vetorT;
  109.    
  110.     int S[vetorS] = {1,4,9,10};
  111.     int T[vetorT] = {2,5};
  112.     int A[vetorA];
  113.    
  114.     int i = 0;
  115.     int j = 0;
  116.     int z = 0;
  117.    
  118.     while(j<vetorT){
  119.         if(S[i] < T[j]){
  120.             A[z] = S[i];
  121.             i++;
  122.         }else{
  123.             A[z] = T[j];
  124.             j++;
  125.         }
  126.         z++;
  127.     }
  128.    
  129.      while(i<vetorS){
  130.         A[z] = S[i];
  131.         i++;
  132.         z++;
  133.     }
  134.    
  135.     cout << "\n\n\n>>>>> Impressão do Vetor A";
  136.        for(i=0; i<vetorA; i++){
  137.          cout << "\nPosição [" << i+1 << "]: "<< A[i];
  138.     }
  139.    
  140.     cout << endl << endl;
  141. }
  142.  
  143. void buscaBinaria(){
  144.     system("cls");
  145.         //Tamanho do Vetor
  146.     const int tamanhoVetor = 10;
  147.    
  148.     //Definição do Vetor
  149.     int vetor[tamanhoVetor] = {0,1,4,5,6,7,8,9,10,14};
  150.  
  151.     //Declaração das demais Variáveis
  152.     int chave;
  153.     int i = 0; //Indice do vetor
  154.     int inicio = 0;
  155.     int fim = tamanhoVetor-1;
  156.     int meio;
  157.    
  158.     cout << "\nImpressão do Vetor" << endl << endl;
  159.     for(i; i<tamanhoVetor;i++){cout << "Posição [" << i << "] " << vetor[i] << endl;}
  160.    
  161.     cout << "\nInforme a Chave a ser Buscada: "; cin >> chave;
  162.    
  163.     while(inicio <= fim){
  164.         meio = (inicio + fim) / 2;
  165.    
  166.         if(chave > vetor[meio]){
  167.             inicio = meio+1;
  168.         }else{
  169.             fim = meio-1;
  170.         }
  171.     }
  172.    
  173.     if(chave == vetor[meio]){
  174.         cout << "\nChave Encontrada na Posição " << meio << endl;
  175.     }else{
  176.         cout << "\nChave Não Encontrada!" << endl;
  177.     }
  178. }
  179.  
  180. void buscaSequencial(){
  181.     system("cls");
  182.    
  183.     const int tamanhoVetor = 4;
  184.     int vetor[tamanhoVetor] = {1,3,5,7};
  185.          
  186.     int k;
  187.     int i;
  188.    
  189.     cout << "\nInforme a Chave: "; cin >> k;
  190.    
  191.     cout << "\nChave a ser buscada [" << k << "]" << endl;
  192.  
  193.    
  194.     for(i=0; i<tamanhoVetor; i++){
  195.         cout << "Posição ["<< i << "]: " << vetor[i] << endl;
  196.     }
  197.    
  198.     for(i=0; vetor[i] != k && k > vetor[i]; i++);
  199.    
  200.     if(vetor[i] == k)
  201.         cout << "\nChave encontrada! Posição: " << i << endl << endl;
  202.     else
  203.         cout << "\nChave não encontrada!" << endl << endl;
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement