<strong><span style= "FONT-SIZE:14PX;" >c language- timed shutdown program </span></strong>
<strong></strong><span style= "FONT-SIZE:14PX;" ></span>
<strong><span style= "FONT-SIZE:14PX;" > Special Attention:</span></strong>
<strong><span style= "FONT-SIZE:14PX;" > 1. Converting from a received char * type to an int must not be converted using (int) This way, with atoi () </span></strong>
<strong><span style= "FONT-SIZE:14PX;" > How to use: Prototype: int atoi (const char *nptr);</span></strong>
<strong><span style= "FONT-SIZE:14PX;" >2. Multiple string merges, using sprintf () </span></strong>
<strong><span style= "FONT-SIZE:14PX;" > How to use: sprintf (char *buffer,const char * format[,argument,...]) </span></strong>
<strong><span style= "FONT-SIZE:14PX;" > below are used </span></strong>
<strong><span style= "FONT-SIZE:14PX;" ></span></strong>
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < time.h> int Isnumber (char *a); void getcurrentdate (int seconds); int main () {printf ("Welcome to the timed shutdown system! \ n "); getcurrentdate (0); Char str[2]; Char flag = 1;while (flag) {printf ("Input a timed off, enter C to cancel timer shutdown \ n"), gets (str), if (strcmp (str, "a") ==0) {printf ("Please enter the number of seconds in time: \ n"); Char Seconds[10];while (1) {gets (seconds); if (Isnumber (char*) seconds) break;else{printf ("Input number of seconds is incorrect, please re-enter!") \ n "); 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");p rintf ("Timed shutdown is canceled \ n"); system ("pause"); ;} else{printf ("Input value is incorrect, please re-enter!\n"); flag=1;}} return 0;} Determines whether the string is a numeric int isnumber (char *a) {char Flag=1;int len = strlen (a); for (int i=0;i<len;i++) {if (IsDigit (A[i]) ==0) {flag = 0;break;}} if (flag) return 1;elsereturn 0;} Get system Current time void Getcurrentdate (int seconds) {time_t rawtime;struct TM *timeinfo,*timeinfo2;time (&rawtime); if (seconds > 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 combined string if (seconds > 0) {sprintf (str, "%d%d months%d%d", "after%d%d seconds", Year,month,day,hour,min, secs); }else{sprintf (str, "Now is the time:%d%d months%d days%d-%d seconds", year,month,day,hour,min,secs); } puts (str); free (str);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C Language-timed shutdown program