1.如果用户输入一个参数,则打印”no args",如果输入两个参数,并且第二个命令行参数是-a,则打印“I will deal with -a",如果是-l,则打印"I will deal with -l"
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
if(argc==1)
printf("no args \n");
if(argc==2)
{
if(strcmp(argv[1],"-a")==0)
printf("I will deal with -a \n");
if(strcmp(argv[1],"-l")==0)
printf("I will deal with -l \n");
}
}
2.获取当前系统的时间,并按命令date的方式显示方式显示出来
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
time_t t;
struct tm *p_tm;
time(&t);
p_tm=localtime(&t);
printf("%d年%d月%d日%d时%d分%d秒\n",
p_tm->tm_year+1900,
p_tm->tm_mon+1,
p_tm->tm_mday,
p_tm->tm_hour,
p_tm->tm_min,
p_tm->tm_sec);
}
3.由用户输入年、月、日、时、分、秒,并将该时间设置为系统时间
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
char smallTips[]="请输入年月日时分秒共计6个参数";
int main(int argc,char *argv[])
{
if(argc==7)
{
time_t t;
struct tm *p_tm;
time(&t);
p_tm=localtime(&t);
int year=strtol(argv[1],NULL,10);
int mon=strtol(argv[2],NULL,10);
int day=strtol(argv[3],NULL,10);
int hour=strtol(argv[4],NULL,10);
int min=strtol(argv[5],NULL,10);
int sec=strtol(argv[6],NULL,10);
printf("%d %d %d %d %d %d",year,mon,day,hour,min,sec);
p_tm->tm_year=year-1900;
p_tm->tm_mon=mon-1;
p_tm->tm_mday=day;
p_tm->tm_hour=hour;
p_tm->tm_min=min;
p_tm->tm_sec=sec;
t=mktime(p_tm);
stime(&t);
}else
{
printf("%s\n",smallTips);
}
return 0;
}
return 0;
}