dimecres, 10 de març del 2021

Cronos




Aquest programa en C ens permet diferents utilitats com a cronometre.
Entre les seves funcions disposa la programacio de un compte enrere per tal de apagar el ordinador a una hora programada.

 
/*

        Cronos es un programa simple que compleix com a conometre d'anar per casa.
        Permet fer un compte atras i tancar el ordinador en el moment programat.
*/

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
HANDLE wHnd;


void menu();
int temporitzador(char a);
void cronometro(int thoraint tminuts,int tsegons);
void compenrera(int thoraint tminuts,int tsegons);
void bomba(int thoraint tminuts,int tsegons);
void alarma();
void credits();


int main (){
    credits
    SetConsoleTitle("Cronos V1.3");
    wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
    SMALL_RECT windowSize = {003015};
    SetConsoleWindowInfo(wHnd, 1, &windowSize);
    
    system("color 0A");
    menu();
    atexit(credits);    // Avans de tancar el programa pasarem per la funcio credits.
    return 0;
}

void menu(){

    char a,i;
    
    system("cls");
    printf("##############################\n");
    printf("#       Seleccioneu mode:    #\n");
    printf("##############################\n");
    printf("#                            #\n");
    printf("#    [1] Cronometre          #\n");
    printf("#    [2] Compte enrera       #\n");
    printf("#    [3] Mode bomba          #\n");
    printf("#                            #\n"); 
    printf("#    [0] sortir              #\n");
    printf("#                            #\n");
    printf("##############################\n");
        
    fflush(stdin);
     i = getch();
    
    switch(i){
        case '1': a = 'A';temporitzador(a);break;
        case '2': a = 'B';temporitzador(a);break
        case '3': a = 'C';temporitzador(a);break;
        case '0'break;
        default: menu(); break;
        
    }   
}

int temporitzador(char a){  
    int thora, tminuts, tsegons, maux;
    int x,i;
    char c; 
        
    thora = 0;                                  
    tminuts = 0;                            
    tsegons = 0;
    
    HANDLE hOut;                                    //Controlarem els colors de la consola.
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    do
        system("cls");
        printf("Cronos V.1 \n\n");
        for (x=1; x<=5;x++){
            for (i=1; i<=30;i++){               
                if((x == 1)||(x ==3)||(x ==5)||(i == 1)||(i == 7)||(x == 12)){
                    printf("#");
                }else if (x ==2 && i == 2){
                    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    printf(" [H] [M] [S]");
                    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    printf(" [R]");
                    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                    printf("   [i]");
                    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
                    printf(" (P)  ");
                    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                }else if ((x == 4)&&(i == 3)){
                    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    printf(" Temporitzador:  %.2i:%.2i:%.2i   ", thora,tminuts,tsegons);
                    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                }   
            }
        printf("\n");
        }
        
        c = toupper(getch());       // capturem un valor char i el pasem a majuscula. per seguir amb el codic.  
        
        if (c == 'I'){
            SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            printf("Informacio: \n  - Pulsa H para hores \n  - Pulsa M para min. \n  - Pulsa S para seg.");  
printf("\n  - Pulsa R para reset. \n  - Pulsa P para iniciar \n  - Pulsa ESC para sortir \n"); 
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            c = toupper(getch());
        }

        switch (c){ 
            case 'H':   printf("Hores  :  ");fflush(stdin);scanf("%i", & maux);thora += maux;break;
            case 'M':   printf("Minuts :  ");fflush(stdin);scanf("%i", & maux);tminuts += maux;break;
            case 'S':   printf("Segons :  ");fflush(stdin);scanf("%i", & maux);tsegons += maux;break;                      
            case 'R':   thora = 0; tminuts = 0; tsegons =  0;break;                                    
            case 'P':   if ( a == 'A'){cronometro(thora,tminuts,tsegons);}  
                        else if( a == 'B'){compenrera(thora,tminuts,tsegons);}
                        else if( a == 'C'){bomba(thora,tminuts,tsegons);}                       
                        break;          
            case 27 :   return 0;break;          
            default :   break;                             
        }
        
        if (tsegons >= 60){
            tminuts += tsegons / 60;
            tsegons = tsegons % 60;
        }
        if (tminuts >= 60){
            thora += tminuts / 60;
            tminuts = tminuts % 60;
        }

    }while(1);   
}

