How to make a clock / how to make it run in the background?

Asked

Viewed 116 times

1

I am making a code that rotates a clock to show when the time of use of a machine (informed by the user). In the code the clock is inside a while(1), so there is no way for the user to inform the time of use of the machine.

the code of the clock is this:

        while (1 == 1) {
        Sleep (100);
        segundos++;
        system("cls");

        if (segundos > 59) { minutos++; segundos = 0; }
        if (minutos > 59) { horas++; minutos = 0; }
        if (horas > 23) { dias++; horas = 0; }
        if (mes > 30) { mes++; dias = 0; }

        printf("%d: %d: %d %d %d", mes, dias, horas, minutos, segundos);

}

And the code to be informed the time is this:

printf("Digite 1 para maquinas, 2 para pessoas, 3 para estoque, 4 para horarios, 5 para alterar o nome do laboratorio e 6 para finalizar.\n");

scanf("%d", &y);
system("cls");

if(y == 1){
    printf("Vamo comecar o cadastro das maquinas.\n");
    printf("Quantas maquinas deseja cadastrar?\n");
        scanf("%d", &z);
    for(i=0; i<z; i++){
        fflush(stdin);
    printf("Qual o nome da maquina %d?\n", i+1);
        gets(m[c].maquina);

    printf("Qual o tempo de vida util da maquina?\n");
    printf("Expresse o tempo em meses.\n");
        scanf("%d", &m[c].tempo);
        fflush(stdin);
        c++;

    }

    system("cls");

    printf("Muito bem, o cadastramento das maquinas foi um sucesso!\n");
    printf("O programa avisa automaticamente quando o tempo de vida util da maquina expirar.\n");
}
    printf("Informacoes do cadastro de maquinas:\n\n");
    for(i=0; i<c; i++){
        printf("CADASTRO DA MAQUINA %d\n", i+1);
        printf("O nome da maquina eh %s\n", m[i].maquina);
        printf("A vida util da maquina eh de %d meses\n", m[i].tempo);
        if(m[i].maquina == mes){
            printf("A maquina necessita de manuntençao.\n");
        }
        printf("\n");

    }

I wonder if you have another way to create a clock or if you have a way to make that clock run in the code background.

1 answer

1

Voce can use the time function to catch the system time, in case the Sleep one stop on the processor would be like running a loop that does nothing just for delay or to spend machine cycles, on the other hand by the Voce team takes the current system time and does not need to wait for anything, see a basic example of how it works

#include <stdio.h>
#include <time.h>

int main(void){
   time_t tempo;
   struct tm *tempo_formatado;

   //pega o tempo atual em segundos deis de 1970
   tempo = time(NULL);

   //colocamos o tempo na estrutura tm para
   //converter em segundos, min, horas, dias e etc
   tempo_formatado = localtime(&tempo);

   //exibimos pela estrutra
   printf("segundos %d \n",tempo_formatado->tm_sec);
   printf("minutos %d \n",tempo_formatado->tm_min);
   printf("horas %d \n",tempo_formatado->tm_hour); 
}

inserir a descrição da imagem aqui

in the case of clock and quite simple just take the current time in a loop

#include <stdio.h>
#include <time.h>

int main(void){
   time_t tempo;
   struct tm *tempo_formatado;

   while(1){    
      tempo = time(NULL);

      tempo_formatado = localtime(&tempo);

      printf("%d",tempo_formatado->tm_hour);
      printf(":%d",tempo_formatado->tm_min);
      printf(":%d\n",tempo_formatado->tm_sec);  
   }
}

another thing that is possible is a counter just seeing the time difference between two team

#include <stdio.h>
#include <time.h>

int main(void){
   time_t tempo, tempo2;
   tempo = time(NULL);
   sleep(2);
   tempo2 = time(NULL);
   printf("%d \n", tempo2 - tempo);
}

or even compare a team to see if it has passed a certain time

#include <stdio.h>
#include <time.h>

int main(void){
   time_t tempo, tempo2;
   tempo = time(NULL);
   sleep(7);
   tempo2 = time(NULL);
   if(tempo2 > tempo + 5){
       printf("mais de 5 segundo\n");
   }
}

good has many things to do using the team ^^

  • The problem of time() is that the AP wants a method that "... does not [have] how the user informs the time of use of the machine", and user can delay maintenance indefinitely by changing Windows clock if you use time().

  • I have a question, from what I understand the team only takes the time of the system, has to get the date of the system ? Since in the case of my program it would take years, months and not just hours.

  • It would be necessary to be able to manipulate this date also, comparing with integers, that is, it would have to be informed in integers.

  • wrmute in this case there would be a good take time from an external server by socket, bad would be if person pull the cable from the net then program does not work kkk, use the Gettickcount api would be an alternative too.

  • Gabriel the team takes the day, week, month and year too. for the day in the month Voce uses tm_mday, the day in the year tm_yday, the month use tm_mon (starts at 0), the year use tm_year (return 117 = 2017)

  • @kodonokami: time() returns time_t, which is the number of seconds elapsed since January 1, 1970, GMT. to return a struct tm (with tm_yday etc) have to say localtime(time(NULL)) or gmtime(time(NULL)) to make the conversion (the first is in the local time zone, the second in UTC).

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.