24.08.05

day5

数组

定义:同种数据类型的集合,一片连续的内存空间。
    
    
一般形式:
    存储类型  数据类型  数组名[元素个数];
    
    
eg:
    int arr[5];   

操作数组元素的方法:
     下标法:
           (1)下标从0开始
           (2)数组名[下标]3)不要越界使用
    
    
1.数组的初始化
    
    (1)完全初始化
    	int arr[5] = {1,2,3,4,5};2)部分初始化:给部分元素赋值,没有赋值的元素自动赋值为0
        int arr[5] = {1,2,3};
        int arr[5] = {0}; 
        int arr[5] = {[1]=1, [3]=2};
       

    (3)自动计数原则:
        int arr[] = {1,2,3};
        int arr[] = {1, [2]= 2, [4] = 5};



	(4)可变数组(危险不要使用)    ----- 动态内存
        int n;
        n = 10int arr[n];

	   不可以在定义的时候给赋初值		
       //int arr[n] = {1,2,3};  //error        


           
(2)赋值    
           
      int arr[5];
      arr[0] = 10;
      arr[1] = 20;


数组的遍历:
     int i;
     for(i = 0; i < 5; i++)
     {
		printf("%d\n", arr[i]);         
     }

练习

1.实现数组元素的累加输出。	

int arr[8] = {1000, 999, 520, 10, 99, 77, 50, 20};
1.实现数组的逆序存储
int a[10] = {1,2,3,4,5,6,7,8,9,10};
  


