嵌入式学习笔记19.11.27

2019-11-27  本文已影响0人  Mo1035

c语言共用体:

#include <stdio.h>

union st{

int x;

char a;

char s[10];

}student;

int main(){

scanf("%d",&student.x);

getchar();

printf("%d\n",student.x);

printf("%d\n",student);

scanf("%c",&student.a);

getchar();

printf("%c\n",student.a);

printf("%c\n",student);

}

//共用体大小,占用共用体最大元素大小

//存数据的瞬间是最后一次赋的值

//运行谁存谁,值为最后存储的数据

//共用体一般用于临时数据

c语言枚举:

#include <stdio.h>

//枚举

enum color{

red,blue,yellow = 10,black,white = 20,pink

};

enum select{

A,B,C,D

}sel;

int main(){

printf("%d\n",red);

printf("%d\n",blue);

printf("%d\n",yellow);

printf("%d\n",black);

printf("%d\n",white);

printf("%d\n",pink);

switch(sel){

case A:printf("666");break;

case 1:printf("888");break;

}

}

//枚举默认第一项为0,第二项为1,以此类推

// 若第n项为10,则n+1项为11,以此类推

//对于选项进行整型判断

c语言二分法:

#include <stdio.h>

#define N ((a+b)/2)

#define M ((a+b+1)/2)

int main(){

int a = 0,b = 9,c = N,n,flag = 0;

while(1){

printf("请输入:");

scanf("%d",&n);

int s[10] = {1,4,7,12,15,27,36,49,51,63};

while(flag == 0){

if(s[c] > n){

b = c;

c = N;

}

else if(s[c] < n){

a = c + 1;

c = M;

}

else if(s[c] == n){

flag = 1;

break;

}

if(a == b){

flag = 2;

break;

}

}

if(flag == 1){

printf("数组的第%d个数\n",c+1);

}

if(flag == 2){

printf("查找的值不存在\n");

}

a = 0;b = 9;c = N;flag = 0;

}

}

//两种数据处理的方式:

//队列,先到先处理

//栈,先到后处理

//数据查找方式:(有序:小到大或大到小 数组)二分法:

//a[10] = {1, 4, 7, 12, 15,  27, 36, 49, 51, 63}

//                        ^                 

//                        |

//不能整除2就都+1/-1

//(0+9-1)/2  ?15

//(5+9-1)/2  ?36

//(6+9-1)/2  ?49√

//若上限与下限值相等都没有,就是没有数据

c语言单链表

#include <stdio.h>

#include <stdlib.h>

struct stu{

int m;

char a;

struct stu *next;//!!结构体指针是4个字节

};

struct stu student[5];

int main(){

int i;

struct stu *head;

if(head == NULL){

head = (struct stu*)malloc(sizeof(stu));

}

//head -> m = 1;

scanf("%d",&head->m);

//head -> a - 'g';

scanf("%d",&head->a);

struct stu *next;

for(i = 0;i < 5;i++){

next = (struct stu*)malloc(sizeof(stu));

scanf("%d",&next->m);

scanf("%c",&next->a);

}

//head -> next = (struct stu*)malloc(sizeof(stu));

//head -> next -> m = 2;

//head -> next -> a = 'h';

//head -> next -> next = (struct stu*)malloc(sizeof(stu));

//head -> next -> next -> m = 3;

//head -> next -> next -> a = 'i';

}

//链表,在结构体的基础上

//定义当前结构体类型的指针

//malloc在内存内申请空间<stdlib.h>

//free(指针)释放空间

//typedef int u32

//之后的代码u32等同于int

上一篇下一篇

猜你喜欢

热点阅读