标签:c语言
<strong><span style="font-size:14px;">C语言 -- 定时关机程序</span></strong>
<strong></strong><span style="font-size:14px;"></span>
<strong><span style="font-size:14px;">特别注意:</span></strong>
<strong><span style="font-size:14px;"> 1.从接收到的char * 类型转换为 int ,一定不能使用(int)这种方式来转换,要用atoi()</span></strong>
<strong><span style="font-size:14px;"> 使用方式:原型: int atoi(const char *nptr);</span></strong>
<strong><span style="font-size:14px;">2. 多个字符串合并,使用sprintf()</span></strong>
<strong><span style="font-size:14px;"> 使用方式:sprintf(char *buffer,const char * format[,argument,...])</span></strong>
<strong><span style="font-size:14px;"> 以下有使用方式</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("欢迎进入定时关机系统!\n"); getCurrentDate(0); char str[2]; char flag = 1; while(flag){ printf("输入A定时关机,输入C取消定时关机\n"); gets(str); if(strcmp(str,"a")==0) { printf("请输入时间的秒数:\n"); char seconds[10]; while(1){ gets(seconds); if(isNumber((char*)seconds)) break; else{ printf("输入的秒数不正确,请重新给输入!\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"); printf("已经取消定时关机\n"); system("pause"); break; }else{ printf("输入的值不正确,请重新输入!\n"); flag=1; } } return 0; } //判断字符串是否是数字 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; else return 0; } //获取系统当前时间 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); //多个值合并字符串 if(seconds > 0){ sprintf(str,"在%d年%d月%d日 %d时%d分%d秒后关机...",year,month,day,hour,min,secs); }else{ sprintf(str,"现在时间是:%d年%d月%d日 %d时%d分%d秒 ",year,month,day,hour,min,secs); } puts(str); free(str); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:c语言
原文地址:http://blog.csdn.net/u013538542/article/details/47210489