#define N 6
int main(int argc, char *argv[])
{
    int i;
    int arr[N];

    //终端输入
    for(i = 0; i < N; i++)
    {
        printf("arr[%d]= ", i);
        scanf("%d", &arr[i]);
    }

    
    //打印
    for(i = 0; i < N; i++)
    {
        printf("%d\t", arr[i]);
    }
    puts("");

    
    //逆序
    for(i = 0; i <= N / 2; i++)
    {
        int temp;
        temp = arr[i];
        arr[i] = arr[N - 1 -i];
        arr[N - 1 -i] = temp;
    }

    //打印
    for(i = 0; i < N; i++)
    {
        printf("%d\t", arr[i]);
    }
    puts("");

    return 0;
}  
2.冒泡排序
   int arr[10] = {10, 99, 66, 77, 33, 58, 26, 25, 18, 6}; 

   //趟数
    int j;
    for(i = 0; i < 10 - 1; i++)
    {   
        //两两比较
        for(j = 0; j < 10 - 1 - i; j++)
        {
            if(arr[j] <  arr[j+1])
            {
                int temp;
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }   

char 型数组

char str[32] = {0};  
    
char str[32] = "hello world";   




char str[32] = {0};
scanf("%s", str);   ---  不可以输入带空格的字符串

gets(str);  --- 可以输入带空格的字符串

练习

1.终端输入字符串,将字符串中的小写字母转为大写, 大写字母转为小写
    char str[32] = {0};

    printf(">:");
    gets(str);

    int i = 0;
    while(1)
    {   
        if(str[i] == '\0')
            break;


        if(str[i] >= 'a' && str[i] <= 'z')
        {
            str[i] = str[i] - ' ';
        }
        else if(str[i] >= 'A' && str[i] <= 'Z')
        {
            str[i] = str[i] + 32; 
        }

        i++;
    }


    puts(str);

作业

编程题 
    1.输出数组中的最小值及其下标,最大值及其下标
    
    
    2. 数组循环移位
	    (1int arr[5] = {1,2,3,4,5};  实现循环右移一位
    	(2)终端输入int arr[5],实现循环右移一位
    	(3)可变数组数组,终端输入,实现循环右移一位
        (4)可变数组数组,终端输入,实现循环右移n位
   
    
    3.编写程序判断一个字符串是否为回文字符串,大小写也必须一致 
      "abcdcba"  "123321"
    
    
    
    4.删除字符串中的空格
    
    
    5.实现字符串转数字, 例如: "+123456"   --->  123456
                            "-128"      --->  -128
                            "1314"      --->   1314        
  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的nrf24l01.c和nrf24l01.h文件的程序示例,仅供参考: nrf24l01.h 文件: ```c #ifndef __NRF24L01_H #define __NRF24L01_H #include "stm32f10x.h" //定义NRF24L01的寄存器地址 #define NRF24L01_CONFIG 0x00 #define NRF24L01_EN_AA 0x01 #define NRF24L01_EN_RXADDR 0x02 #define NRF24L01_SETUP_AW 0x03 #define NRF24L01_SETUP_RETR 0x04 #define NRF24L01_RF_CH 0x05 #define NRF24L01_RF_SETUP 0x06 #define NRF24L01_STATUS 0x07 #define NRF24L01_OBSERVE_TX 0x08 #define NRF24L01_CD 0x09 #define NRF24L01_RX_ADDR_P0 0x0A #define NRF24L01_RX_ADDR_P1 0x0B #define NRF24L01_RX_ADDR_P2 0x0C #define NRF24L01_RX_ADDR_P3 0x0D #define NRF24L01_RX_ADDR_P4 0x0E #define NRF24L01_RX_ADDR_P5 0x0F #define NRF24L01_TX_ADDR 0x10 #define NRF24L01_RX_PW_P0 0x11 #define NRF24L01_RX_PW_P1 0x12 #define NRF24L01_RX_PW_P2 0x13 #define NRF24L01_RX_PW_P3 0x14 #define NRF24L01_RX_PW_P4 0x15 #define NRF24L01_RX_PW_P5 0x16 #define NRF24L01_FIFO_STATUS 0x17 #define NRF24L01_DYNPD 0x1C #define NRF24L01_FEATURE 0x1D //定义NRF24L01指令 #define NRF24L01_R_REGISTER 0x00 #define NRF24L01_W_REGISTER 0x20 #define NRF24L01_R_RX_PAYLOAD 0x61 #define NRF24L01_W_TX_PAYLOAD 0xA0 #define NRF24L01_FLUSH_TX 0xE1 #define NRF24L01_FLUSH_RX 0xE2 #define NRF24L01_REUSE_TX_PL 0xE3 #define NRF24L01_NOP 0xFF //定义NRF24L01寄存器位 #define NRF24L01_CONFIG_MASK_RX_DR 0x40 #define NRF24L01_CONFIG_MASK_TX_DS 0x20 #define NRF24L01_CONFIG_MASK_MAX_RT 0x10 #define NRF24L01_CONFIG_EN_CRC 0x08 #define NRF24L01_CONFIG_CRCO 0x04 #define NRF24L01_CONFIG_PWR_UP 0x02 #define NRF24L01_CONFIG_PRIM_RX 0x01 #define NRF24L01_STATUS_RX_DR 0x40 #define NRF24L01_STATUS_TX_DS 0x20 #define NRF24L01_STATUS_MAX_RT 0x10 #define NRF24L01_STATUS_TX_FULL 0x01 #define NRF24L01_RF_SETUP_CONT_WAVE 0x80 #define NRF24L01_RF_SETUP_RF_DR_LOW 0x20 #define NRF24L01_RF_SETUP_PLL_LOCK 0x10 #define NRF24L01_LNA_HCURR 0x01 #define NRF24L01_RX_PW_PX_DEFAULT 0x00 #define NRF24L01_SPI_TIMEOUT 100 //定义NRF24L01模块的IO口 #define NRF24L01_CE_PORT GPIOB #define NRF24L01_CE_PIN GPIO_Pin_0 #define NRF24L01_CSN_PORT GPIOB #define NRF24L01_CSN_PIN GPIO_Pin_1 #define NRF24L01_IRQ_PORT GPIOB #define NRF24L01_IRQ_PIN GPIO_Pin_5 void NRF24L01_GPIO_Init(void); void NRF24L01_SPI_Init(void); void NRF24L01_Init(void); void NRF24L01_CE(uint8_t level); void NRF24L01_CSN(uint8_t level); uint8_t NRF24L01_ReadWriteByte(uint8_t txData); uint8_t NRF24L01_ReadReg(uint8_t regAddr); void NRF24L01_WriteReg(uint8_t regAddr, uint8_t txData); void NRF24L01_ReadBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len); void NRF24L01_WriteBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len); void NRF24L01_SetupRxMode(void); void NRF24L01_SetupTxMode(void); void NRF24L01_TxPacket(uint8_t *txBuf, uint8_t len); uint8_t NRF24L01_RxPacket(uint8_t *rxBuf); #endif ``` nrf24l01.c 文件: ```c #include "nrf24l01.h" static uint8_t NRF24L01_SPI_SendByte(uint8_t txData) { uint8_t retry = NRF24L01_SPI_TIMEOUT; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) { if (--retry == 0) { return 0; } } SPI_I2S_SendData(SPI1, txData); retry = NRF24L01_SPI_TIMEOUT; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) { if (--retry == 0) { return 0; } } return SPI_I2S_ReceiveData(SPI1); } void NRF24L01_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = NRF24L01_CE_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(NRF24L01_CE_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = NRF24L01_CSN_PIN; GPIO_Init(NRF24L01_CSN_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = NRF24L01_IRQ_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(NRF24L01_IRQ_PORT, &GPIO_InitStructure); } void NRF24L01_SPI_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); } void NRF24L01_Init(void) { NRF24L01_GPIO_Init(); NRF24L01_SPI_Init(); NRF24L01_CE(0); NRF24L01_CSN(1); } void NRF24L01_CE(uint8_t level) { if (level) { GPIO_SetBits(NRF24L01_CE_PORT, NRF24L01_CE_PIN); } else { GPIO_ResetBits(NRF24L01_CE_PORT, NRF24L01_CE_PIN); } } void NRF24L01_CSN(uint8_t level) { if (level) { GPIO_SetBits(NRF24L01_CSN_PORT, NRF24L01_CSN_PIN); } else { GPIO_ResetBits(NRF24L01_CSN_PORT, NRF24L01_CSN_PIN); } } uint8_t NRF24L01_ReadWriteByte(uint8_t txData) { NRF24L01_CSN(0); uint8_t rxData = NRF24L01_SPI_SendByte(txData); NRF24L01_CSN(1); return rxData; } uint8_t NRF24L01_ReadReg(uint8_t regAddr) { NRF24L01_CSN(0); NRF24L01_SPI_SendByte(NRF24L01_R_REGISTER | regAddr); uint8_t regValue = NRF24L01_SPI_SendByte(NRF24L01_NOP); NRF24L01_CSN(1); return regValue; } void NRF24L01_WriteReg(uint8_t regAddr, uint8_t txData) { NRF24L01_CSN(0); NRF24L01_SPI_SendByte(NRF24L01_W_REGISTER | regAddr); NRF24L01_SPI_SendByte(txData); NRF24L01_CSN(1); } void NRF24L01_ReadBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len) { NRF24L01_CSN(0); NRF24L01_SPI_SendByte(NRF24L01_R_REGISTER | regAddr); for (uint8_t i = 0; i < len; i++) { pBuf[i] = NRF24L01_SPI_SendByte(NRF24L01_NOP); } NRF24L01_CSN(1); } void NRF24L01_WriteBuf(uint8_t regAddr, uint8_t *pBuf, uint8_t len) { NRF24L01_CSN(0); NRF24L01_SPI_SendByte(NRF24L01_W_REGISTER | regAddr); for (uint8_t i = 0; i < len; i++) { NRF24L01_SPI_SendByte(pBuf[i]); } NRF24L01_CSN(1); } void NRF24L01_SetupRxMode(void) { NRF24L01_CE(0); NRF24L01_WriteReg(NRF24L01_CONFIG, NRF24L01_CONFIG_EN_CRC | NRF24L01_CONFIG_CRCO | NRF24L01_CONFIG_PWR_UP | NRF24L01_CONFIG_PRIM_RX); NRF24L01_WriteReg(NRF24L01_EN_AA, 0x01); NRF24L01_WriteReg(NRF24L01_EN_RXADDR, 0x01); NRF24L01_WriteReg(NRF24L01_SETUP_RETR, 0x5F); NRF24L01_WriteReg(NRF24L01_RF_CH, 0x02); NRF24L01_WriteReg(NRF24L01_RF_SETUP, NRF24L01_RF_SETUP_RF_DR_LOW | NRF24L01_RF_SETUP_CONT_WAVE | NRF24L01_RF_SETUP_PLL_LOCK); NRF24L01_WriteReg(NRF24L01_RX_PW_P0, 0x20); NRF24L01_WriteReg(NRF24L01_FEATURE, 0x06); NRF24L01_CE(1); } void NRF24L01_SetupTxMode(void) { NRF24L01_CE(0); NRF24L01_WriteReg(NRF24L01_CONFIG, NRF24L01_CONFIG_EN_CRC | NRF24L01_CONFIG_CRCO | NRF24L01_CONFIG_PWR_UP | !NRF24L01_CONFIG_PRIM_RX); NRF24L01_WriteReg(NRF24L01_EN_AA, 0x01); NRF24L01_WriteReg(NRF24L01_EN_RXADDR, 0x01); NRF24L01_WriteReg(NRF24L01_SETUP_RETR, 0x5F); NRF24L01_WriteReg(NRF24L01_RF_CH, 0x02); NRF24L01_WriteReg(NRF24L01_RF_SETUP, NRF24L01_RF_SETUP_RF_DR_LOW | NRF24L01_RF_SETUP_CONT_WAVE | NRF24L01_RF_SETUP_PLL_LOCK); NRF24L01_WriteReg(NRF24L01_RX_PW_P0, 0x20); NRF24L01_WriteReg(NRF24L01_FEATURE, 0x06); NRF24L01_CE(1); } void NRF24L01_TxPacket(uint8_t *txBuf, uint8_t len) { NRF24L01_CE(0); NRF24L01_WriteBuf(NRF24L01_W_TX_PAYLOAD, txBuf, len); NRF24L01_CE(1); GPIO_ResetBits(NRF24L01_CE_PORT, NRF24L01_CE_PIN); delay_us(10); NRF24L01_CE(0); } uint8_t NRF24L01_RxPacket(uint8_t *rxBuf) { uint8_t status = NRF24L01_ReadReg(NRF24L01_STATUS); if (status & NRF24L01_STATUS_RX_DR) { NRF24L01_ReadBuf(NRF24L01_R_RX_PAYLOAD, rxBuf, 32); NRF24L01_WriteReg(NRF24L01_STATUS, status | NRF24L01_STATUS_RX_DR); return 1; } return 0; } ``` 这个程序示例是基于STM32F10x的,如果您使用其他单片机,您可能需要根据您的单片机进行适当的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值