结构体

重要语句

struct date
{
 int mouth;
 int day;
 int year;
}

//结构体

struct date purchase;

//结构体变量

struct date today;
today.day = 25;

//结构体成员赋值

printf("Today's date is %i/%i/%.2i.\n",today.month,today.day,today.year%100);

//%.2i表示显示两个整数位,不足地方用0填充‘.’;

const int daysPerMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

//用于月份转化

struct date today ={4,25,2018};

//结构体赋值

struct date today = {.year = 2018};

//.member = value 给结构体里特定成员赋值

today = (struct date){4,25,2018};
today = (struct date){.month = 4};

//复合字面量方式给结构体赋值

struct date birthdays[10];

//结构体数组

birthdays[1].day = 16;

//结构体数组中结构体[1]的日期成员赋值

struct date birthdays[3] = {{06,16,1994},{07,17,1995},{03,03,1970}};
//struct date birthdays[3] = {06,16,1994,07,17,1995,03,03,1970};
struct date birthdays[3] = {[1] = {06,16,1994}};
struct date birthdays[3] = {[1].day= 16 ,[1],month = 06};

//结构体数组初始化语句

struct dateAndtime
{
    struct date sdate;
    struct time stime;
};

//包含结构体的结构体

struct dateAndtime event;
event.sdate = dateUpdate(event.sdate);
event.sdate.month = 10;
struct dateAndtime event = {{2018,4,25},{16,16,52}};
//2018年4月25日,16点16分52秒
struct dateAndtime event = {{.month=4}{.minutes = 16,.second = 52 }};
struct dateAndtime events[100];
evevts[0].stime.hour = 16;

//包含结构体的结构体赋值方法

strcut month
{
   int numberOfDays;
   char name[3];
};

//包含数组的结构体

struct month aMonth;
aMonth.numberOfaDays =31;
aMonth.name[0]='J';
aMonth.name[1]='a';
aMonth.name[2]='n';
struct month aMonth = {31,{'J','a','n'}};
struct month aMonth[12];
//包含数组的结构体的数组

//包含数组的结构体赋值语句

struct date
{
   int mouth;
   int day;
   int year;
}todaysDate,birthday;
struct date
{
   int mouth;
   int day;
   int year;
}todaysDate = {1112011};
struct date
{
   int mouth;
   int day;
   int year;
}Date[100];

//定义结构体同时声明变量加赋值

经典程序(确定明天的日期)


#include<stdio.h>
#include<stdbool.h>

struct date 
{
    int month;
    int day;
    int year;
};

/*此函数计算明天的日期
输入参数struct date 返回参数struct date*/
struct date  dateUpdate(struct date today)
{
    struct date tomorrow;
    int numberOfDays(struct date d);
    if (today.day != numberOfDays(today)) 
    {
        tomorrow.day = today.day + 1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
    }
    else if (today.month == 12) 
    {
        tomorrow.day = 1;
        tomorrow.month = 1;
        tomorrow.year = today.year + 1;
    }
    else 
    {
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }
    return tomorrow;
};
/*此函数计算一个月份的天数
输入参数struct date 返回参数 int numberOfDays*/
int numberOfDays(struct date d) 
{
    int days;
    bool isLeapYear(struct date d);
    const int daysPerMonth[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (isLeapYear(d) && d.month == 2)
        days = 29;
    else
        days = daysPerMonth[d.month];
    return days;
}
/*此函数判断是否为闰年
输入参数 struct date,返回参数 bool leapYearFlag*/
bool isLeapYear(struct date d) 
{
    bool leapYearFlag;
    if ( (d.year % 4 == 0 && d.year & 100 != 0) || d.year % 400 == 0)
        leapYearFlag = true;
    else
        leapYearFlag = false;
    return
        leapYearFlag;
}
/*主函数*/
int main(void)
{
    struct date  dateUpdate(struct date today);
    struct date thisday, nextday;
    printf("Enter today's date(mm dd yyyy):");
    scanf_s("%i%i%i", &thisday.month, &thisday.day, &thisday.year);
    nextday = dateUpdate(thisday);
    printf("Tomorrow's date is %i%i%i\n", nextday.month, nextday.day, nextday.year);
    //ptintf("Tomorrow's date is %i%i%i\n", nextday.month, nextday.day, nextday.year);
    system("pause");
    return 0;
}

纠错:之前printf拼写错误导致vs2017编译出问题,显示LNK1120和LNK2019两个错误。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值