所谓的凯撒日期就是输入一个日期输出这一天是这一年中的第几天。
这是今天在论坛帮人写的一个小程序,好久没写c了,写个简单的练练,记录下。
- #include <stdio.h>
- #include <stdbool.h>
- int main(){
- int year; //the year
- int mon; //the month
- int day; //the day
- int dayOfYear = 0; //day of the year
- bool leapYear; //if the year is a leap year??
- printf("Please enter the data(YYYY-MM-DD): ");
- scanf("%d-%d-%d",&year,&mon,&day);
- while((mon < 1 || mon > 12)||(day < 0 || day > 31)){ //check the input
- printf("Enter error!Please enter again!\n\n");
- printf("Please enter the data(YYYY-MM-DD): ");
- scanf("%d-%d-%d",&year,&mon,&day);
- }
- if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)){ //if the year is a leap year,then leapYear=true
- leapYear = true;
- }
- else{
- leapYear = false;
- }
- if(leapYear){ //if the year is a leap year
- if(mon == 1){
- dayOfYear == day;
- }
- else if(mon == 2){
- dayOfYear = 31 + day;
- }
- else if(mon > 2) {
- dayOfYear += 60;
- dayOfYear += day;
- for(int i = 3;i< mon;i++){
- if((i == 1)||(i == 3)||(i == 5)||(i == 7)||(i == 8)||(i == 10)||(i == 12)){
- dayOfYear += 31;
- }
- else if((i == 4)||(i == 6)||(i == 9)||(i == 11)){
- dayOfYear += 30;
- }
- }
- }
- }
- else if(!leapYear){ //if the year is not a leap year
- if(mon == 1){
- dayOfYear == day;
- }
- else if(mon == 2){
- dayOfYear = 31 + day;
- }
- else if(mon > 2) {
- dayOfYear += 59;
- dayOfYear += day;
- for(int i = 3;i< mon;i++){
- if((i == 1)||(i == 3)||(i == 5)||(i == 7)||(i == 8)||(i == 10)||(i == 12)){
- dayOfYear += 31;
- }
- else if((i == 4)||(i == 6)||(i == 9)||(i == 11)){
- dayOfYear += 30;
- }
- }
- }
- }
- printf("The day of the year is: %d\n",dayOfYear);
- }