void cronometro(int thoraint tminuts,int tsegons){
    int hora,minuts,segons,x,i;
    
    HANDLE hOut;                                    //Controlarem els colors de la consola.
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    for( hora = 0; hora < 24; hora++){      
        for( minuts = 0; minuts < 60; minuts++){            
            for ( segons = 0; segons < 60; segons++){
                
                Sleep(1000);
                system("cls");
                printf("##############################\n#");
                
                SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                    printf("    Alarma:   %.2i:%.2i:%.2i      ", thora,tminuts,tsegons);
                                
                SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                    printf("#\n##############################\n#");
                                
                SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    printf("    Temps:    %.2i:%.2i:%.2i      ", hora,minuts,segons);
                                                    
                SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                    printf("#\n##############################\n");      
            
                if((thora == hora)&&(tminuts == minuts)&&(tsegons == segons))   {alarma();break;}               
                }                           
            if((thora == hora)&&(tminuts == minuts)&&(tsegons == segons))   {alarma();break;}
            }           
        if((thora == hora)&&(tminuts == minuts)&&(tsegons == segons))   {alarma();break;}   
        }
}

void compenrera(int thoraint tminuts,int tsegons){
    
    int hora,minuts,segons,x,i;

    HANDLE hOut;                                    //Controlarem els colors de la consola.
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    
    for( hora = thora; hora >= 0; hora--){      
        for( minuts = tminuts; minuts >= 0; minuts--){          
            for ( segons = tsegons; segons >= 0; segons--){
                Sleep(1000);
                
                
                Sleep(1000);
                system("cls");
                printf("CronosV1 \n\n##############################\n#");
                
                    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                    printf("    Alarma:   %.2i:%.2i:%.2i      ", hora,minuts,segons);
                    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                
                printf("#\n##############################\n");
                
                if((hora == 0)&&(minuts == 0)&&(segons == 0))   {alarma();break;}               
                }                           
            }               
        }       
}


void bomba(int thoraint tminuts,int tsegons){
//  SHUTDOWN /S /T 10                       en 10 segons windows s'apaga.

int hora,minuts,segons,x,i,pass;

    HANDLE hOut;                                    //Controlarem els colors de la consola.
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    system("color 40");
    system("cls");
    printf("CronosV1             DEATH PC\n\n##############################\n#");
    printf("   INTRODUEIX CLAU SEGURETAT:  \n   *:\r pass ");
    scanf("%d",&pass);
    printf("\n");   
        
while( !kbhit(pass) )   {   
    for( hora = thora; hora >= 0; hora--){      
        for( minuts = tminuts; minuts >= 0; minuts--){          
            for ( segons = tsegons; segons >= 0; segons--){
                Sleep(1000);
                
                system("cls");
                printf("CronosV1 \n\n##############################\n#");
                
                    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                    printf("    Alarma:   %.2i:%.2i:%.2i      ", hora,minuts,segons);
                    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                
                printf("#\n##############################\n");
                
                if((hora == 0)&&(minuts == 0)&&(segons == 0))   {;break;}               
                }                           
            }               
        }   
    }
}


void alarma(){  
    printf("ALARMA!\n");
    
    /*
    while ( !kbhit() )  {printf("\r\a"); Sleep(1000);}      // ALARMA sonora, pitara cada segon fins que pulsem un boto.
    */

    system("C:\\WINDOWS\\System32\\shutdown /s");       //Apagarem l'ordenador
}

void credits(){
    int i,j;
    
    printf("Emakt 2020 \n");    
    for(i=1;i<=15;i++){
        for (j=1;j<=15;j++){
        
            if          (i==1  && (j==6||j==7||j==8||j==9||j==10))                                          {printf("##");}                     
            else if     (i==2  && (j==4||j==5||j==8||j==11||j==12))                                         {printf("##");}     
            else if     (i==3  && (j==3||j==7||j==9||j==13))                                                {printf("##");}
            else if     (i==4  && (j==2||j==6||j==7||j==9||j==10||j==14))                                   {printf("##");} 
            else if     (i==5  && (j==1||j==2||j==6||j==10||j==14||j==15))                                  {printf("##");} 
            else if     (i==6  && (j==1||j==5||j==11||j==15))                                               {printf("##");} 
            else if     (i==7  && (j==1||j==5||j==11||j==15))                                               {printf("##");} 
            else if     (i==8  && (j==1||j==4||j==5||j==6||j==7||j==8||j==9||j==10||j==11||j==12||j==15))   {printf("##");} 
            else if     (i==9  && (j==1||j==4||j==12||j==15))                                               {printf("##");} 
            else if     (i==10 && (j==1||j==3||j==13||j==15))                                               {printf("##");}
            else if     (i==11 && (j==1||j==2||j==3||j==13||j==14||j==15))                                  {printf("##");} 
            else if     (i==12 && (j==2||j==14))                                                            {printf("##");} 
            else if     (i==13 && (j==3||j==13))                                                            {printf("##");} 
            else if     (i==14 && (j==4||j==5||j==11||j==12))                                               {printf("##");} 
            else if     (i==15 && (j==6||j==7||j==8||j==9||j==10))                                          {printf("##");} 
            else                                                                                            {printf("  ");}             
        }   
        printf("\n");
    }
    printf("                  No copy raid! \n"); 
    Sleep(1000);
}