#C语言单片机——数组拼接

通过c语言实现两个数组的拼接,从而接到显示屏幕上实现连续显示。

digit.c文件

/*
 * digit.c
 *
 *  Created on: 2025年3月8日
 *      Author: 86139
 */
#include"digit.h"
extern char display_str[MAX_DISPLAY_LENGTH] = ""; // 用于存储要显示的字符串
// 手动实现整数转字符串
void IntToString(int num, char *str)
{
    int i = 0;
    int isNegative = 0;
    if (num < 0) {
        isNegative = 1;
        num = -num;
    }
    // 处理个位数
    if (num == 0) {
        str[i++] = '0';
    } else {
        while (num > 0) {
            str[i++] = num % 10 + '0';
            num /= 10;
        }
    }
    // 如果是负数,添加负号
    if (isNegative) {
        str[i++] = '-';
    }
    str[i] = '\0';

    // 反转字符串
    int start = 0;
    int end = i - 1;
    while (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

// 手动实现字符串拼接
void MyStrcat(char *dest, const char *src)
{
    int dest_len = 0;
    // 找到 dest 的末尾
    while (dest[dest_len] != '\0') {
        dest_len++;
    }
    // 将 src 复制到 dest 的末尾
    int i = 0;
    while (src[i] != '\0') {
        dest[dest_len + i] = src[i];
        i++;
    }
    // 添加字符串结束符
    dest[dest_len + i] = '\0';
}


digit.h文件

/*
 * digit.h
 *
 *  Created on: 2025年3月8日
 *      Author: 86139
 */

#ifndef  DIGIT_H_
#define  DIGIT_H_
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#define MAX_DISPLAY_LENGTH 50
// 手动实现整数转字符串
void IntToString(int num, char *str);
// 手动实现字符串拼接
void MyStrcat(char *dest, const char *src);


#endif /* APP_CONNECTION_DIGIT_DIGIT_H_ */

main.c

#include "digit.h"
//还要引用一些必要的头文件
void main ()
{
 char display_str[MAX_DISPLAY_LENGTH] = "";
// 用于存储要显示的字符串  用于来连接两个数组的来自digit.h文件
    while()
    {
        if (keynumber != 16)//让按键返回值保留
            {
              char temp_str[10];//数组
               IntToString(keynumber, temp_str);
              MyStrcat(display_str, temp_str);

             OLED_ShowString(4, 1, display_str);//屏幕显示函数
            }
    }
}

这样就可以实现连续显示,保留前面按键输入的数值。

原理:将按键返回值放到一个temp_str数组,然后通过拼接函数让temp_str数组的内容放到display_str数组中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值