iOS OC C

枚举、宏

920 发布: 2015/10/26 05:55 本文总阅读量

枚举、宏

#pragma mark 1 创建普通的枚举类型
#include 
int enumOne()
{
    enum Season {spring=1,summer,autumn,winter};
    enum Season s = summer;//或者:s=2;
    return s;
}
#pragma mark 2 模拟ios中label的文本对齐方式
void textAligment()
{
    enum NSTextAligment {NSTextAligmentLeft,NSTextAligmentCenter,NSTextAligmentRight} al=NSTextAligmentRight;
    struct UILabel {
        float frame;
        enum NSTextAligment aligment;
    };
    struct UILabel label1={100.0,1};
    struct UILabel label2={200.0,al};
    struct UILabel label3={130.0,1};
    printf("\n%d-%d-%d",label1.aligment,label2.aligment,label3.aligment);
}
#pragma mark 宏替换
void typedefOne()
{
    typedef long int Integer;//给数据类型long int起一个别名
    typedef unsigned long int UInteger;//给数据类型unsigned long int起一个别名
#define Integer1 int//原封不动的替换:Integer1替换int
    Integer a;
    Integer1 a1;
   
    typedef char* String;
#define String1 char *//原封不动的替换
   
    String name,address;        //char *name,char *address
    String1 name1,address1;      //char *name1,char address1;
}
#pragma mark 宏替换结构体
void typedefTwo()
{
   
    typedef struct
         {
          int age;
          float height;
         } AAA;
    AAA xiaoli={23,176.5};
    AAA xiaozhang;
}
#pragma mark 宏替换枚举
void typedefThree()
{
    typedef enum{spring=1,summer,autumn,winter} Season;
    Season s1=summer;
    Season s2=winter;
    printf(" %d %d",s1,s2);//结果  2   4;
}
int main(int argc, const char * argv[]) {
    textAligment();
    //枚举
    int season=enumOne();
    switch (season) {
        case 1:
            printf("spring");
            break;
           
        case 2:
            printf("summer");
            break;
           
        case 3:
            printf("autumn");
            break;
           
        case 4:
            printf("winter");
            break;
           
        default:
            break;
    }
    return 0;
}