美文网首页
C语言枚举,联合体,预处理

C语言枚举,联合体,预处理

作者: 帅碧 | 来源:发表于2016-11-04 16:25 被阅读0次

枚举

enum Seaeon
{
    spring,//spring=1;则summer=2
    summer,//若summer=6,则autumn=7
    autumn,
    winter
};

int main()
{
    enum Season season=spring;//(Season)1
    enum Season season1=summer;
    printf("season=%d\n",season);
    printf("season1=%d\n",season1);
    return 0;
}

>- 结果为0,1

  • 作业:将字符串"123"以整型123输出

#include "stdio.h"
int func(char *str1)
{
    char p[10];
    int length=0,i,num=0;
    while(*str1!='\0')
    {
        length++;
        p[length-1]=*str1;
        str1++;
    }
    for(i=0;i<length;i++)
    {
        num=num*10+(p[i]-48);
    }
    return num;
}
int main()
{
    char string[20];
    int num=0;
    printf("请输入一个字符串\n");
    scanf("%s",string);
    getchar();
    num=func(string);
    printf("原来的字符串为%s\n",string);
    printf("变成整形为:%d\n",num);
    return 0;
}

双链表

#include "stdio.h"
#include "stdlib.h"
typedef struct LINK
{
    int num;
    struct LINK *pre;
    struct LINK *next;
}LINK,*pLINK;

pLINK createDoubleList(pLINK head)
{
    if(head==NULL)
    {   
        head=(pLINK)malloc(sizeof(LINK));
        head->pre=NULL;
        head->next=NULL;
    }
    printf("双链表创建成功\n");
    return head;

}

int getNum()
{
    int num;
    printf("请输入数字\n");
    scanf("%d",&num);
    return num;
}
void headInsertData(pLINK head)
{
    if(head==NULL)
    {
        printf("双链表没有创建\n");
        return;
    }
    if(head->next==NULL)
    {
        pLINK p=(pLINK)malloc(sizeof(LINK));
        p->next=NULL;
        p->pre=head;
        head->next=p;
        p->num=getNum();
        return;
    }
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->num=getNum();
    p->next=head->next;
    p->pre=head;
    head->next=p;
    p->next->pre=p;
    printf("头插数据成功\n");
}
void printData(pLINK head)
{
    if(head==NULL||head->next==NULL)
    {
        printf("无信息可打印\n");
        return;
    }
    pLINK temp;
    printf("head--->");
    for(temp=head->next;temp!=NULL;temp=temp->next)
    {
        printf("[%d]--->",temp->num);
    }
    printf("NULL\n");
}
void tailInsertData(pLINK head)
{
    if(head==NULL)
    {
        printf("双链表没有创建\n");
        return;
    }
    pLINK temp;
    for(temp=head;temp->next!=NULL;temp=temp->next);
    pLINK p=(pLINK)malloc(sizeof(LINK));
    temp->next=p;
    p->next=NULL;
    p->pre=temp;
    p->num=getNum();
    printf("尾插数据成功\n");
}
void headDeleteData(pLINK head)
{
    if(head==NULL||head->next==NULL)
    {
        printf("无信息可删\n");
        return;
    }
    if(head->next->next==NULL)
    {
        pLINK p=head->next;
        head->next=NULL;
        free(p);
        p=NULL;
        return;
    }
    pLINK p=head->next;
    head->next=p->next;
    p->next->pre=head;
    free(p);
    p=NULL;
    printf("头删成功\n");
}
void tailDeleteData(pLINK head)
{
    if(head==NULL||head->next==NULL)
    {
        printf("无信息可删\n");
        return;
    }
    if(head->next->next==NULL)
    {
        pLINK p=head->next;
        head->next=NULL;
        free(p);
        p=NULL;
        return;
    }
    pLINK temp; 
    for(temp=head;temp->next!=NULL;temp=temp->next);
    pLINK p=temp->pre;
    p->next=NULL;
    free(temp);
    temp=NULL;
    printf("尾删成功\n");
    return;
}

