struct union

来源:互联网 发布:剑三眼睛捏脸数据 编辑:程序博客网 时间:2024/06/05 08:03

#include<stdio.h>
#include<stdlib.h>

enum kk{NO,THIN,NORMAL,FAT};
union BED
{
    char thin;
    int normal;
    double fat;
};
struct ROOM
{
    int num;
    int type;
    union BED bed;
};
struct HOTEL
{
    struct ROOM rooms[10];
};
int main()
{
    struct HOTEL hotel;
    srand(time(NULL));
    int i;
    for(i=0;i<10;i++)
    {
        switch(rand()%4)
        {
            case NO:
                hotel.rooms[i].type=NO;break;
            case THIN:
                hotel.rooms[i].type=THIN;
                hotel.rooms[i].bed.thin='x';break;
            case NORMAL:
                hotel.rooms[i].type=NORMAL;
                hotel.rooms[i].bed.normal=123456;break;
            case FAT:
                hotel.rooms[i].type=FAT;
                hotel.rooms[i].bed.fat=999.9999;break;
            default:
                break;
        }
    }
    for(i=0;i<10;i++)
    {
        switch(hotel.rooms[i].type)
        {
            case NO:
                printf("%d room meiyouren\n",i);break;
            case THIN:
                printf("%d room %c thin \n",i,hotel.rooms[i].bed.thin);break;
            case NORMAL:
                printf("%d room %d normal \n",i,hotel.rooms[i].bed.normal);break;
            case FAT:
                printf("%d room %lf fat \n",i,hotel.rooms[i].bed.fat);break;
            default:
                break;
        }
    }
    return 0;
}

 

0 0
原创粉丝点击