📄 arithmetic.c
字号:
#include "../include/arithmetic.h"
//定义全局缓冲池
COMM_LIST g_tcp_buf = NULL, g_tcp_tail = NULL; //TCP接收缓冲
COMM_LIST g_serial_buf = NULL, g_serial_tail = NULL; //串口接收缓冲
SEND_LIST g_tcpsend_buf = NULL, g_tcpsend_tail = NULL; //TCP发送缓冲
SEND_LIST g_serialsend_buf = NULL, g_serialsend_tail = NULL; //串口发送缓冲
//临时缓冲定义
TTY_LIST g_recv_buf = NULL, g_recv_tail = NULL;
//****************************************************************************
//平衡二叉树
void Select(AVL * prTree, TREE * ptr, unsigned char *Key); /* AvlSelect调用的查询函数 */
void Export(AVL * prTree, TREE * ptr, void (*Func) (TREE * ptr1)); /* AvlExport调用的遍历函数 */
void not_old(AVL * prTree, unsigned char *NewKey, TREE * ptr); /* 判断NewKey是否已存在 */
void InitNode(TREE * ptr, unsigned char *Key, void *Fd); /* 初始化一结点 */
void delroot(AVL * prTree); /* 删除根结点 */
void delnode(AVL * prTree, TREE * ptr, TREE * father, TREE * ffptr); /* 删除一个结点 */
void modi(AVL * prTree, TREE * fpr, TREE * ffpr); /* 不平衡结点的平衡化 */
void avl(AVL * prTree, TREE * ptr, TREE * fptr); /* 改变平衡因子,平衡化结点 */
TREE *ll(AVL * prTree, TREE * a, TREE * f); /* 平衡结点的移动 */
TREE *lr(AVL * prTree, TREE * a, TREE * f); /* 平衡结点的移动 */
TREE *l0(AVL * prTree, TREE * a, TREE * f); /* 平衡结点的移动 */
TREE *rr(AVL * prTree, TREE * a, TREE * f); /* 平衡结点的移动 */
TREE *rl(AVL * prTree, TREE * a, TREE * f); /* 平衡结点的移动 */
TREE *r0(AVL * prTree, TREE * a, TREE * f); /* 平衡结点的移动 */
//******************************************************************
/*
* 参数 : p_data 数据单元指针;i_datalength 数据单元长度
* 返回值 : 校验和
* 描述 : chksum_generate 校验和产生函数
*/
WORD chksum_generate(WORD * p_data, WORD i_datalength)
{
DWORD l_sum;
WORD w_oddbyte;
WORD chksum_answer;
l_sum = 0l;
while (i_datalength > 1) {
l_sum += *p_data++;
i_datalength -= 2;
}
if (i_datalength == 1) { //长度奇数处理
w_oddbyte = 0;
*((unsigned char *) &w_oddbyte) = *(unsigned char *) p_data;
l_sum += w_oddbyte;
}
l_sum = (l_sum >> 16) + (l_sum & 0xFFFF);
l_sum += (l_sum >> 16);
chksum_answer = (BYTE) ~ l_sum;
return chksum_answer;
}
/*
* 参数 : word_reverse 要转换的字
* 返回值 : 转换值
* 描述 : swap_word 转换字排列顺序的转换算法
*/
WORD swap_word(WORD word_reverse)
{
WORD lo, hi;
WORD result;
lo = word_reverse & 0x00FF;
hi = word_reverse & 0xFF00;
lo = lo << 8;
hi = hi >> 8;
result = hi | lo;
return result;
}
/*
* 参数 : bcd_source 要转换的字
* 返回值 : 转换值
* 描述 : BCD码转换成十进制
*/
UINT32 BCDtoDEC(BYTE * bcd_source, UINT32 len)
{
int i, i_lo = 0, i_hi = 0, count = 0;
BYTE lo, hi;
UINT32 answer = 0;
for (i = len; i > 0; i--) {
lo = bcd_source[i - 1] & 0x0F;
hi = bcd_source[i - 1] & 0xF0;
hi = hi >> 4;
i_lo = count;
i_hi = count + 1;
answer += hi * (pow(10, i_hi)) + lo * (pow(10, i_lo));
count += 2;
}
return answer;
}
/*
* 参数 : status 状态寄存器, bit 第几位
* 返回值 : 位值
* 描述 : 取指定位置的寄存器值
*/
INT8 get_bit_value(int status, INT8 bit)
{
status = status >> (bit - 1); //移位
status &= 0x0001; //与取最后一位
return status;
}
//*********************************************************************************
//指令缓冲处理
/*
* 参数 : pr_comm 指令数据, m_type缓冲池类型
* 返回值 : 0--成功 -1--错误
* 描述 : 增加指令到指令缓冲池
*/
int add_comm(void *pr_comm, int m_type)
{
COMM_BUF *c_node; //接收临时节点
SEND_BUF *s_node;
switch (m_type) {
case 0: //TCP接收缓冲处理
c_node = (COMM_BUF *) malloc(sizeof(COMM_BUF));
c_node->accept_comm = *((STR_COMM *) pr_comm);
c_node->next = NULL;
if (g_tcp_buf == NULL) { //根结点处理
g_tcp_buf = c_node;
g_tcp_tail = g_tcp_buf;
} else {
g_tcp_tail->next = c_node;
g_tcp_tail = c_node;
}
return 0;
case 1: //串口接收缓冲处理
c_node = (COMM_BUF *) malloc(sizeof(COMM_BUF));
c_node->accept_comm = *((STR_COMM *) pr_comm);
c_node->next = NULL;
if (g_serial_buf == NULL) { //根结点处理
g_serial_buf = c_node;
g_serial_tail = g_serial_buf;
} else {
g_serial_tail->next = c_node;
g_serial_tail = c_node;
}
return 0;
case 2: //TCP发送缓冲处理
s_node = (SEND_BUF *) malloc(sizeof(COMM_BUF));
s_node->send_comm = *((STR_SEND *) pr_comm);
s_node->next = NULL;
if (g_tcpsend_buf == NULL) { //根结点处理
g_tcpsend_buf = s_node;
g_tcpsend_tail = g_tcpsend_buf;
} else {
g_tcpsend_tail->next = s_node;
g_tcpsend_tail = s_node;
}
return 0;
case 3: //串口发送缓冲处理
s_node = (SEND_BUF *) malloc(sizeof(COMM_BUF));
s_node->send_comm = *((STR_SEND *) pr_comm);
s_node->next = NULL;
if (g_serialsend_buf == NULL) { //根结点处理
g_serialsend_buf = s_node;
g_serialsend_tail = g_serialsend_buf;
} else {
g_serialsend_tail->next = s_node;
g_serialsend_tail = s_node;
}
return 0;
default: //错误的缓冲区类型
return -1;
}
}
/*
* 参数 : m_type缓冲池类型
* 返回值 : *pr_comm 0--有数据 -1--空数据
* 描述 : 取指令从指令缓冲池
*/
int get_comm(void *pr_comm, int m_type)
{
COMM_BUF *c_gettmp; //取指令临时节点
SEND_BUF *s_gettmp;
switch (m_type) {
case 0: //TCP取接收指令缓冲处理
if (g_tcp_buf == NULL) {
return -1;
}
if (g_tcp_buf != g_tcp_tail) {
c_gettmp = g_tcp_buf;
memcpy((STR_COMM *) pr_comm, &(g_tcp_buf->accept_comm), sizeof(STR_COMM)); //取指令
g_tcp_buf = g_tcp_buf->next;
free(c_gettmp);
} else {
memcpy((STR_COMM *) pr_comm, &(g_tcp_buf->accept_comm), sizeof(STR_COMM)); //取指令
free(g_tcp_buf);
g_tcp_buf = NULL;
g_tcp_tail = NULL;
}
return 0;
case 1: //串口取接收指令缓冲处理
if (g_serial_buf == NULL) {
return -1;
}
if (g_serial_buf != g_serial_tail) {
c_gettmp = g_serial_buf;
memcpy((STR_COMM *) pr_comm, &(g_serial_buf->accept_comm), sizeof(STR_COMM)); //取指令
g_serial_buf = g_serial_buf->next;
free(c_gettmp);
} else {
memcpy((STR_COMM *) pr_comm, &(g_serial_buf->accept_comm), sizeof(STR_COMM)); //取指令
free(g_serial_buf);
g_serial_buf = NULL;
g_serial_tail = NULL;
}
return 0;
case 2: //TCP取发送指令处理
if (g_tcpsend_buf == NULL) {
return -1;
}
if (g_tcpsend_buf != g_tcpsend_tail) {
s_gettmp = g_tcpsend_buf;
memcpy((STR_SEND *) pr_comm, &(g_tcpsend_buf->send_comm), sizeof(STR_SEND)); //取指令
g_tcpsend_buf = g_tcpsend_buf->next;
free(s_gettmp);
} else {
memcpy((STR_SEND *) pr_comm, &(g_tcpsend_buf->send_comm), sizeof(STR_SEND)); //取指令
free(g_tcpsend_buf);
g_tcpsend_buf = NULL;
g_tcpsend_tail = NULL;
}
return 0;
case 3: //串口取发送指令处理
if (g_serialsend_buf == NULL) {
return -1;
}
if (g_serialsend_buf != g_serialsend_tail) {
s_gettmp = g_serialsend_buf;
memcpy((STR_SEND *) pr_comm, &(g_serialsend_buf->send_comm), sizeof(STR_SEND)); //取指令
g_serialsend_buf = g_serialsend_buf->next;
free(s_gettmp);
} else {
memcpy((STR_SEND *) pr_comm, &(g_serialsend_buf->send_comm), sizeof(STR_SEND)); //取指令
free(g_serialsend_buf);
g_serialsend_buf = NULL;
g_serialsend_tail = NULL;
}
return 0;
default: //错误的类型
return -1;
}
}
/*
* 参数 :
* 返回值 :
* 描述 : 指令缓冲池释放
*/
void free_buf(void)
{
COMM_BUF *p_tmp;
if (g_tcp_buf == NULL) {
return;
} //指令池空释放
//存在指令释放
while (g_tcp_buf != g_tcp_tail) {
p_tmp = g_tcp_buf;
g_tcp_buf = g_tcp_buf->next;
free(p_tmp);
}
if (g_tcp_buf == g_tcp_tail) {
free(g_tcp_buf);
}
return;
}
/*
* 参数 : *m_data数据, len 长度
* 返回值 :
* 描述 : 增加数据到缓冲池
*/
void tty_data_add(BYTE * m_data, int len)
{
int i;
TTY_BUF *c_node; //接收临时节点
for (i = 0; i < len; i++) {
c_node = (TTY_BUF *) malloc(sizeof(TTY_BUF));
c_node->data = m_data[i];
c_node->next = NULL;
if (g_recv_buf == NULL) { //根结点处理
g_recv_buf = c_node;
g_recv_tail = g_recv_buf;
} else {
g_recv_tail->next = c_node;
g_recv_tail = c_node;
}
}
return;
}
/*
* 参数 : len 长度
* 返回值 : *m_data数据
* 描述 : 取数据从缓冲池
*/
int tty_data_get(BYTE * m_data, int len)
{
int i;
TTY_BUF *c_gettmp; //取临时节点
BYTE *m_tmp;
m_tmp = (BYTE *) malloc(len); //分配临时空间
bzero(m_tmp, len);
for (i = 0; i < len; i++) {
if (g_recv_buf == NULL) {
return -1;
}
if (g_recv_buf != g_recv_tail) {
c_gettmp = g_recv_buf;
m_tmp[i] = g_recv_buf->data;
g_recv_buf = g_recv_buf->next;
free(c_gettmp);
} else {
m_tmp[i] = g_recv_buf->data;
free(g_recv_buf);
g_recv_buf = NULL;
g_recv_tail = NULL;
break;
}
}
//传递数据
memcpy(m_data, m_tmp, len);
free(m_tmp);
return 0;
}
/*
* 参数 : len 长度
* 返回值 : *m_data数据
* 描述 : 取数据从缓冲池
*/
int tty_data_seek(BYTE * m_data, int len)
{
int i;
TTY_BUF *c_gettmp; //取临时节点
BYTE m_tmp;
c_gettmp = g_recv_buf;
if (g_recv_buf == NULL) {
return -1;
}
for (i = 0; i < (len - 1); i++) {
if (c_gettmp != g_recv_tail) {
c_gettmp = c_gettmp->next;
}
}
m_tmp = c_gettmp->data;
//传递数据
memcpy(m_data, &m_tmp, 1);
return 0;
}
/*
* 参数 :
* 返回值 :
* 描述 : 指令缓冲池释放
*/
void tty_data_free(void)
{
TTY_BUF *p_tmp;
if (g_recv_buf == NULL) {
return;
} //指令池空释放
//存在指令释放
while (g_recv_buf != g_recv_tail) {
p_tmp = g_recv_buf;
g_recv_buf = g_recv_buf->next;
free(p_tmp);
}
if (g_recv_buf == g_recv_tail) {
free(g_recv_buf);
}
return;
}
//*********************************************************************************
//平衡二叉树处理
/*
* 参数 : prTree:AVL树指针,FdSize:域大小
* 返回值 :
* 描述 : 平衡二叉树算法,初始化一棵AVL树
*/
void AvlInit(AVL * prTree, unsigned short FdSize)
{
prTree->FieldSize = FdSize;
prTree->Root = NULL;
prTree->Result = NULL;
prTree->ErrorCode = 0;
strcpy(prTree->ErrorInfo, "");
}
/*
* 参数 : prTree:AVL树指针
* 返回值 :
* 描述 : 平衡二叉树算法,释放一棵AVL树
*/
void AvlFree(AVL * prTree)
{
while (prTree->Root != NULL)
AvlDelete(prTree, prTree->Root->Key);
}
/*
* 参数 : prTree:AVL树指针,NewFd:指向要插入域的指针,NewKey:所要插入域的关键字指针
* 返回值 :
* 描述 : 向一棵AVL树中插入一个结点
*/
void AvlInsert(AVL * prTree, void *NewFd, unsigned char *NewKey)
{
TREE *s, *p, *q, *f1, *a1, *b, *c;
char d, balance;
prTree->ErrorCode = 0;
if ((s = (TREE *) malloc(sizeof(TREE))) == NULL) {
prTree->ErrorCode = 5;
strcpy(prTree->ErrorInfo, "malloc fail!");
return;
}
InitNode(s, NewKey, NewFd);
if (prTree->Root == NULL) {
prTree->Root = s;
return;
} else {
f1 = NULL;
a1 = prTree->Root;
p = prTree->Root;
q = NULL;
while (p != NULL) {
if (p->bf != 0) {
a1 = p;
f1 = q;
}
q = p;
if (strcmp(s->Key, p->Key) == 0) {
prTree->ErrorCode = 6;
sprintf(prTree->ErrorInfo,
"data:%s is already in the tree", s->Key);
free(s);
return;
}
if (strcmp(s->Key, p->Key) < 0)
p = p->left_pointer;
else
p = p->right_pointer;
}
if (strcmp(s->Key, q->Key) < 0)
q->left_pointer = s;
else
q->right_pointer = s;
if (strcmp(s->Key, a1->Key) < 0) {
p = a1->left_pointer;
b = p;
d = 1;
} else {
p = a1->right_pointer;
b = p;
d = -1;
}
while (p != s) {
if (strcmp(s->Key, p->Key) < 0) {
p->bf = 1;
p = p->left_pointer;
} else {
p->bf = -1;
p = p->right_pointer;
}
}
balance = 1;
if (a1->bf == 0)
a1->bf = d;
else if (a1->bf + d == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -