C/C++学习笔记4 结构体联合体枚举
2017-11-07 16:08
393 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 图形绘制隐形的架构
很多种类型的图元,我要绘制它们。
*/
// 图元的类型
enum SHAPE_TYPE {
SHAPE_TRIANGLE
= 0,
SHAPE_RECT
= 1,
SHAPE_CIRCLE
= 2,
SHAPE_NUM
};
struct point {
float x;
float y;
};
struct shape {
//
int type;
enum SHAPE_TYPE type;
union {
struct {
struct point t0;
struct point t1;
struct point t2;
}triangle;
struct {
float x;
float y;
float w;
float h;
}rect;
struct {
float c_x;
float c_y;
float r;
}circle;
}u;
};
void init_circle_shape(struct shape*
s,
float c_x,
float c_y,
float r)
{
s->type
= SHAPE_CIRCLE;
s->u.circle.c_x
= c_x;
s->u.circle.c_y
= c_y;
s->u.circle.r
= r;
}
void init_rect_shape(struct shape*
s,
float x,
float y,
float w,
float h)
{
s->type
= SHAPE_RECT;
s->u.rect.x
= x;
s->u.rect.y
= y;
s->u.rect.w
= w;
s->u.rect.h
= h;
}
void draw_shapes(struct shape*
shapes,
int count)
{
int i;
for (i
= 0; i < count; i++) {
switch (shapes[i].type)
{
case SHAPE_CIRCLE:{
printf("circle
%f, %f, %f\n",
shapes[i].u.circle.c_x,
shapes[i].u.circle.c_y,
shapes[i].u.circle.r);
}
break;
case SHAPE_TRIANGLE:
{
printf("triangle
%f, %f, %f, %f, %f, %f\n",
shapes[i].u.triangle.t0.x,
shapes[i].u.triangle.t0.y,
shapes[i].u.triangle.t1.x,
shapes[i].u.triangle.t1.y,
shapes[i].u.triangle.t2.x,
shapes[i].u.triangle.t2.y);
}
break;
case SHAPE_RECT:
{
printf("rect
%f, %f, %f, %f\n",
shapes[i].u.rect.x,
shapes[i].u.rect.y,
shapes[i].u.rect.w,
shapes[i].u.rect.h);
}
break;
default:
{
printf("Warning
Not a valid shape !!!!\n");
}
break;
}
}
}
void init_triangle_shape(struct shape*
s,
float x0,
float y0,
float x1,
float y1,
float x2,
float y2)
{
s->type
= SHAPE_TRIANGLE;
s->u.triangle.t0.x
= x0;
s->u.triangle.t0.y
= y0;
s->u.triangle.t1.x
= x1;
s->u.triangle.t1.y
= y1;
s->u.triangle.t2.x
= x2;
s->u.triangle.t2.y
= y2;
}
int main(int argc,
char**
argv) {
struct shape shapes_set[3];
init_triangle_shape(&shapes_set[0],
100, 100, 200, 200, 300, 300);
init_rect_shape(&shapes_set[1],
100, 100, 200, 200);
init_circle_shape(&shapes_set[2],
100, 100, 200);
draw_shapes(shapes_set,
3);
system("pause");
return 0;
}
上面案例就是使用结构体 。联合体 ,枚举的一个综合案例,可以使用一个结构体 就能做3个功能,
struct person{
char name[64];
int age;
int sex;
};
struct person p; //这样声明可以通过 p.name,p.age,p.sex来访问和赋值
struct person* p; //声明指针类型的,可以通过p->name,p->age,p->sex来访问和赋值
枚举:
如果没有指定枚举值 ,默认第一个从0开始 ,下一个是上一个加1
enum WEAPON_TYPE{
JIN_TYPE,
MU_TYPE,
SHUI_TYPE=100,
HUO_TYPE,
TU_TYPE,
};
JIN_TYPE = 0
MU_TYPE = 1
SHUI_TYPE=100 //指定了值
HUO_TYPE = 101
TU_TYPE = 102
#include <string.h>
#include <stdlib.h>
/* 图形绘制隐形的架构
很多种类型的图元,我要绘制它们。
*/
// 图元的类型
enum SHAPE_TYPE {
SHAPE_TRIANGLE
= 0,
SHAPE_RECT
= 1,
SHAPE_CIRCLE
= 2,
SHAPE_NUM
};
struct point {
float x;
float y;
};
struct shape {
//
int type;
enum SHAPE_TYPE type;
union {
struct {
struct point t0;
struct point t1;
struct point t2;
}triangle;
struct {
float x;
float y;
float w;
float h;
}rect;
struct {
float c_x;
float c_y;
float r;
}circle;
}u;
};
void init_circle_shape(struct shape*
s,
float c_x,
float c_y,
float r)
{
s->type
= SHAPE_CIRCLE;
s->u.circle.c_x
= c_x;
s->u.circle.c_y
= c_y;
s->u.circle.r
= r;
}
void init_rect_shape(struct shape*
s,
float x,
float y,
float w,
float h)
{
s->type
= SHAPE_RECT;
s->u.rect.x
= x;
s->u.rect.y
= y;
s->u.rect.w
= w;
s->u.rect.h
= h;
}
void draw_shapes(struct shape*
shapes,
int count)
{
int i;
for (i
= 0; i < count; i++) {
switch (shapes[i].type)
{
case SHAPE_CIRCLE:{
printf("circle
%f, %f, %f\n",
shapes[i].u.circle.c_x,
shapes[i].u.circle.c_y,
shapes[i].u.circle.r);
}
break;
case SHAPE_TRIANGLE:
{
printf("triangle
%f, %f, %f, %f, %f, %f\n",
shapes[i].u.triangle.t0.x,
shapes[i].u.triangle.t0.y,
shapes[i].u.triangle.t1.x,
shapes[i].u.triangle.t1.y,
shapes[i].u.triangle.t2.x,
shapes[i].u.triangle.t2.y);
}
break;
case SHAPE_RECT:
{
printf("rect
%f, %f, %f, %f\n",
shapes[i].u.rect.x,
shapes[i].u.rect.y,
shapes[i].u.rect.w,
shapes[i].u.rect.h);
}
break;
default:
{
printf("Warning
Not a valid shape !!!!\n");
}
break;
}
}
}
void init_triangle_shape(struct shape*
s,
float x0,
float y0,
float x1,
float y1,
float x2,
float y2)
{
s->type
= SHAPE_TRIANGLE;
s->u.triangle.t0.x
= x0;
s->u.triangle.t0.y
= y0;
s->u.triangle.t1.x
= x1;
s->u.triangle.t1.y
= y1;
s->u.triangle.t2.x
= x2;
s->u.triangle.t2.y
= y2;
}
int main(int argc,
char**
argv) {
struct shape shapes_set[3];
init_triangle_shape(&shapes_set[0],
100, 100, 200, 200, 300, 300);
init_rect_shape(&shapes_set[1],
100, 100, 200, 200);
init_circle_shape(&shapes_set[2],
100, 100, 200);
draw_shapes(shapes_set,
3);
system("pause");
return 0;
}
上面案例就是使用结构体 。联合体 ,枚举的一个综合案例,可以使用一个结构体 就能做3个功能,
struct person{
char name[64];
int age;
int sex;
};
struct person p; //这样声明可以通过 p.name,p.age,p.sex来访问和赋值
struct person* p; //声明指针类型的,可以通过p->name,p->age,p->sex来访问和赋值
枚举:
如果没有指定枚举值 ,默认第一个从0开始 ,下一个是上一个加1
enum WEAPON_TYPE{
JIN_TYPE,
MU_TYPE,
SHUI_TYPE=100,
HUO_TYPE,
TU_TYPE,
};
JIN_TYPE = 0
MU_TYPE = 1
SHUI_TYPE=100 //指定了值
HUO_TYPE = 101
TU_TYPE = 102
相关文章推荐
- C语言学习笔记10-结构体、枚举、联合体
- (c/c++学习笔记三) 结构体与联合体的字节对齐
- 新手学习C++笔记7:结构体、联合体的区别与联系
- C#学习之路,学习笔记 第五章 枚举、结构体和数组
- C++学习笔记之作用域为类的常量和作用域内的枚举
- C++-结构体,联合体,枚举,的区别
- iOS学习笔记44-Swift(四)枚举和结构体
- 学习笔记-C/C++-结构体与sizeof,内存对齐的题目怎么做
- swift学习笔记3——类、结构体、枚举
- iOS学习笔记44-Swift(四)枚举和结构体
- C笔记系列五:结构体、联合体、枚举
- 【算法学习笔记】04.C++中结构体定义练习(bign初步)
- C/C++ 学习笔记:结构体中最后一个成员为[0]或[1]长度数组(柔性数组成员)的用法
- day01 C++名字空间、结构体、联合体、枚举 、字符串
- 【算法学习笔记】04.C++中结构体定义练习(bign初步)
- JNI学习笔记 C++传递结构体、String、数组对象给JavaC++传递结构体、String、数组对象给Java
- C++学习笔记 6th —— 结构体
- C++ 学习笔记(19)new/delete表达式、定位new、typeid、dynamic_cast、type_info、枚举类型、成员函数指针、union、位域、volatile限定符、链接指示
- C++学习笔记(第七章 自定义数据类型 结构体应用 之二)
- C++学习笔记(二) bool const 和 #define 结构体内存对齐