Advertisement
FoxTuGa

StartWinning [Not finished]

Oct 30th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. #define TRUE 1
  6.  
  7. typedef struct {
  8.     int num;
  9. }tagNumeros;
  10.  
  11. typedef struct {
  12.     tagNumeros numeros[5];
  13.     tagNumeros estrelas[2];
  14. }tagChaves;
  15.  
  16. char MENU(void);
  17. int InserirChave(tagChaves Keys[], int quantidade);
  18. void PAUSA(void);
  19. void copyarray_to_struct(tagNumeros Numeros[5], int Array_Orig[], int tam);
  20. int ApagarChave(tagChaves Keys[], int quantidade);
  21. void ApagarC(tagChaves Keys[], int num, int quantidade);
  22. void NextKey(tagChaves Keys[], int quantidade);
  23. void CountNums(int NumProb[], int tam, int *Menor, int *Menor_Posicao);
  24. void IniArray(int Array[], int num, int tam);
  25.  
  26. int main() {
  27.     char op;
  28.     int quantidade;
  29.     tagChaves Keys[15000];
  30.     quantidade = 0;
  31.  
  32.     while(TRUE) {
  33.         op = MENU();
  34.  
  35.         if( op == 't' )
  36.             return 0;
  37.         else    if( op == 'q' ) {
  38.             InserirChave(Keys, quantidade);
  39.             quantidade++;
  40.         }
  41.         else    if( op == 'w' ) {
  42.             ApagarChave(Keys, quantidade);
  43.             quantidade--;
  44.         }
  45.         else    if( op == 'e' ) {
  46.             NextKey(Keys, quantidade);
  47.         }
  48.     }
  49.  
  50.     return 0;
  51. }
  52.  
  53. char MENU(void) {
  54.     char c = 'x';
  55.  
  56.     system("cls");
  57.     while(c != 'q' && c != 'w' && c != 'e' && c != 'r' && c != 't') {
  58.         printf("\n\t\tMenu\n\n\n");
  59.         printf("q - Inserir chave\n");
  60.         printf("w - Apagar chave\n");
  61.         printf("e - Proxima Chave\n");
  62.         printf("r - Ver Chaves\n");
  63.         printf("t - Sair\n");
  64.         fflush(stdin);
  65.         c = getch();
  66.         system("cls");
  67.     }
  68.     return c;
  69. }
  70.  
  71. int InserirChave(tagChaves Keys[], int quantidade) {
  72.     int num[5] = {-1,-1,-1,-1,-1};
  73.     int est[2] = {-1,-1};
  74.     int idx, r;
  75.  
  76.     printf("\n\t\tInserir Chave %d\n\n", quantidade+1);
  77.     for(idx=0; idx < 7; idx++) {
  78.         if( idx < 5 ) {
  79.             printf("Numero %d: ", idx+1);
  80.             scanf("%d", &num[idx]);
  81.             if(num[idx] < 1 || num[idx] > 50) {
  82.                 system("cls");
  83.                 printf("Tem de estar entre 1 e 50!!\n");
  84.                 PAUSA();
  85.                 idx--;
  86.                 continue;
  87.             }
  88.         }
  89.         else {
  90.             printf("Estrela %d: ", idx-4);
  91.             scanf("%d", &est[idx-5]);
  92.             if(est[idx-5] < 1 || est[idx-5] > 9) {
  93.                 system("cls");
  94.                 printf("Tem de estar entre 1 e 9!!\n");
  95.                 PAUSA();
  96.                 idx--;
  97.                 continue;
  98.  
  99.             }
  100.         }
  101.     }
  102.  
  103.     copyarray_to_struct(Keys[quantidade].numeros,num, 5);
  104.     copyarray_to_struct(Keys[quantidade].estrelas,est, 2);
  105.  
  106.     return 1;
  107. }
  108.  
  109. void PAUSA(void) {
  110.     printf("\nPrima uma tecla para continuar\n");
  111.     fflush(stdin);
  112.     getch();
  113.     system("cls");
  114. }
  115.  
  116. void copyarray_to_struct(tagNumeros Numeros[5], int Array_Orig[], int tam) {
  117.     int idx;
  118.     for(idx=0; idx < tam; idx++)
  119.         Numeros[idx].num = Array_Orig[idx];
  120. }
  121.  
  122. int ApagarChave(tagChaves Keys[], int quantidade) {
  123.     int idx, key;
  124.     char c;
  125.     key = 0;
  126.     c = 's';
  127.  
  128.     while(TRUE) {
  129.         printf("\n\t\tApagar Chave\n\n\n");
  130.         printf("Chave: ");
  131.         scanf("%d", &key);
  132.         system("cls");
  133.         if( key < 1 || key > quantidade ) {
  134.             system("cls");
  135.             printf("Tem de estar entre 1 e %d!!\n\n", quantidade);
  136.             printf("Deseja repetir? s/n");
  137.             fflush(stdin);
  138.             c = getch();
  139.             if( c == 's' )
  140.                 continue;
  141.             if( c == 'n' )
  142.                 return 0;
  143.         }
  144.         else {
  145.             ApagarC(Keys, key, quantidade);
  146.             break;
  147.         }
  148.     }
  149.     return 1;
  150. }
  151.  
  152. void ApagarC(tagChaves Keys[], int num, int quantidade) {
  153.     int idx;
  154.  
  155.     num--;
  156.     for(idx = num; idx < quantidade; idx++)
  157.         Keys[idx] = Keys[idx+1];
  158. }
  159.  
  160. void NextKey(tagChaves Keys[], int quantidade) {
  161.     int idx, i, Menor, Menor_Posicao;
  162.     int NumProb[50], EstProb[9];
  163.     tagChaves NextKey;
  164.  
  165.     IniArray(NumProb, 0, 50);
  166.     IniArray(EstProb, 0, 9);
  167.     Menor = Menor_Posicao = 0;
  168.  
  169.     for(idx = 0; idx < quantidade; idx++) {
  170.         for(i=0; i < 7; i++) {
  171.             if( i < 5 )
  172.                 NumProb[Keys[idx].numeros[i].num-1]++;
  173.             else
  174.                 EstProb[Keys[idx].estrelas[i-5].num-1]++;
  175.         }
  176.     }
  177.  
  178.     for(idx = 0; idx < 7; idx++) {
  179.         if( idx < 5 ) {
  180.             CountNums(NumProb,5,&Menor,&Menor_Posicao);
  181.             NumProb[Menor_Posicao] = -1;
  182.             NextKey.numeros[idx].num = NumProb[MenorPosicao];
  183.         }
  184.         else {
  185.             CountNums(EstProb,2,&Menor,&Menor_Posicao);
  186.             EstProb[Menor_Posicao] = -1;
  187.             NextKey.estrelas[idx-5].num = EstProb[MenorPosicao];
  188.         }
  189.     }
  190.  
  191.     NextKey;
  192. }
  193.  
  194. void CountNums(int NumProb[], int tam, int *Menor, int *Menor_Posicao) {
  195.     int idx, menor, pos;
  196.  
  197.     menor = 0;
  198.  
  199.     for(idx = 0; idx < tam; idx++) {
  200.         if( ( NumProb[idx] < menor || menor == 0 ) && NumProb[idx] != -1) {
  201.             menor = NumProb[idx];
  202.             pos = idx;
  203.         }
  204.     }
  205.  
  206.     *Menor = menor;
  207.     *Menor_Posicao = pos;
  208. }
  209.  
  210. void IniArray(int Array[], int num, int tam) {
  211.     int idx;
  212.  
  213.     for(idx=0; idx < tam; idx++)
  214.         Array[idx] = num;
  215. }
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement