Date related questions (questions after n days)

Date of current computer system

#include<stdio.h>
#include<time.h>
int main(void){
	time_t now,t1;
	
	t1=time(&now);
	printf("Seconds from 0:00 on January 1, 1970, current calendar time:%u\n",t1); 
	
	struct tm *localtp;
	localtp=localtime(&now);
	printf("Now the time is:%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;
} 
Seconds from 0:00 on January 1, 1970, current calendar time: 1583886788
 Now it's 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 days later

Given a date and any positive integer n, find the date after n days

note

#include<stdio.h>

struct Date
{
	int year,month,day;
};
typedef  struct Date Date;//Define synonyms for struct Date
int IsLeap(int y);//Determine whether it is a leap year, return 1 if yes, return 0 if no
void NextDay(Date *pd);//Change the date referred to in pd to the next date 
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}};
//There are 366 days in leap year (31 days, 29 days, 31 days, 30 days, 31 days, 30 days, 31 days, 31 days, 30 days, 31 days, 31 days, 31 days, 31 days, respectively).
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);//If the integer is less than 2 columns, add 0
	
	return 0;
}
//The calculation method of leap year in Gregorian calendar: the ordinary year which can be divided by 4 and cannot be divided by 100 is leap year; the century year which can be divided by 400 is leap year;
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{//If it is the end of the year, the next day is January 1 of the next year 
		pd->year++;
		pd->month=1;
		pd->day=1; 
	}
}
2019-2-25 5
2019-03-02

Imitation writing

Variables defined outside of all functions are called global variables. Their scope is the whole program by default, that is, all source files, including. c and. h files
ps: in C language, the return statement can only return one value from a function every time it is called. But in many practical applications, we need to return multiple values from the function. Method 1: set global variables. Method 2: use array name or pointer as parameter of function. Method 3: use structure pointer as parameter of function.
ps: if we use more global variables, it will damage the security and structure of the code. This is mainly because global variables can be used in all functions, so the change of their values is uncertain, so we should use them with caution.

#include<stdio.h>
void Getday(int y,int m,int d);
int year,mon,day;//Set global variables 
int main(void){
	
	int n;
	printf("Please enter the start time:");
	scanf("%d%d%d",&year,&mon,&day);
	printf("Please input n Value:");
	scanf("%d",&n);
	for(int i;i<=n;i++){
	    Getday(year,mon,day);
	    printf("%d Next time is%d-%d-%d\n",i,year,mon,day);
    }
	printf("output n After the day:%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;
	
}

The following is the wrong answer (input time format is wrong, resulting in wrong result; and i=1, not 0)

Please enter the start time: February 23, 2019
 Please enter the value of n: 5
-22
 0 time is 2019-2-22
-21
 One is 2019-2-21
-20
 2 times 2019-2-20
-19
 Three times is 2019-2-19
-18
 4 times 2019-2-18
-17
 5 times 2019-2-17
 After n days of output: 2019-2-17
--------------------------------
Process exited after 13.13 seconds with return value 0
 Please press any key to continue

After modification: the following is the correct answer

Please enter the start time: 2019 2 23
 Please enter the value of n: 5
24
 One is February 24, 2019
25
 2 times, 2019-2-25
26
 3 times 2019-2-26
27
 4 times 2019-2-27
28
 5 times 2019-2-28
 After n days of output: February 28, 2019
--------------------------------
Process exited after 6.709 seconds with return value 0
 Please press any key to continue

PS: structure
struct tag {
member-list
member-list
member-list
...
} variable-list ;

Tag is the structure tag.
Member list is a standard variable definition, such as int i; float f, or other valid variable definitions.
Variable list structure variable, defined at the end of the structure, before the last semicolon, you can specify one or more structure variables.

//You can also create new types with typedef
typedef struct
{
int a;
char b;
double c;
} Simple2;
//Now you can declare a new struct variable with Simple2 as the type
Simple2 u1, u2[20], *u3;

Copy with structure

#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("Please enter the start time:");
	scanf("%d%d%d",&today.year,&today.mon,&today.day);
	printf("Please input n Value:");
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
	    Getday(today.year,today.mon,today.day);
	    printf("%d Next time is%d-%d-%d\n",i,today.year,today.mon,today.day);
    }
	printf("output n After the day:%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;
	
}

Copy with pointer

#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("Please enter the start time:");
	scanf("%d%d%d",&today.year,&today.mon,&today.day);
	printf("Please input n Value:");
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
	    Getday(&today);
	    printf("%d Next time is%d-%d-%d\n",i,today.year,today.mon,today.day);
    }
	printf("output n After the day:%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);
	}
}

Use the first line of the two-dimensional array monthdays[2][13] to store the days of each month in the normal year. Add the islap function to determine whether it is a leap year. If it is a leap year, then leap=1. So when leap is 1, monthdays [leap] [PD - > mon] finds a month of the leap year.

#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("Please enter the start time:");
	scanf("%d%d%d",&today.year,&today.mon,&today.day);
	printf("Please input n Value:");
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
	    Getday(&today);
	    printf("%d Next time is%d-%d-%d\n",i,today.year,today.mon,today.day);
    }
	printf("output n After the day:%d-%d-%d\n",today.year,today.mon,today.day);
}
int IsLeap(int y)//Leap year return 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);
	}
}

Improvement

When n is big, the efficiency is low

#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("Please enter the start time:");
	scanf("%d%d%d",&today.year,&today.mon,&today.day);
	printf("Please input n Value:");
	scanf("%d",&n);
	
	int sumdays=Days(today);
    Getday(sumdays+n,&today);
    
	printf("output n After the day:%d-%d-%d\n",today.year,today.mon,today.day);
}
int IsLeap(int y)//Leap year return 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 days in leap year 
    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;
}

error: stray '\ 243' in program
1 is there a Chinese character. 2. Whether there is Chinese punctuation. 3. Whether there are full angle English characters (in C, full angle characters are also illegal, such as a writing a. For this point, it should be noted that full space is difficult to find due to the invisibility of space. When there is no explicit error sign, you can try to delete all spaces and recompile.)

Similar exercises

Given the year and n, find the nth day of the year as the number of months

#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("Please input year and 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;
	
}
Published 6 original articles, won praise 0, and visited 368
Private letter follow

Keywords: less C

Added by jcd on Wed, 11 Mar 2020 09:34:49 +0200