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

简介: 贺老师教学链接 C语言及程序设计进阶 本课讲解He先生方案一:用整型表示品牌、颜色#include <stdio.h>int main( ){ int brand,color; //brand=0,1,2分别表示Lavida、Tiggo和Skoda //color=0,1,2分别表示红黑白 for(color=0; c

贺老师教学链接 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;
}
目录
相关文章
|
21天前
|
存储 数据处理 C语言
C语言高级应用探讨与实例
C语言高级应用探讨与实例
21 1
|
21天前
|
传感器 网络协议 C语言
C语言在网络编程中的实际应用
C语言在网络编程中的实际应用
19 1
|
15天前
|
编译器 C语言 C++
C语言学习记录——位段(内存分配、位段的跨平台、位段的应用)
C语言学习记录——位段(内存分配、位段的跨平台、位段的应用)
15 0
|
21天前
|
算法 Java Unix
C语言:基础、特性与实际应用
C语言:基础、特性与实际应用
30 0
|
6天前
|
机器学习/深度学习 算法 C语言
详细介绍递归算法在 C 语言中的应用,包括递归的基本概念、特点、实现方法以及实际应用案例
【6月更文挑战第15天】递归算法在C语言中是强大力量的体现,通过函数调用自身解决复杂问题。递归涉及基本概念如自调用、终止条件及栈空间管理。在C中实现递归需定义递归函数,分解问题并设定停止条件。阶乘和斐波那契数列是经典应用示例,展示了递归的优雅与效率。然而,递归可能导致栈溢出,需注意优化。学习递归深化了对“分而治之”策略的理解。**
20 7
|
10天前
|
数据库 C语言
C语言进阶 文件操作知识(上)
C语言进阶 文件操作知识(上)
11 3
|
10天前
|
存储 C语言
C语言进阶 文件操作知识(下)
C语言进阶 文件操作知识(下)
14 2
|
18天前
|
存储 编译器 数据库
【再识C进阶5(上)】详细介绍C语言文件操作——文件是用于存储数据
【再识C进阶5(上)】详细介绍C语言文件操作——文件是用于存储数据
|
21天前
|
C语言
C语言函数调用详解与实战应用
C语言函数调用详解与实战应用
21 2
|
21天前
|
算法 C语言
深度探索C语言中的do-while语句:理解、应用与性能优化
深度探索C语言中的do-while语句:理解、应用与性能优化
11 1