结构体的相关笔记

枚举

在这里插入图片描述

# include <stdio.h>

enum color {red,yellow,green};


int main(void)
{
	printf("code for red is %d\n",red);
	printf("code for yellow is %d\n",yellow);
	printf("code for green is %d\n",green);
	
	
	return 0;
}     

现象:
在这里插入图片描述
在这里插入图片描述

声明结构类型

在这里插入图片描述

# include <stdio.h>

struct date {  //声明一个结构体
	int month;
	int day;
	int year;
}today;


int main(void)
{
//	struct date today;//定义一个变量
	today.month = 07;
	today.day = 31;
	today.year =2020;
	
	printf("today's date is %i-%i-%i.\n",today.year,
			today.month,today.day);
	
	
	return 0;
}               
 


结构成员

在这里插入图片描述

结构运算

在这里插入图片描述

结构指针

在这里插入图片描述

/*
	计算明天日期 
*/ 

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

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

bool isleap ( struct date d ){//是闰年返回true
	bool leap = false;
	if ((d.year%4 == 0 && d.year%100 != 0) || d.year%100 == 0)
		leap = true;
	return leap;             
}

int daysofmonth ( struct date d ){//判断每月天数
	int days;
	const int dayspermonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	if ( d.month == 2 && isleap(d) )
		days = 29;
	else
		days = dayspermonth[d.month-1];
	return days;    //注意函数的返回值不能丢
}
int main ( int argc, char const *argv[])
{
	struct date today, tomorrow;
	printf("请输入今天的日期(XXXX XX XX):\n");
	scanf("%d %d %d",&today.year,&today.month,&today.day);
	//注意%d用什么隔开,输入时就用什么隔开
	if( today.day<1 || today.day>31 || today.month<1 ||today.month>12 ){
		printf("输入有误!!\n");
		return 0; 	
	}
	if( today.day != daysofmonth(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;
	}
	printf( "明天的日期是%d-%d-%d",tomorrow.year,tomorrow.month,tomorrow.day);
	
	return 0;
}


/* 

#include<stdio.h>
int main(){
	int year,mouth,day;
	int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
	scanf("%d %d %d",&year,&mouth,&day);
	putchar(10); //换行 
	int i;
	for(i=0;i<1;i++){
		day++;
		if(abc(year)&&day>a[mouth]){
			day=1;
			mouth++;
		}
		if(!abc(year)&&day>b[mouth]){
			day=1;
			mouth++;
		}
		if(mouth>12){
			year++;
			mouth=1;
		}

	}
	
printf("%d %d %d\n",year,mouth,day);
	return 0;
}

int abc(int a){
	if(a%4 == 0 && a%100 !=0 || a%400 ==0)
		return 0;
	return 1;
}

*/

指向结构的指针

在这里插入图片描述

嵌套的结构

在这里插入图片描述

自定义数据类型(typedef)

在这里插入图片描述

联合

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值