当前计算机系统的日期
#include<stdio.h>
#include<time.h>
int main(void){
time_t now,t1;
t1=time(&now);
printf("距离1970年1月1日0时的秒数,当前日历时间:%u\n",t1);
struct tm *localtp;
localtp=localtime(&now);
printf("现在时间是:%dyear%dmonth%dday%d:%d:%d\n",localtp->tm_year+1900,localtp->tm_mon+1,localtp->tm_mday,localtp->tm_hour,localtp->tm_min,localtp->tm_sec);
printf("asctime:%s",asctime(localtp));
printf("ctime:%s",ctime(&now));
struct tm *gmtp;
gmtp=gmtime(&now);
printf("UTF:%s",asctime(gmtp));
return 0;
}
距离1970年1月1日0时的秒数,当前日历时间:1583886788
现在时间是:2020year3month11day8:33:8
asctime:Wed Mar 11 08:33:08 2020
ctime:Wed Mar 11 08:33:08 2020
UTF:Wed Mar 11 00:33:08 2020
n天以后问题
已知一个日期和任意正整数n,求n天以后的日期
笔记
#include<stdio.h>
struct Date
{
int year,month,day;
};
typedef struct Date Date;//将struct Date定义同义词
int IsLeap(int y);//判断是否为闰年,是返回1,否返回0
void NextDay(Date *pd);//将pd所指日期变为下一天日期
int monthdays[2][13]={{0,31,28,31,30,31,30,31,30,31,30,31,30},{0,31,29,31,30,31,30,31,30,31,30,31,30}};
//闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)。
int main(void)
{
Date today;
int n;
scanf("%d-%d-%d %d",&today.year,&today.month,&today.day,&n);
for(int i= 1;i <=n;i++)
NextDay(&today);
printf("%d-%02d-%02d\n",today.year,today.month,today.day);//如果整数不够2列就补上0
return 0;
}
//公历闰年计算方法:普通年能被4整除且不能被100整除的为闰年;世纪年能被400整除的是闰年;
int IsLeap(int y)
{
return y%400==0||y%4==0&&y%100 !=0;
}
void NextDay(Date *pd)
{
int leap =0;
if(IsLeap(pd->year))
leap=1;
if(pd->day<monthdays[leap][pd->month])
pd->day++;
else if(pd->month<12){//
pd->month++;
pd->day=1;
}
else{//如果是年末,下一天为下一年的一月一号
pd->year++;
pd->month=1;
pd->day=1;
}
}
2019-2-25 5
2019-03-02
仿写
在所有函数外部定义的变量称为全局变量(Global Variable),它的作用域默认是整个程序,也就是所有的源文件,包括 .c 和 .h 文件
ps:在C语言中,每调用一次return语句只能从函数中返回一个值。但在实际很多应用中,我们需要从函数中返回多个值。方法一:设置全局变量。方法二:使用数组名或指针作为函数的形参。方法三:使用结构体指针作为函数的形参。
ps:全局变量用多了会破坏代码的安全性,结构性,这主要是全局变量在所有函数中都可以使用,从而其值的变化不确定,所以我们要慎用。
#include<stdio.h>
void Getday(int y,int m,int d);
int year,mon,day;//设置全局变量
int main(void){
int n;
printf("请输入开始的时间:");
scanf("%d%d%d",&year,&mon,&day);
printf("请输入n的值:");
scanf("%d",&n);
for(int i;i<=n;i++){
Getday(year,mon,day);
printf("%d次是%d-%d-%d\n",i,year,mon,day);
}
printf("输出n天以后:%d-%d-%d",year,mon,day);
}
void Getday(int y,int m,int d){
if(d<30)
{ d++;
printf("%d\n",d);
}
else if(m<12)
{ m++;
d=1;
}
else{
y++;
m=1;
d=1;
}
year=y;
mon=m;
day=d;
}
以下为错误答案(输入时间格式错误,导致结果错误;而且i=1,不是0)
请输入开始的时间:2019-2-23
请输入n的值:5
-22
0次是2019--2--22
-21
1次是2019--2--21
-20
2次是2019--2--20
-19
3次是2019--2--19
-18
4次是2019--2--18
-17
5次是2019--2--17
输出n天以后:2019--2--17
--------------------------------
Process exited after 13.13 seconds with return value 0
请按任意键继续. . .
修改后:以下为正确答案
请输入开始的时间:2019 2 23
请输入n的值:5
24
1次是2019-2-24
25
2次是2019-2-25
26
3次是2019-2-26
27
4次是2019-2-27
28
5次是2019-2-28
输出n天以后:2019-2-28
--------------------------------
Process exited after 6.709 seconds with return value 0
请按任意键继续. . .
PS:结构体
struct tag {
member-list
member-list
member-list
…
} variable-list ;
tag 是结构体标签。
member-list 是标准的变量定义,比如 int i; 或者 float f,或者其他有效的变量定义。
variable-list 结构变量,定义在结构的末尾,最后一个分号之前,您可以指定一个或多个结构变量。
//也可以用typedef创建新类型
typedef struct
{
int a;
char b;
double c;
} Simple2;
//现在可以用Simple2作为类型声明新的结构体变量
Simple2 u1, u2[20], *u3;
用结构仿写
#include<stdio.h>
void Getday(int y, int m,int d);
struct Date
{
int year,mon,day;
};
Date today;
typedef struct Date Date;
int main(void){
int n;
printf("请输入开始的时间:");
scanf("%d%d%d",&today.year,&today.mon,&today.day);
printf("请输入n的值:");
scanf("%d",&n);
for(int i=1;i<=n;i++){
Getday(today.year,today.mon,today.day);
printf("%d次是%d-%d-%d\n",i,today.year,today.mon,today.day);
}
printf("输出n天以后:%d-%d-%d\n",today.year,today.mon,today.day);
}
void Getday(int y,int m,int d){
if(d<30)
{ d++;
printf("%d\n",d);
}
else if(m<12)
{ m++;
d=1;
}
else{
y++;
m=1;
d=1;
printf("%d\n",d);
}
today.year=y;
today.mon=m;
today.day=d;
}
用指针仿写
#include<stdio.h>
struct Date
{
int year,mon,day;
};
typedef struct Date Date;
void Getday(Date *pd);
int main(void){
Date today;
int n;
printf("请输入开始的时间:");
scanf("%d%d%d",&today.year,&today.mon,&today.day);
printf("请输入n的值:");
scanf("%d",&n);
for(int i=1;i<=n;i++){
Getday(&today);
printf("%d次是%d-%d-%d\n",i,today.year,today.mon,today.day);
}
printf("输出n天以后:%d-%d-%d\n",today.year,today.mon,today.day);
}
void Getday(Date *pd){
if(pd->day<30)
{ pd->day++;
printf("%d\n",pd->day);
}
else if(pd->mon<12)
{ pd->mon++;
pd->day=1;
}
else{
pd->year++;
pd->mon=1;
pd->day=1;
printf("%d\n",pd->day);
}
}
用二维数组monthdays[2][13]的第一行储存平年的各月份的天数。加入Isleap函数判断是否为闰年,是闰年则leap=1,所以当leap为1时,monthdays[leap][pd->mon]找到闰年的某个月份。
#include<stdio.h>
struct Date
{
int year,mon,day;
};
typedef struct Date Date;
void Getday(Date *pd);
int monthdays[2][13]={{0,31,28,31,30,31,30,31,30,31,30,31,30},{0,31,29,31,30,31,30,31,30,31,30,31,30}};
int IsLead(int y);
int main(void){
Date today;
int n;
printf("请输入开始的时间:");
scanf("%d%d%d",&today.year,&today.mon,&today.day);
printf("请输入n的值:");
scanf("%d",&n);
for(int i=1;i<=n;i++){
Getday(&today);
printf("%d次是%d-%d-%d\n",i,today.year,today.mon,today.day);
}
printf("输出n天以后:%d-%d-%d\n",today.year,today.mon,today.day);
}
int IsLeap(int y)//闰年返回1
{
return y%400==0||y%4==0&&y%100!=0;
}
void Getday(Date *pd){
int leap=0;
if(IsLeap(pd->year))
leap=1;
if(pd->day<monthdays[leap][pd->mon])
{ pd->day++;
//printf("%d\n",pd->day);
}
else if(pd->mon<12)
{ pd->mon++;
pd->day=1;
}
else{
pd->year++;
pd->mon=1;
pd->day=1;
// printf("%d\n",pd->day);
}
}
改进
n大的时候,效率低
#include<stdio.h>
struct Date
{
int year,mon,day;
};
typedef struct Date Date;
int Days(Date dd);
void Getday(int days,Date *pd);
int monthdays[2][13]={{0,31,28,31,30,31,30,31,30,31,30,31,30},{0,31,29,31,30,31,30,31,30,31,30,31,30}};
int IsLead(int y);
int main(void){
Date today;
int n;
printf("请输入开始的时间:");
scanf("%d%d%d",&today.year,&today.mon,&today.day);
printf("请输入n的值:");
scanf("%d",&n);
int sumdays=Days(today);
Getday(sumdays+n,&today);
printf("输出n天以后:%d-%d-%d\n",today.year,today.mon,today.day);
}
int IsLeap(int y)//闰年返回1
{
return y%400==0||y%4==0&&y%100!=0;
}
int Days(Date dd){
int sum=0,leap=0;
if(IsLeap(dd.year))
leap=1;
for(int i=1;i<dd.mon;i++){
sum+=monthdays[leap][i];
}
sum+=dd.day;
return sum;
}
void Getday(int days,Date *pd){
int y,m,yeardays;
y=pd->year;
yeardays=365;
if(IsLeap(y)) yeardays=366;//闰年为366天
while(days>yeardays){
days-=yeardays;
y++;
yeardays=365;
if(IsLeap(y)) yeardays=366;
}
int leap=0;
if(IsLeap(y)) leap=1;
m=1;
while(days>monthdays[leap][m])
{
days-=monthdays[leap][m];
m++;
}
pd->year=y;
pd->mon=m;
pd->day=days;
}
PS:C语言里的变异错误error:stray’\243’in program
1 是否存在汉字。 2是否存在汉语标点。3是否有全角英文字符(在C中,全角字符同样是非法的,比如a写作a。对于这一点,需要注意的是全角的空格,由于空格的不可见性,全角空格比较难查,当无法查到有显式的错误符号时,可以尝试删除所有空格,重新编译)
类似习题
给定year和n,求year年的第n天是几月几号
#include<stdio.h>
int monthdays[2][13]={{0,31,28,31,30,31,30,31,30,31,30,31,30},{0,31,29,31,30,31,30,31,30,31,30,31,30}};
int month,day;
int IsLeap(int y);
void Getday(int y,int days);
int main(void)
{
int year,n;
printf("请输入year和n:");
scanf("%d%d",&year,&n);
IsLeap(year);
Getday(year,n);
printf("%d-%d",month,day);
}
int IsLeap(int y)
{
return y%400==0||y%4==0&&y%100!=0;
}
void Getday(int y,int days){
int leap=0,m,d;
if(IsLeap(y)) leap=1;
for(int i=1;i<=12;i++){
if(days>=monthdays[leap][i])
days -=monthdays[leap][i];
else{
m=i;
d=days;
break;
}
}
month=m;
day=d;
}