博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
枚举类型的应用
阅读量:3897 次
发布时间:2019-05-23

本文共 965 字,大约阅读时间需要 3 分钟。

1.利用枚举类型定义函数

   

typedef enum state{    

    ERROR,

    OK,

    DEFOUT

}Staute;

 

Staute parity_char(int num);

int main()

{

    int num;

    int mycount;

    printf("Please enter a num:");

    scanf("%d",&num);

    mycount = parity_char(num);

    if(mycount == OK)

    {

        printf("success\n");

    }

    if(mycount == ERROR)

    {

        printf("failed\n");

    }

 

    return 0;

}

Staute parity_char(int num)

{

    if(num == 0)

    {

        return OK;

    }

    else{

        return ERROR;

    }   

}

2.利用枚举类型作为函数形参

int test(Status m)

{

 switch(m)

case ERROR:

.....

break;

case OK:

.....

break;

}

int main(void)

{

Status w;

test(w);

}

在stm32库中经常有结构体和枚举类型的联合应用

typedef enum

{ GPIO_Mode_AIN = 0x0,
  GPIO_Mode_IN_FLOATING = 0x04,
  GPIO_Mode_IPD = 0x28,
  GPIO_Mode_IPU = 0x48,
  GPIO_Mode_Out_OD = 0x14,
  GPIO_Mode_Out_PP = 0x10,
  GPIO_Mode_AF_OD = 0x1C,
  GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;

typedef struct

{
  uint16_t GPIO_Pin;              
  GPIOMode_TypeDef GPIO_Mode;    
}GPIO_InitTypeDef;

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
/*后面是对GPIO_Mode的一系列操作,使单片机执行指令*/
 

转载地址:http://seuen.baihongyu.com/

你可能感兴趣的文章
【C++基础】const成员函数
查看>>
【设计模式基础】行为模式 - 5 - 策略(Strategy)
查看>>
【Maven】Archetype
查看>>
【Java.Web】Cookie —— 基础
查看>>
【Tools.Eclipse】代码自动提示
查看>>
【Java.Web】MVC —— Model1 V.S. Model2
查看>>
【Java.Web】MVC —— 基于Servlet Controller的Model2 —— 示例
查看>>
【Java.Web】MVC —— 基于Filter Dispatcher的Model2 —— 示例
查看>>
【Java.Web】MVC —— Action的验证器 —— Validator
查看>>
【Java.Spring.MVC】使用Spring MVC构建Web应用程序
查看>>
【DB.PL/SQL】程序流程控制 —— 异常处理
查看>>
【Java.IO】I/O 【字节】【处理流】 - 之 - 【压缩流】 - ZipInputStream,ZipOutputStream
查看>>
【Java.JDBC/ORM】纯JDBC系统的开发随想
查看>>
【Unix/Linux】【系统】环境变量
查看>>
【Architecture】CPU-bound(计算密集型) 和I/O bound(I/O密集型)
查看>>
【MacOS】Mac 系统下类似于 apt-get 的软件包管理器 -- Homebrew
查看>>
为窗口添加鼠标HOVER和LEAVE事件
查看>>
VC小技巧20个
查看>>
MFC Feature Pack for Visual C++ 2008的BUG之一
查看>>
POJ - 2739 Sum of Consecutive Prime Numbers
查看>>