Advertisement
Irlan

Menu Principal Versão Final

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