size_strlen(const char*):无法将参数从“unit8_t"转化为”const char*
时间: 2024-05-05 07:21:47 浏览: 48
这个错误是因为函数 `size_strlen` 的参数类型是 `const char*`,但是你传入的是 `unit8_t` 类型的参数。你需要将 `unit8_t` 类型的参数转换为 `const char*` 类型,例如:
```c++
size_strlen(reinterpret_cast<const char*>(your_unit8_t_variable));
```
或者你可以修改函数 `size_strlen` 的参数类型为 `unit8_t*`,这样就可以直接传入 `unit8_t` 类型的参数了。
相关问题
写一个函数 char * Convert(int m),将传入的参数m(m最多为五位数而且是整数)转换为大写的金额。如m为12345,则返回“壹万贰仟叁佰肆拾伍”。用c语言给出完整代码,不要注释不要解释直接给代码
### 回答1:
这是一个用 C 语言实现的代码,它实现了将传入的整数转换为大写金额的功能:
```
#include <stdio.h>
#include <string.h>
#define MAX_LEN 6
const char *number_table[] = {
"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"
};
const char *unit_table[] = {
"", "拾", "佰", "仟", "万"
};
char *Convert(int m) {
static char result[MAX_LEN * 3 + 1]; // 最多能表示5位数,每位最多3个字符
memset(result, 0, sizeof(result)); // 初始化为0
int len = 0;
char str[MAX_LEN + 1]; // 用于存储转换后的字符串
// 将整数m转换为字符串
sprintf(str, "%d", m);
int str_len = strlen(str);
// 逐位处理
for (int i = 0; i < str_len; i++) {
// 取出当前位的数字
int digit = str[i] - '0';
// 将数字转换为大写金额
strcat(result, number_table[digit]);
// 加上单位
strcat(result, unit_table[str_len - i - 1]);
}
return result;
}
int main() {
printf("%s\n", Convert(12345)); // 输出:壹万贰仟叁佰肆拾伍
return 0;
}
```
上述代码首先定义了两个字符串数组 `number_table` 和 `unit_table`,分别用于存储数字的大写表示和单位的大写表示。然后定义了函数 `Convert`,它接收一个整数作为参数,并将其转换为大写金额。代码中使用了 `sprintf` 函数将整
### 回答2:
以下是用C语言编写的完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *Convert(int m) {
const char *digit[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
const char *unit[] = {"","拾","佰","仟","万"};
char *result = malloc(sizeof(char) * 100);
memset(result, 0, sizeof(char) * 100);
int index = 0, zeroCount = 0;
if (m == 0) {
result[index++] = digit[0][0];
result[index++] = '\0';
return result;
}
while (m > 0) {
int num = m % 10;
if (num != 0) {
result[index++] = digit[num][0];
result[index++] = unit[zeroCount][0];
zeroCount = 0;
} else {
zeroCount++;
}
m /= 10;
}
if (zeroCount < 4) {
result[index++] = unit[zeroCount][0];
} else if (index > 0) {
index--;
}
int len = strlen(result);
char *res = malloc(sizeof(char) * (len + 1));
strcpy(res, result);
free(result);
return res;
}
int main() {
int m = 12345;
char *res = Convert(m);
printf("%s\n", res);
free(res);
return 0;
}
### 回答3:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* Convert(int m);
int main() {
int m;
printf("请输入一个五位数的整数:");
scanf("%d", &m);
char* result = Convert(m);
printf("%s\n", result);
free(result);
return 0;
}
char* Convert(int m) {
char* chinese = (char*)malloc(sizeof(char) * 50);
char* digits[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
char* units[] = {"", "拾", "佰", "仟", "万"};
int length = snprintf(NULL, 0, "%d", m);
int i = 0;
int flag = 0;
while (m > 0) {
int digit = m % 10;
char* unit = units[i % 5];
if (digit != 0) {
if (flag == 1) {
strcat(chinese, "零");
}
strcat(chinese, digits[digit]);
strcat(chinese, unit);
flag = 0;
} else {
flag = 1;
}
m = m / 10;
i++;
}
int chineseLength = strlen(chinese);
char* result = (char*)malloc(sizeof(char) * (chineseLength + 1));
strcpy(result, chinese);
result[chineseLength] = '\0';
free(chinese);
return result;
}
```
注意:该代码仅实现了将整数转换为大写金额的功能,输入需要保证为五位数的整数才能正确输出结果。
优化这段代码 int Lcd_Modify_Param(int ikey,unsigned char mode,int _boardid,int gapid,int ioa,int digit) { float param; int len; int index = digit - 1; const float add_arr[3][8] = { {pow(10,0), 0 ,pow(10,-1),pow(10,-2), pow(10,-3),pow(10,-4)}, {pow(10,1),pow(10,0), 0 , pow(10,-1), pow(10,-2),pow(10,-3),pow(10,-4)}, {pow(10,2),pow(10,1),pow(10,0), 0 , pow(10,-1),pow(10,-2),pow(10,-3),pow(10,-4)} }; if(mode == ALTER_RUNPARAM) param = get_RunParaInfo_val(_boardid,gapid,ioa); else if (mode == ALTER_PROTECT) param = get_ActionDZInfo_val(_boardid,gapid,ioa); else if (mode == ALTER_SERI) param = gRunPara.COMMS_SerialInfo[gapid][ioa].val; if ((mode == ALTER_SERI) || (mode == ALTER_PROTECT&&(ioa == RT1064KZZ_UAB_CH || ioa == RT1064KZZ_UBC_CH || ioa == RT1064_DZ_CHZCS))) { printf("szName:%s\n",gRunPara.gap_ActionDZInfo[gapid][ioa].szName); param = SetInteger(ikey,param,digit); printf("param:%f\n", param); } else { len = snprintf(NULL, 0, "%0.3f", param); // 获取字符串长度 char buf[len+1]; // 创建缓冲区 snprintf(buf, len+1, "%0.3f", param); // 将浮点数转换为字符串 if (ikey == LCD_KEY_ADD) { if (len >= 5 && len <= 7 && index >= 0 && index <= 7) param += add_arr[len-5][index]; } else if(ikey == LCD_KEY_DECREASE) { if (len >= 5 && len <= 7 && index >= 0 && index <= 7) param -= add_arr[len-5][index]; } } if (param >= 0) { if(mode == ALTER_RUNPARAM) { if (_boardid == UNIT_PUBLIC_MX6) { if(gRunPara.ALLptRunParaInfo[ioa].IDbyBoard == 0) { if(gRunPara.ALLptRunParaInfo[ioa].IDbyPt < MX6RUN_TOTALSUM) { gRunPara.pub_RunParaInfo[gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val= param; } } else { if (gRunPara.ALLptRunParaInfo[ioa].IDbyPt != RT1064KZZ_PTDX && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RUN_INNER_PARA_SIZE) { gRunPara.gap_RunParaInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val= param; } else if ((gRunPara.ALLptRunParaInfo[ioa].IDbyPt == RT1064KZZ_PTDX || gRunPara.ALLptRunParaInfo[ioa].IDbyPt >= RT1064_DZ_YY) && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RT1064_YS_TOTALSUM) //--四个参数在 内部动作参数区 { gRunPara.gap_ActionDZInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val= param; } } } else if (_boardid == UNIT_GAP_RT1064) gRunPara.gap_RunParaInfo[gapid][ioa].val= param; } else if (mode == ALTER_PROTECT) { if (_boardid == UNIT_PUBLIC_MX6) gRunPara.pub_ActionDZInfo[ioa].val = param; else { if (param <= 999999) gRunPara.gap_ActionDZInfo[gapid][ioa].val = param; } } else if (mode == ALTER_SERI) { if (param <= 999999) gRunPara.COMMS_SerialInfo[gapid][ioa].val = param; } } return 1; }
1. 避免在函数内部定义变长数组。这里使用了变长数组来存储字符串,但是它的长度是在运行时才确定的,会占用大量的栈空间,容易引发栈溢出问题。可以使用动态分配内存来代替。
2. 将重复的代码抽象为函数,提高代码的可读性和可维护性。例如,多次使用了 `SetInteger` 函数,可以将其封装为一个单独的函数。
3. 将常量提取为全局变量或宏定义,提高代码的可维护性和可读性。
4. 使用更有意义的变量名,增加代码的可读性。例如,`ikey` 可以改为 `key`,`gapid` 可以改为 `gap_id` 等等。
5. 使用 `switch` 语句来替代多个 `if-else` 语句,提高代码的可读性。
下面是优化后的代码:
```c
#define FLOAT_PRECISION 0.001
#define MAX_FLOAT_LENGTH 10
typedef enum {
ALTER_RUNPARAM,
ALTER_PROTECT,
ALTER_SERI
} AlterMode;
typedef enum {
LCD_KEY_ADD,
LCD_KEY_DECREASE
} KeyType;
float add_arr[3][8] = {
{pow(10,0), 0 ,pow(10,-1), pow(10,-2), pow(10,-3), pow(10,-4)},
{pow(10,1), pow(10,0), 0 , pow(10,-1), pow(10,-2), pow(10,-3), pow(10,-4)},
{pow(10,2), pow(10,1), pow(10,0), 0 , pow(10,-1), pow(10,-2), pow(10,-3), pow(10,-4)}
};
float get_param(AlterMode mode, int board_id, int gap_id, int ioa) {
switch (mode) {
case ALTER_RUNPARAM:
return get_RunParaInfo_val(board_id, gap_id, ioa);
case ALTER_PROTECT:
return get_ActionDZInfo_val(board_id, gap_id, ioa);
case ALTER_SERI:
return gRunPara.COMMS_SerialInfo[gap_id][ioa].val;
default:
return 0.0;
}
}
void set_param(AlterMode mode, int board_id, int gap_id, int ioa, float param) {
if (param >= 0) {
switch (mode) {
case ALTER_RUNPARAM:
if (board_id == UNIT_PUBLIC_MX6) {
if (gRunPara.ALLptRunParaInfo[ioa].IDbyBoard == 0) {
if (gRunPara.ALLptRunParaInfo[ioa].IDbyPt < MX6RUN_TOTALSUM) {
gRunPara.pub_RunParaInfo[gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val = param;
}
} else {
if (gRunPara.ALLptRunParaInfo[ioa].IDbyPt != RT1064KZZ_PTDX && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RUN_INNER_PARA_SIZE) {
gRunPara.gap_RunParaInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val = param;
} else if ((gRunPara.ALLptRunParaInfo[ioa].IDbyPt == RT1064KZZ_PTDX || gRunPara.ALLptRunParaInfo[ioa].IDbyPt >= RT1064_DZ_YY) && gRunPara.ALLptRunParaInfo[ioa].IDbyPt < RT1064_YS_TOTALSUM) {
gRunPara.gap_ActionDZInfo[1][gRunPara.ALLptRunParaInfo[ioa].IDbyPt].val = param;
}
}
} else if (board_id == UNIT_GAP_RT1064) {
gRunPara.gap_RunParaInfo[gap_id][ioa].val = param;
}
break;
case ALTER_PROTECT:
if (board_id == UNIT_PUBLIC_MX6) {
gRunPara.pub_ActionDZInfo[ioa].val = param;
} else {
if (param <= 999999) {
gRunPara.gap_ActionDZInfo[gap_id][ioa].val = param;
}
}
break;
case ALTER_SERI:
if (param <= 999999) {
gRunPara.COMMS_SerialInfo[gap_id][ioa].val = param;
}
break;
default:
break;
}
}
}
float modify_param(KeyType key, AlterMode mode, int board_id, int gap_id, int ioa, int digit) {
float param = get_param(mode, board_id, gap_id, ioa);
if ((mode == ALTER_SERI) || (mode == ALTER_PROTECT && (ioa == RT1064KZZ_UAB_CH || ioa == RT1064KZZ_UBC_CH || ioa == RT1064_DZ_CHZCS))) {
printf("szName:%s\n",gRunPara.gap_ActionDZInfo[gap_id][ioa].szName);
param = SetInteger(key, param, digit);
printf("param:%f\n", param);
} else {
char buf[MAX_FLOAT_LENGTH];
snprintf(buf, MAX_FLOAT_LENGTH, "%0.3f", param);
int len = strlen(buf);
int index = digit - 1;
if (key == LCD_KEY_ADD) {
if (len >= 5 && len <= 7 && index >= 0 && index <= 7) {
param += add_arr[len-5][index];
}
} else if(key == LCD_KEY_DECREASE) {
if (len >= 5 && len <= 7 && index >= 0 && index <= 7) {
param -= add_arr[len-5][index];
}
}
}
set_param(mode, board_id, gap_id, ioa, param);
return param;
}
```