C language-Timed Shutdown program
C language-Timed Shutdown program
Note:
1. The conversion from the received char * type to the int type must not be converted using the (int) method. atoi () is used ()
Usage: Prototype: int atoi (const char * nptr );
2. Merge multiple strings and use sprintf ()
Usage: sprintf (char * buffer, const char * format [, argument,...])
Use the following methods:
# Include
# Include
# Include
# Include
# Include
Int isNumber (char * a); void getCurrentDate (int seconds); int main () {printf (Welcome To The Timed Shutdown System !); GetCurrentDate (0); char str [2]; char flag = 1; while (flag) {printf (input A Timed Shutdown, input C Timed Shutdown); gets (str ); if (strcmp (str, a) = 0) {printf (enter the time in seconds :); char seconds [10]; while (1) {gets (seconds ); if (isNumber (char *) seconds) break; else {printf (the input number of seconds is incorrect. Please input it again !); Continue ;}} int temp_seconds = atoi (seconds); getCurrentDate (temp_seconds); char shutdown [50] = shutdown-s-t; strcat (shutdown, seconds ); system (shutdown); system (pause); break;} else if (strcmp (str, C) = 0 | strcmp (str, c) = 0) {system (shutdown-a); printf (Timed shutdown has been canceled); system (pause); break;} else {printf (incorrect value, please enter it again !); Flag = 1 ;}}return 0 ;}// determines whether the string is a numeric int isNumber (char * a) {char flag = 1; int len = strlen (); for (int I = 0; I
0) {rawtime + = seconds;} timeinfo = localtime (& rawtime); int year = timeinfo-> tm_year + 1900; int month = timeinfo-> tm_mon + 1; int day = timeinfo-> tm_mday; int hour = timeinfo-> tm_hour; int min = timeinfo-> tm_min; int secs = timeinfo-> tm_sec; char * str = (char *) malloc (sizeof (char) * 100); memset (str, 0,100); // multiple values are merged into the string if (seconds> 0) {sprintf (str, shutdown after % d seconds ..., year, month, day, hour, min, secs);} else {sprintf (str, the current time is: % d year % d month % d hour % d minute % d Second, year, month, day, hour, min, secs);} puts (str ); free (str );}