Tag: totoloto

Guião laboratorial n.º 3 – exercício 6

“O ficheiro “totoloto.txt” contém todas as combinações que saíram desde que o jogo do totoloto começou. Desenvolva um programa que leia do teclado a chave em que um jogador apostou e a compare com cada uma das chaves armazenadas no ficheiro, escrevendo quantos números comuns existem para cada chave.”

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TAM 6

void totoloto(){
FILE *f1;
int ch,i=0, k=0, aux=1;
int tnum=0,contalinhas=0;
int chave[TAM], chavebd[TAM];

f1=fopen("totoloto.txt", "r");

if(f1==NULL){
  printf("\nerro ao abrir o ficheiro");
  return;
}

//pedir numeros ao utilizador
while(tnum<TAM){
  printf("Qual o %d numero?", tnum+1);
  scanf(" %d", &chave[tnum]);
  tnum++;
}
//
printf("\na sua chave:\n");
tnum=0;
while(tnum<6){
  printf("%d\t", chave[tnum]);
  tnum++;
}

printf("\nesta e a chave guardada no ficheiro:\n");
while((ch=fgetc(f1))!=EOF){
printf("%c", ch);
if(ch=='\n'){
  contalinhas++;
}
}
printf("\ntotal de linhas no ficheiro: %d", contalinhas);
fclose(f1);
tnum=0;
//-----------------------
f1=fopen("totoloto.txt", "r");
while(contalinhas>0){
  fscanf(f1,"%d %d %d %d %d %d",&chavebd[0], &chavebd[1], &chavebd[2], &chavebd[3], &chavebd[4], &chavebd[5]);
  for(i=0; i < TAM; i++){
    for(k=0; k < TAM; k++){
        if(chave[i]==chavebd[k]){
          tnum++;
        }
    }
  }
  printf("\nna %d chave total de iguais: %d", aux, tnum);
  aux++;
  tnum=0;
  contalinhas--;
}


fclose(f1);
}


int main()
{
    totoloto();
    return 0;
}


ficheiro TXT usado:
1 2 3 45 44 5
22 2 3 45 44 5
27 2 3 45 44 24
1 2 3 45 44 13
33 2 3 34 44 11

Tags : , ,