int main()
{
    pLINK head=NULL;
    int select;
    while(1)
    {
        printf("======\n");
        printf("1.创建链表\n");
        printf("2.头插数据\n");
        printf("3.尾插数据\n");
        printf("4.头删数据\n");
        printf("5.尾删数据\n");
        printf("6.打印数据\n");
        printf("7.退出\n");
        printf("=========\n");
        scanf("%d",&select);
        switch(select)
        {
            case 1:
                head=createDoubleList(head);
                break;
            case 2:
                headInsertData(head);
                break;
            case 3:
                tailInsertData(head);
                break;
            case 4:
                headDeleteData(head);
                break;
            case 5:
                tailDeleteData(head);
                break;
            case 6:
                printData(head);
                break;
            case 7:
                return 0;
            default :
                break;
                
        }

    }
}

联合体

  • 联合体:多个成员变量共用一块空间,一个时间段只能用其中的一个成员
  1. 如果成员变量都是基本数据类型,那么这个联合体的所占空间是最大成员变量所占的空间
  2. 如果不是基本数据类型,struct Person{int num;double score;};最后收尾是按成员变量里的最大字节数的成原变量的最小倍数。
  3. 最后徐收尾的时候,这个联合体所占的空间能够容纳最大成员变量的所占的空间,还要是单个成员变量字节数的最小倍数。

预编译处理

#define N 1
#include "stdio.h"
int main()
{
#if N
    int a=8;
    printf("a=%d\n",a);
#else    
    int b=9;
    printf("b=%d\n",b);
#endif
    return 0;
}

条件编译

#if N

#endif
#if N

#else

#endif

#if N

#elif M

#else

#endif

  • 案例
#define N 0
#define M 0
#include "stdio.h"
int main()
{
#if N
    int a=8;
    printf("a=%d\n",a);
#elif M    
    int c=10;
    printf("c=%d\n",c);
#else
    int b=9;
    printf("b=%d\n",b);
#endif
    return 0;
}


#ifdef MM//分析:如果上面有对MM进行过宏定义,就编译ifdef下面的语句。

    int a=9;
    printf("a=%d\n",a);
#endif
    return 0;
>- 结果为a=9;


#ifndef MM
    int a=9;
    printf("a=%d\n",a);
#endif
    return 0;
    
>- 结果为:return 0;不执行a=9;

队列

相关文章

  • C语言枚举,联合体,预处理

    枚举 作业:将字符串"123"以整型123输出 双链表 联合体 联合体:多个成员变量共用一块空间,一个时间段只能用...

  • C语言基础及指针⑩预编译及jni.h分析

    接续上篇C语言基础及指针⑨联合体与枚举 在上篇中我们了解了 , 多类型集合的联合体 , 固定值集合的枚举 , 内容...

  • (五)C语言之联合体,枚举与IO

    (五)C语言之联合体,枚举与IO 一、联合体(共用体) 1、定义 不同类型的变量共同占用一段内存(相互覆盖),联合...

  • NDK—C语言IO操作

    前面讲解了C语言的结构体、联合体和枚举,虽然只是基础,但是对于开发NDK来说已经够用了,接下来我们来学习一下C语言...

  • C语言马拉松_03.3

    结构体 联合体 枚举类型 枚举类型 在实际生活中有很多对象的属性并不能使用C语言中给出的变量来直观描述,为了使程序...

  • 嵌入式第十天:结构

    今天来说一下C语言里的结构体(struct)、共用体(l联合体)union、枚举。欢迎加入嵌入式学习群:55960...

  • C语言 结构体,联合体,枚举

    结构体 C语言中结构体是一组构造数据类型,把不同的数据类型整合起来成为一个自定义的数据类型。如: 初始化方式 或者...

  • 慕课网-Linux C语言结构体-学习笔记

    Linux C语言结构体 编译指令:预处理,宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 逻辑运算符...

  • 位移枚举

    位移枚举 一. OC中常见的三种枚举 C语言枚举 // C语言枚举 typedef enum : NSUInteg...

  • 基础C语言知识串串香10☞数组&字符串&结构体&

    五、数组&字符串&结构体&联合体&枚举 5.1、c语言中定义一个字符串:char a[6]={'l','i','n...

网友评论

      本文标题:C语言枚举,联合体,预处理

      本文链接:https://www.haomeiwen.com/subject/exaluttx.html