Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #define TRUE 1
- typedef struct {
- int num;
- }tagNumeros;
- typedef struct {
- tagNumeros numeros[5];
- tagNumeros estrelas[2];
- }tagChaves;
- char MENU(void);
- int InserirChave(tagChaves Keys[], int quantidade);
- void PAUSA(void);
- void copyarray_to_struct(tagNumeros Numeros[5], int Array_Orig[], int tam);
- int ApagarChave(tagChaves Keys[], int quantidade);
- void ApagarC(tagChaves Keys[], int num, int quantidade);
- void NextKey(tagChaves Keys[], int quantidade);
- void CountNums(int NumProb[], int tam, int *Menor, int *Menor_Posicao);
- void IniArray(int Array[], int num, int tam);
- int main() {
- char op;
- int quantidade;
- tagChaves Keys[15000];
- quantidade = 0;
- while(TRUE) {
- op = MENU();
- if( op == 't' )
- return 0;
- else if( op == 'q' ) {
- InserirChave(Keys, quantidade);
- quantidade++;
- }
- else if( op == 'w' ) {
- ApagarChave(Keys, quantidade);
- quantidade--;
- }
- else if( op == 'e' ) {
- NextKey(Keys, quantidade);
- }
- }
- return 0;
- }
- char MENU(void) {
- char c = 'x';
- system("cls");
- while(c != 'q' && c != 'w' && c != 'e' && c != 'r' && c != 't') {
- printf("\n\t\tMenu\n\n\n");
- printf("q - Inserir chave\n");
- printf("w - Apagar chave\n");
- printf("e - Proxima Chave\n");
- printf("r - Ver Chaves\n");
- printf("t - Sair\n");
- fflush(stdin);
- c = getch();
- system("cls");
- }
- return c;
- }
- int InserirChave(tagChaves Keys[], int quantidade) {
- int num[5] = {-1,-1,-1,-1,-1};
- int est[2] = {-1,-1};
- int idx, r;
- printf("\n\t\tInserir Chave %d\n\n", quantidade+1);
- for(idx=0; idx < 7; idx++) {
- if( idx < 5 ) {
- printf("Numero %d: ", idx+1);
- scanf("%d", &num[idx]);
- if(num[idx] < 1 || num[idx] > 50) {
- system("cls");
- printf("Tem de estar entre 1 e 50!!\n");
- PAUSA();
- idx--;
- continue;
- }
- }
- else {
- printf("Estrela %d: ", idx-4);
- scanf("%d", &est[idx-5]);
- if(est[idx-5] < 1 || est[idx-5] > 9) {
- system("cls");
- printf("Tem de estar entre 1 e 9!!\n");
- PAUSA();
- idx--;
- continue;
- }
- }
- }
- copyarray_to_struct(Keys[quantidade].numeros,num, 5);
- copyarray_to_struct(Keys[quantidade].estrelas,est, 2);
- return 1;
- }
- void PAUSA(void) {
- printf("\nPrima uma tecla para continuar\n");
- fflush(stdin);
- getch();
- system("cls");
- }
- void copyarray_to_struct(tagNumeros Numeros[5], int Array_Orig[], int tam) {
- int idx;
- for(idx=0; idx < tam; idx++)
- Numeros[idx].num = Array_Orig[idx];
- }
- int ApagarChave(tagChaves Keys[], int quantidade) {
- int idx, key;
- char c;
- key = 0;
- c = 's';
- while(TRUE) {
- printf("\n\t\tApagar Chave\n\n\n");
- printf("Chave: ");
- scanf("%d", &key);
- system("cls");
- if( key < 1 || key > quantidade ) {
- system("cls");
- printf("Tem de estar entre 1 e %d!!\n\n", quantidade);
- printf("Deseja repetir? s/n");
- fflush(stdin);
- c = getch();
- if( c == 's' )
- continue;
- if( c == 'n' )
- return 0;
- }
- else {
- ApagarC(Keys, key, quantidade);
- break;
- }
- }
- return 1;
- }
- void ApagarC(tagChaves Keys[], int num, int quantidade) {
- int idx;
- num--;
- for(idx = num; idx < quantidade; idx++)
- Keys[idx] = Keys[idx+1];
- }
- void NextKey(tagChaves Keys[], int quantidade) {
- int idx, i, Menor, Menor_Posicao;
- int NumProb[50], EstProb[9];
- tagChaves NextKey;
- IniArray(NumProb, 0, 50);
- IniArray(EstProb, 0, 9);
- Menor = Menor_Posicao = 0;
- for(idx = 0; idx < quantidade; idx++) {
- for(i=0; i < 7; i++) {
- if( i < 5 )
- NumProb[Keys[idx].numeros[i].num-1]++;
- else
- EstProb[Keys[idx].estrelas[i-5].num-1]++;
- }
- }
- for(idx = 0; idx < 7; idx++) {
- if( idx < 5 ) {
- CountNums(NumProb,5,&Menor,&Menor_Posicao);
- NumProb[Menor_Posicao] = -1;
- NextKey.numeros[idx].num = NumProb[MenorPosicao];
- }
- else {
- CountNums(EstProb,2,&Menor,&Menor_Posicao);
- EstProb[Menor_Posicao] = -1;
- NextKey.estrelas[idx-5].num = EstProb[MenorPosicao];
- }
- }
- NextKey;
- }
- void CountNums(int NumProb[], int tam, int *Menor, int *Menor_Posicao) {
- int idx, menor, pos;
- menor = 0;
- for(idx = 0; idx < tam; idx++) {
- if( ( NumProb[idx] < menor || menor == 0 ) && NumProb[idx] != -1) {
- menor = NumProb[idx];
- pos = idx;
- }
- }
- *Menor = menor;
- *Menor_Posicao = pos;
- }
- void IniArray(int Array[], int num, int tam) {
- int idx;
- for(idx=0; idx < tam; idx++)
- Array[idx] = num;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement