C语言及程序设计进阶例程-29 枚举类型及其应用

来源:互联网 发布:东华软件 薛向东 编辑:程序博客网 时间:2024/05/17 06:51

贺老师教学链接 C语言及程序设计进阶 本课讲解

He先生方案一:用整型表示品牌、颜色

#include <stdio.h>int main( ){    int brand,color;    //brand=0,1,2分别表示Lavida、Tiggo和Skoda    //color=0,1,2分别表示红黑白    for(color=0; color<3; color++)        for(brand=0; brand<3; brand++)            if(!((color==1&&brand==1)||(color==2&&brand==2)))            {                switch(color)                {                case 0:                    printf("红");                    break;                case 1:                    printf("黑");                    break;                case 2:                    printf("白");                    break;                }                switch(brand)                {                case 0:                    printf("Lavida\n");                    break;                case 1:                    printf("Tiggo\n");                    break;                case 2:                    printf("Skoda\n");                    break;                }            }    return 0;}//不要红Tiggo,但条件明显错了,成了不要黑Tiggo,这种错误很容易造成,不容易找出

方案二:应用枚举类型

#include <stdio.h>int main( ){    enum Color {red,black,white};    enum Brand {lavida,tiggo,skoda};    enum Color color;    enum Brand brand;    for(color=red; color<=white; color++)        for(brand=lavida; brand<=skoda; brand++)            if(!((color==red&&brand==tiggo)||(color==white&&brand==skoda)))            {                switch(color)                {                case red:                    printf("红");                    break;                case black:                    printf("黑");                    break;                case white:                    printf("白");                    break;                }                switch(brand)                {                case lavida:                    printf("Lavida\n");                    break;                case tiggo:                    printf("Tiggo\n");                    break;                case skoda:                    printf("Skoda\n");                    break;                }            }    return 0;}//用枚举,除了好读,方案一的错误也不容易发生

枚举再例

#include <stdio.h>#include <math.h>enum SymmetricStyle { axisx,axisy,point};//分别表示按x轴, y轴, 原点对称struct CPoint{    double x;  // 横坐标    double y;  // 纵坐标};double distance(struct CPoint p1, struct CPoint p2)   // 两点之间的距离{    double d, dx, dy;    dx = p1.x-p2.x;    dy = p1.y-p2.y;    d=sqrt(dx*dx+dy*dy);    return d;}double distance0(struct CPoint p)// 到原点的距离{    double d;    d=sqrt(p.x*p.x+p.y*p.y);    return d;}struct CPoint SymmetricAxis(struct CPoint p, enum SymmetricStyle style)   // 返回对称点{    struct CPoint p1;    switch(style)    {    case axisx:        p1.x=p.x;        p1.y=-p.y;        break;    case axisy:        p1.x=-p.x;        p1.y=p.y;        break;    case point:        p1.x=-p.x;        p1.y=-p.y;    }    return p1;}int main( ){    struct CPoint p1 = {2, 2}, p2={-2, 4}, p;    printf("第1个点p1: (%.2lf, %.2lf)\n", p1.x, p1.y);    printf("第2个点p2: (%.2lf, %.2lf)\n", p2.x, p2.y);    printf("两点的距离为:%.2lf\n", distance(p1, p2));    printf("p1到原点的距离为:%.2lf\n",distance0(p1));    p = SymmetricAxis(p1, axisx);    printf("p1关于x轴的对称点为:(%.2lf, %.2lf)\n", p.x, p.y);    p = SymmetricAxis(p1, axisy);    printf("p1关于x轴的对称点为:(%.2lf, %.2lf)\n", p.x, p.y);    p = SymmetricAxis(p1, point);    printf("p1关于原点的对称点为:(%.2lf, %.2lf)\n", p.x, p.y);    return 0;}
0 0
原创粉丝点击