打印
[DemoCode下载]

枚举与结构体混合使用的学习笔记

[复制链接]
414|8
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
稳稳の幸福|  楼主 | 2020-11-22 19:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stdio.h>

enum Week{ MON=1, TUE, WED, THU, FRI, SAT, SUN};
enum Month{January=1,February,March,April,May,June,July,August,September,October,November,December};
struct Time
{
        unsigned        int                year;
        enum                Month        month;
        unsigned        char        day;
        enum                Week        week;
        unsigned        char        hour;
        unsigned        char        min;
        unsigned        char        sec;       
};

void delay(unsigned int x)
{
        while(x)
                {
                        x--;
                        unsigned int a,b;
                        for(a=0;a<20000;a++)
                                for(b=0;b<20000;b++);
                }       
}
struct Time time;

void time_update(void)
{
        if(++time.sec==60)
                {
                        time.sec=0;
                        if(++time.min==60)
                        {
                                time.min=0;
                                if(++time.hour==24)
                                time.hour=0;
                        }
                }       
}

void time_display(void)
{
       
}

int main(void)
{
        int index=0;
       
        time.year=2020;
        time.month=November;
        time.day=3;
        time.hour=23;
        time.min=40;
        time.sec=10;



        printf("index:");
        while(1)
                {
                        printf("%4d\b\b\b\b",index++);
                        time_update();
                        time_display();
                        delay(10000);

                }       
        return 0;
}


使用特权

评论回复
沙发
稳稳の幸福|  楼主 | 2020-11-22 19:45 | 只看该作者
之前发过一个枚举的学习笔记,这次把上次学习笔记的深入版本发出来
首先枚举是干什么的大家要知道。就是将变量的取值范围固定到某个整数集里,同时增加程序的可读性。
然后作为一个变量元素,还可以作为结构体的成员元素。

使用特权

评论回复
板凳
稳稳の幸福|  楼主 | 2020-11-22 20:24 | 只看该作者
这个例子就是定义了星期7天的枚举变量,与12个月份的枚举变量,然后作为一个时间结构体的部分成员。
该例子演示了如何将枚举类型的变量作为结构体的一个成员。

使用特权

评论回复
地板
稳稳の幸福|  楼主 | 2020-11-22 20:49 | 只看该作者
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
enum Week{ MON=1, TUE, WED, THU, FRI, SAT, SUN};
enum Month{January=1,February,March,April,May,June,July,August,September,October,November,December};
struct Time
{
        unsigned        int                year;
        enum                Month        month;
        unsigned        char        day;
        enum                Week        week;
        unsigned        char        hour;
        unsigned        char        min;
        unsigned        char        sec;       
};

void delay(unsigned int x)
{
        while(x)
                {
                        x--;
                        unsigned int a,b;
                        for(a=0;a<20000;a++)
                                for(b=0;b<20000;b++);
                }       
}
struct Time time;

void time_update(void)
{
        if(++time.sec==60)
                {
                        time.sec=0;
                        if(++time.min==60)
                        {
                                time.min=0;
                                if(++time.hour==24)
                                time.hour=0;
                        }
                }       
}

void time_display(void)
{
         printf("%4d年%2d月%2d日\n",time.year,time.month,time.day);
        if(time.week==7)
                printf("星期日\n");
        else printf("星期%d\n",time.week);
         printf("%02d:%02d:%02d\n",time.hour,time.min,time.sec);
        system("cls");
}

int main(void)
{
       
        time.year=2020;
        time.month=November;
        time.day=22;
        time.week=SUN;
        time.hour=23;
        time.min=40;
        time.sec=10;



        printf("index:");
        while(1)
                {

                        time_update();
                        time_display();
                        delay(10000);

                }       
        return 0;
}

使用特权

评论回复
5
x史可法| | 2020-11-22 22:25 | 只看该作者
比较典型的例子用星期

使用特权

评论回复
6
正圆椭圆| | 2020-11-22 22:38 | 只看该作者
枚举占内存吗

使用特权

评论回复
7
329547875| | 2020-11-23 09:11 | 只看该作者

使用特权

评论回复
8
cr315| | 2020-11-23 09:26 | 只看该作者

同问:枚举占内存吗

使用特权

评论回复
9
elephant00| | 2020-11-24 10:00 | 只看该作者
不错,支持

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

162

主题

3098

帖子

8

粉丝