Advertisement
Irlan

Menu Principal 2.2

Mar 26th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. int main ()
  9. {
  10.     int menu;
  11.     int i,j,aux,numero[10],pesquisa, p, n, meio,inicio = 0,fim = 9;
  12.     string def = "sim";
  13.     string resposta;
  14.    
  15.     cout<<"Deseja preencher vetores?"<<endl;
  16.     cin>>resposta;
  17.     system("cls");  
  18.    
  19.         if(resposta=="sim")
  20.     {
  21.              for( i=0;i<10;i++){
  22.              cout<<"Digite o "<<i+1<<"o numero:"<<endl;
  23.              cin>>numero[i];
  24.              cin.ignore();
  25.              system ("CLS");}
  26.  
  27.            while(def=="sim")
  28.            {
  29.              cout<<"\n\n MENU PRINCIPAL \n\n";
  30.              cout<<"\n[1] - Busca Sequencial\n[2] - Busca Binaria \n[3] - Buble Sort \n[4] - Selecao \n[5] - Fim   "<<endl;
  31.              cout<<"\n\nDigite a sua opcao."<<endl;
  32.              cin>>menu;
  33.              
  34.              
  35.              
  36.  
  37.        
  38.          
  39.                  switch(menu)
  40.          {
  41.             case 1:
  42.             {
  43.             cout<<"Digite o valor a ser encontrado."<<endl;
  44.             cin>>pesquisa;
  45.             // processamento: Método de Pesquisa Sequencial
  46.             int encontrou = 0; // flag, 0- pesquisa “mal” sucedida
  47.                           //       1- pesquisa “bem” sucedida
  48.             p = 0;
  49.            
  50.             while ((p < i) && (encontrou == 0))
  51.             if (numero[p] == pesquisa)
  52.             {
  53.                 encontrou = 1;          // o elemento foi encontrado
  54.                 cout<<"O Valor desejado foi encontrado na posicao "<<p<<" do vetor.";
  55.             }
  56.             else
  57.             {    
  58.             p = p + 1;
  59.            
  60.             }
  61.            
  62.             cout<<"Deseja reiniciar?"<<endl;
  63.             cin>>def;
  64.             getch();
  65.            
  66.                
  67.             }
  68.            
  69.        
  70.             case 2:
  71.             {
  72.             for(i=0;i<9;i++)
  73.             for(j=i+1;j<10;j++)
  74.             if(numero[i]>numero[j])
  75.             {      
  76.            
  77.             aux=numero[i];
  78.             numero[i]=numero[j];
  79.             numero[j]=aux;
  80.             }
  81.  
  82.             cout<<"Digite o valor a ser encontrado."<<endl;
  83.             cin>>pesquisa;
  84.            
  85.             int encontrou = 0; // flag, 0- pesquisa “mal” sucedida
  86.                           //       1- pesquisa “bem” sucedida
  87.          
  88.              while ((inicio <= fim) && (encontrou == 0))
  89.               {
  90.               meio = (inicio + fim) / 2;
  91.               p = 0;
  92.              
  93.               if (numero[meio] == pesquisa)
  94.              
  95.               encontrou = 1;            // o elemento foi encontrado
  96.               if (numero[meio] > pesquisa)
  97.                   fim = meio - 1;               // primeira metade do vetor
  98.               else
  99.                   inicio = meio + 1;
  100.                 // segunda metade do vetor
  101.  
  102.              
  103.               }
  104.              
  105.               if(encontrou==1)
  106.               cout<<"O numero foi encontrado na posicao "<<meio+1<<endl;
  107.               else
  108.               cout<<"O numero digitado nao foi encontrado."<<endl;
  109.              
  110.              
  111.      
  112.              cout<<"Deseja reiniciar?"<<endl;
  113.              cin>>def;
  114.              getch();
  115.              
  116.             }
  117.            
  118.             case 3:
  119.                     {
  120.                
  121.                
  122.              for(i=0;i<9;i++)
  123.              for(j=i+1;j<10;j++)
  124.              if(numero[i]>numero[j])
  125.               {    
  126.            
  127.               aux=numero[i];
  128.               numero[i]=numero[j];
  129.               numero[j]=aux;
  130.               }
  131.                                  cout <<endl;    
  132.                                          cout<<"Ordem dos numeros e:"<<endl;
  133.                                          for(int i = 0 ;i < 10; i++)
  134.                                          cout <<"O numero  "<<numero[i]<<" na posicao "<<i+1<<endl;      
  135.                                          getch();
  136.      
  137.                 cout<<"Deseja reiniciar?"<<endl;
  138.                 cin>>def;
  139.                
  140.                          
  141.                          getch();
  142.                          
  143.              }
  144.                
  145.             case 4:
  146.                 {
  147.                    for (i=0; i<10; i++)
  148.                {
  149.                 int min;
  150.                 min = i;
  151.                 for (j=(i+1); j<10; j++)
  152.      
  153.                
  154.                 if (numero[j] < numero[min])
  155.                 min = j;      // min = posição do menor elemento
  156.                 aux = numero[i];   // swap: troca dois elementos de posição
  157.                 numero[i] = numero[min];
  158.                 numero[min] = aux;
  159.                 }          
  160.    
  161.                 for(i=0; i<10; i++)
  162.                 cout<<"Os numeros sao "<< numero[i]<<endl;
  163.                  
  164.                  cout<<"Deseja reiniciar?"<<endl;
  165.                 cin>>def;
  166.                
  167.                   getch();
  168.                          
  169.                 }
  170.                
  171.                         case 5:
  172.                 {
  173.                                 cout<<"Voce nao quis utilizar um programa feito por Irlan Freitas Jr.\n"<<endl;
  174.                                 cout<<"Os numeros que voce digitou\n"<<endl;
  175.                                 for(i=0;i<10;i++)
  176.                                 cout<<"Ordem de digitacao "<<numero[i]<<endl;
  177.                                
  178.                                
  179.                 cout<<"Deseja reiniciar?"<<endl;
  180.                 cin>>def;
  181.                
  182.                                 getch();
  183.                                
  184.                                
  185.                 }
  186.            
  187.            
  188.                         default:
  189.             {
  190.                 cout<<"Nao tem essa opcao"<<endl;
  191.                 cout<<"Deseja reiniciar?"<<endl;
  192.                 cin>>def;
  193.                
  194.                 getch();
  195.              }
  196.          
  197.                  }
  198.                  }
  199.                
  200.          
  201.        
  202.          
  203.                  
  204.     }
  205.         else
  206.         {
  207.         cout<<"Okay."<<endl;
  208.         getch();
  209.         return 0;
  210.        
  211.         }
  212.    
  213.  
  214.  
  215. getch();
  216. return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement