Advertisement
Jvsierra

Segunda Lista ATP II

Aug 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio2.h>
  3. #include <math.h>
  4. #include <ctype.h>
  5.  
  6. #define TF 100
  7.  
  8. void LeVetor(int vet[TF], int &TL)
  9. {
  10. int ele;
  11.  
  12. clrscr();
  13.  
  14. printf("Digite o numero %d:\n", TL);
  15. scanf("%d", &ele);
  16.  
  17. while(TL < TF && ele > 0)
  18. {
  19. vet[TL] = ele;
  20.  
  21. TL++;
  22.  
  23. printf("Digite o numero %d:\n", TL);
  24. scanf("%d", &ele);
  25. }
  26.  
  27. getch();
  28. }
  29.  
  30. void ImprimeVetor(int vet[TF], int TL)
  31. {
  32. int i;
  33.  
  34. clrscr();
  35.  
  36. printf("Elementos do vetor:\n");
  37.  
  38. if(TL == 0)
  39. printf("Vetor vazio\n");
  40. else
  41. for(i = 0; i < TL; i++)
  42. printf("[%d]:%d\n", i, vet[i]);
  43.  
  44. getch();
  45. }
  46.  
  47. void frequencia(int vet[TF], int TL, int &num, int &freq)
  48. {
  49. int i, j, cont, maisVezes = 0;
  50.  
  51. for(i = 0; i < TL; i++)
  52. {
  53. cont = 0;
  54.  
  55. for(j = 0; j < TL; j++)
  56. if(vet[i] == vet[j])
  57. cont++;
  58.  
  59. if(cont > maisVezes)
  60. {
  61. maisVezes = cont;
  62. num = vet[i];
  63. }
  64. }
  65.  
  66. freq = maisVezes;
  67. }
  68.  
  69. int somaElementos(int vet[TF], int TL)
  70. {
  71. int i, soma = 0;
  72.  
  73. for(i = 0; i < TF; i++)
  74. soma += vet[i];
  75.  
  76. return soma;
  77. }
  78.  
  79. int contemTl(int vet[TF], int TL)
  80. {
  81. int i;
  82.  
  83. for(i = 0; i < TL; i++)
  84. if(vet[i] == TL)
  85. return 1;
  86.  
  87. return 0;
  88. }
  89.  
  90. int decToBin(int dec)
  91. {
  92. int soma = 0, cont = 0;
  93.  
  94. while(dec >= 1)
  95. {
  96. if(dec % 2 == 1)
  97. soma += pow(10, cont);
  98.  
  99. cont++;
  100. dec /= 2;
  101. }
  102.  
  103. return soma;
  104. }
  105.  
  106. void subtracao(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF], int &TLC)
  107. {
  108. int i, pos;
  109.  
  110. for(i = 0; i < TLA; i++)
  111. {
  112. pos = 0;
  113.  
  114. while(pos < TLB && VB[pos] != VA[i])
  115. pos++;
  116.  
  117. if(pos == TLB)
  118. {
  119. pos = 0;
  120.  
  121. while(pos < TLC && VC[pos] != VA[i])
  122. pos++;
  123.  
  124. if(pos == TLC)
  125. {
  126. VC[TLC] = VA[i];
  127. TLC++;
  128. }
  129. }
  130. }
  131. }
  132.  
  133. void interseccao(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF], int &TLC)
  134. {
  135. int i, pos;
  136.  
  137. for(i = 0; i < TLA; i++)
  138. {
  139. pos = 0;
  140.  
  141. while(pos < TLB && VB[pos] != VA[i])
  142. pos++;
  143.  
  144. if(pos < TLB)
  145. {
  146. pos = 0;
  147.  
  148. while(pos < TLC && VC[pos] != VA[i])
  149. pos++;
  150.  
  151. if(pos == TLC)
  152. {
  153. VC[TLC] = VA[i];
  154. TLC++;
  155. }
  156. }
  157. }
  158. }
  159.  
  160. void merge(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF * 2], int &TLC)
  161. {
  162. int posA = TLA - 1, posB = TLB - 1, posC = (TLA + TLB) - 1;
  163.  
  164. while(posA >= 0 && posB >= 0)
  165. {
  166. if(VA[posA] > VB[posB])
  167. {
  168. VC[posC] = VA[posA];
  169. posA--;
  170. }
  171. else
  172. {
  173. VC[posC] = VB[posB];
  174. posB--;
  175. }
  176.  
  177. TLC++;
  178. posC--;
  179. }
  180. }
  181.  
  182. void uniao(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF * 2], int &TLC)
  183. {
  184. int i, pos;
  185.  
  186. for(i = 0; i < TLA; i++)
  187. {
  188. pos = 0;
  189.  
  190. while(pos < TLC && VC[pos] != VA[i])
  191. pos++;
  192.  
  193. if(pos < TLC)
  194. {
  195. VC[TLC] = VA[i];
  196. TLC++;
  197. }
  198. }
  199.  
  200. for(i = 0; i < TLB; i++)
  201. {
  202. pos = 0;
  203.  
  204. while(pos < TLC && VC[pos] != VB[i])
  205. pos++;
  206.  
  207. if(pos < TLC)
  208. {
  209. VC[TLC] = VB[i];
  210. TLC++;
  211. }
  212. }
  213. }
  214.  
  215. void separa(int vet[TF], int TL)
  216. {
  217. int i, j, pos = 0, aux;
  218.  
  219. for(i = 0; i < TL; i++)
  220. {
  221. if(vet[i] % 2 == 0)
  222. {
  223. aux = vet[i];
  224.  
  225. for(j = i; j > pos; j--)
  226. vet[i] = vet[i - 1];
  227.  
  228. vet[pos] = aux;
  229.  
  230. pos++;
  231. }
  232. }
  233. }
  234.  
  235. char menu(void)
  236. {
  237. clrscr();
  238.  
  239. printf("[A] - Qual numero aparece com mais frequencia - vetor A\n");
  240. printf("[B] - Qual numero aparece com mais frequencia - vetor B\n");
  241. printf("[C] - Soma dos elementos de A\n");
  242. printf("[D] - Soma dos elementos de B\n");
  243. printf("[E] - Checar se vetor A contem TL\n");
  244. printf("[F] - Checar se vetor B contem TL\n");
  245. printf("[G] - Decimal para binario\n");
  246. printf("[H] - Subtracao de conjuntos entre A e B - retorna C\n");
  247. printf("[I] - Interseccao de conjuntos entre A e B - retorna C\n");
  248. printf("[J] - Merge entre A e B\n");
  249. printf("[K] - Uniao de conjuntos\n");
  250. printf("[L] - Separar pares de impares do vetor A\n");
  251. printf("[M] - Separar pares de impares do vetor B\n");
  252. printf("[N] - Ler vetor A\n");
  253. printf("[O] - Ler vetor B\n");
  254. printf("[P] - Mostrar vetor A\n");
  255. printf("[Q] - Mostrar vetor B\n");
  256. printf("[R] - Mostrar vetor C\n");
  257. printf("[ESC] - sair\n");
  258. return toupper(getche());
  259. }
  260.  
  261. void executa(void)
  262. {
  263. int vetA[TF], tamA = 0, vetB[TF], tamB = 0, vetC[TF * 2], tamC = 0, freq, num, dec;
  264. char op;
  265.  
  266. op = menu();
  267.  
  268. while(op != 27)
  269. {
  270. switch(op)
  271. {
  272. case 'A':
  273. frequencia(vetA, tamA, freq, num);
  274.  
  275. printf("O numero %d aparece com mais frequencia (%d vezes)\n", num, freq);
  276. break;
  277. case 'B':
  278. frequencia(vetB, tamB, freq, num);
  279.  
  280. printf("O numero %d aparece com mais frequencia (%d vezes)\n", num, freq);
  281. break;
  282. case 'C':
  283. printf("Soma = %d\n", somaElementos(vetA, tamA));
  284. break;
  285. case 'D':
  286. printf("Soma = %d\n", somaElementos(vetB, tamB));
  287. break;
  288. case 'E':
  289. if(contemTl(vetA, tamA) == 1)
  290. printf("O vetor A tem o seu tamanho logico\n");
  291. else
  292. printf("O vetor A nao tem o seu tamanho logico nos elementos\n");
  293. break;
  294. case 'F':
  295. if(contemTl(vetB, tamB) == 1)
  296. printf("O vetor B tem o seu tamanho logico\n");
  297. else
  298. printf("O vetor B nao tem o seu tamanho logico nos elementos\n");
  299. break;
  300. case 'G':
  301. printf("Digite o valor em decimal:\n");
  302. scanf("%d", &dec);
  303.  
  304. printf("Valor em binario: %d\n", decToBin(dec));
  305. break;
  306. case 'H':
  307. subtracao(vetA, tamA, vetB, tamB, vetC, tamC);
  308.  
  309. ImprimeVetor(vetC, tamC);
  310. break;
  311. case 'I':
  312. interseccao(vetA, tamA, vetB, tamB, vetC, tamC);
  313.  
  314. ImprimeVetor(vetC, tamC);
  315. break;
  316. case 'J':
  317. merge(vetA, tamA, vetB, tamB, vetC, tamC);
  318.  
  319. ImprimeVetor(vetC, tamC);
  320. break;
  321. case 'K':
  322. uniao(vetA, tamB, vetB, tamB, vetC, tamC);
  323.  
  324. ImprimeVetor(vetC, tamC);
  325. break;
  326. case 'L':
  327. separa(vetA, tamA);
  328.  
  329. ImprimeVetor(vetC, tamC);
  330. break;
  331. case 'M':
  332. separa(vetB, tamB);
  333.  
  334. ImprimeVetor(vetC, tamC);
  335. break;
  336. case 'N':
  337. LeVetor(vetA, tamA);
  338. break;
  339. case 'O':
  340. LeVetor(vetB, tamB);
  341. break;
  342. case 'P':
  343. ImprimeVetor(vetA, tamA);
  344. break;
  345. case 'Q':
  346. ImprimeVetor(vetB, tamB);
  347. break;
  348. case 'R':
  349. ImprimeVetor(vetC, tamC);
  350. break;
  351. default:
  352. printf("Opcao invalida\n");
  353. }
  354.  
  355. op = menu();
  356. }
  357. }
  358.  
  359. int main(void)
  360. {
  361. executa();
  362.  
  363. getch();
  364. return 1;
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement