Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio2.h>
- #include <math.h>
- #include <ctype.h>
- #define TF 100
- void LeVetor(int vet[TF], int &TL)
- {
- int ele;
- clrscr();
- printf("Digite o numero %d:\n", TL);
- scanf("%d", &ele);
- while(TL < TF && ele > 0)
- {
- vet[TL] = ele;
- TL++;
- printf("Digite o numero %d:\n", TL);
- scanf("%d", &ele);
- }
- getch();
- }
- void ImprimeVetor(int vet[TF], int TL)
- {
- int i;
- clrscr();
- printf("Elementos do vetor:\n");
- if(TL == 0)
- printf("Vetor vazio\n");
- else
- for(i = 0; i < TL; i++)
- printf("[%d]:%d\n", i, vet[i]);
- getch();
- }
- void frequencia(int vet[TF], int TL, int &num, int &freq)
- {
- int i, j, cont, maisVezes = 0;
- for(i = 0; i < TL; i++)
- {
- cont = 0;
- for(j = 0; j < TL; j++)
- if(vet[i] == vet[j])
- cont++;
- if(cont > maisVezes)
- {
- maisVezes = cont;
- num = vet[i];
- }
- }
- freq = maisVezes;
- }
- int somaElementos(int vet[TF], int TL)
- {
- int i, soma = 0;
- for(i = 0; i < TF; i++)
- soma += vet[i];
- return soma;
- }
- int contemTl(int vet[TF], int TL)
- {
- int i;
- for(i = 0; i < TL; i++)
- if(vet[i] == TL)
- return 1;
- return 0;
- }
- int decToBin(int dec)
- {
- int soma = 0, cont = 0;
- while(dec >= 1)
- {
- if(dec % 2 == 1)
- soma += pow(10, cont);
- cont++;
- dec /= 2;
- }
- return soma;
- }
- void subtracao(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF], int &TLC)
- {
- int i, pos;
- for(i = 0; i < TLA; i++)
- {
- pos = 0;
- while(pos < TLB && VB[pos] != VA[i])
- pos++;
- if(pos == TLB)
- {
- pos = 0;
- while(pos < TLC && VC[pos] != VA[i])
- pos++;
- if(pos == TLC)
- {
- VC[TLC] = VA[i];
- TLC++;
- }
- }
- }
- }
- void interseccao(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF], int &TLC)
- {
- int i, pos;
- for(i = 0; i < TLA; i++)
- {
- pos = 0;
- while(pos < TLB && VB[pos] != VA[i])
- pos++;
- if(pos < TLB)
- {
- pos = 0;
- while(pos < TLC && VC[pos] != VA[i])
- pos++;
- if(pos == TLC)
- {
- VC[TLC] = VA[i];
- TLC++;
- }
- }
- }
- }
- void merge(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF * 2], int &TLC)
- {
- int posA = TLA - 1, posB = TLB - 1, posC = (TLA + TLB) - 1;
- while(posA >= 0 && posB >= 0)
- {
- if(VA[posA] > VB[posB])
- {
- VC[posC] = VA[posA];
- posA--;
- }
- else
- {
- VC[posC] = VB[posB];
- posB--;
- }
- TLC++;
- posC--;
- }
- }
- void uniao(int VA[TF], int TLA, int VB[TF], int TLB, int VC[TF * 2], int &TLC)
- {
- int i, pos;
- for(i = 0; i < TLA; i++)
- {
- pos = 0;
- while(pos < TLC && VC[pos] != VA[i])
- pos++;
- if(pos < TLC)
- {
- VC[TLC] = VA[i];
- TLC++;
- }
- }
- for(i = 0; i < TLB; i++)
- {
- pos = 0;
- while(pos < TLC && VC[pos] != VB[i])
- pos++;
- if(pos < TLC)
- {
- VC[TLC] = VB[i];
- TLC++;
- }
- }
- }
- void separa(int vet[TF], int TL)
- {
- int i, j, pos = 0, aux;
- for(i = 0; i < TL; i++)
- {
- if(vet[i] % 2 == 0)
- {
- aux = vet[i];
- for(j = i; j > pos; j--)
- vet[i] = vet[i - 1];
- vet[pos] = aux;
- pos++;
- }
- }
- }
- char menu(void)
- {
- clrscr();
- printf("[A] - Qual numero aparece com mais frequencia - vetor A\n");
- printf("[B] - Qual numero aparece com mais frequencia - vetor B\n");
- printf("[C] - Soma dos elementos de A\n");
- printf("[D] - Soma dos elementos de B\n");
- printf("[E] - Checar se vetor A contem TL\n");
- printf("[F] - Checar se vetor B contem TL\n");
- printf("[G] - Decimal para binario\n");
- printf("[H] - Subtracao de conjuntos entre A e B - retorna C\n");
- printf("[I] - Interseccao de conjuntos entre A e B - retorna C\n");
- printf("[J] - Merge entre A e B\n");
- printf("[K] - Uniao de conjuntos\n");
- printf("[L] - Separar pares de impares do vetor A\n");
- printf("[M] - Separar pares de impares do vetor B\n");
- printf("[N] - Ler vetor A\n");
- printf("[O] - Ler vetor B\n");
- printf("[P] - Mostrar vetor A\n");
- printf("[Q] - Mostrar vetor B\n");
- printf("[R] - Mostrar vetor C\n");
- printf("[ESC] - sair\n");
- return toupper(getche());
- }
- void executa(void)
- {
- int vetA[TF], tamA = 0, vetB[TF], tamB = 0, vetC[TF * 2], tamC = 0, freq, num, dec;
- char op;
- op = menu();
- while(op != 27)
- {
- switch(op)
- {
- case 'A':
- frequencia(vetA, tamA, freq, num);
- printf("O numero %d aparece com mais frequencia (%d vezes)\n", num, freq);
- break;
- case 'B':
- frequencia(vetB, tamB, freq, num);
- printf("O numero %d aparece com mais frequencia (%d vezes)\n", num, freq);
- break;
- case 'C':
- printf("Soma = %d\n", somaElementos(vetA, tamA));
- break;
- case 'D':
- printf("Soma = %d\n", somaElementos(vetB, tamB));
- break;
- case 'E':
- if(contemTl(vetA, tamA) == 1)
- printf("O vetor A tem o seu tamanho logico\n");
- else
- printf("O vetor A nao tem o seu tamanho logico nos elementos\n");
- break;
- case 'F':
- if(contemTl(vetB, tamB) == 1)
- printf("O vetor B tem o seu tamanho logico\n");
- else
- printf("O vetor B nao tem o seu tamanho logico nos elementos\n");
- break;
- case 'G':
- printf("Digite o valor em decimal:\n");
- scanf("%d", &dec);
- printf("Valor em binario: %d\n", decToBin(dec));
- break;
- case 'H':
- subtracao(vetA, tamA, vetB, tamB, vetC, tamC);
- ImprimeVetor(vetC, tamC);
- break;
- case 'I':
- interseccao(vetA, tamA, vetB, tamB, vetC, tamC);
- ImprimeVetor(vetC, tamC);
- break;
- case 'J':
- merge(vetA, tamA, vetB, tamB, vetC, tamC);
- ImprimeVetor(vetC, tamC);
- break;
- case 'K':
- uniao(vetA, tamB, vetB, tamB, vetC, tamC);
- ImprimeVetor(vetC, tamC);
- break;
- case 'L':
- separa(vetA, tamA);
- ImprimeVetor(vetC, tamC);
- break;
- case 'M':
- separa(vetB, tamB);
- ImprimeVetor(vetC, tamC);
- break;
- case 'N':
- LeVetor(vetA, tamA);
- break;
- case 'O':
- LeVetor(vetB, tamB);
- break;
- case 'P':
- ImprimeVetor(vetA, tamA);
- break;
- case 'Q':
- ImprimeVetor(vetB, tamB);
- break;
- case 'R':
- ImprimeVetor(vetC, tamC);
- break;
- default:
- printf("Opcao invalida\n");
- }
- op = menu();
- }
- }
- int main(void)
- {
- executa();
- getch();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement