c - 有什么方法可以使用 strtof() 来转换“负数”?
问题描述
我正在尝试编写一个函数来将字符串转换为数学运算。
int RPN_Evaluate(char * rpn_string, double * result)
{
struct Stack calculate = {};
char *token;
char *endptr;
double x,y;
token = strtok(rpn_string, " ");
while (token != NULL) {
*number = strtof(token, &endptr);
if (*token == '+') {
StackPop(&calculate, &y);
StackPop(&calculate, &x);
result_operation = x + y;
StackPush(&calculate, result_operation);
} else if (*token == '-') {
StackPop(&calculate, &y);
StackPop(&calculate, &x);
result_operation = x - y;
StackPush(&calculate, result_operation);
}else if (*endptr != NULL) {
return RPN_ERROR_INVALID_TOKEN;
} else if (*endptr == NULL) {
StackPush(&calculate, *number);
}
token = strtok(NULL, " ");
}
StackPop(&calculate, result);
return RPN_NO_ERROR;
}
如果我传入 "1 1 +" ,它工作得很好。但是,当我传入“2 -1 +”时遇到问题有什么办法可以使用 strtof() 将“-1”转换为 -1?strtof() 似乎不会自动执行此操作。
解决方案
推荐阅读
- php - 字符到整数值的转换(或有时返回数组)
- python - Python:将音频频谱数据转换为频率平均-scipy
- macros - Jinja2:如何编写宏以有条件地包含内容
- r - 将特定选项卡从 Excel 读入 R
- c# - LinqtoSQL 带回来只需要
- amazon-redshift - RedShift:函数 json_extract_array_element_text(super, integer, boolean) 不存在
- c# - 如何从 Utf8JsonReader 获取属性路径?
- spring-boot - 为什么我的两个类似的 CustomExceptionHandler 类返回两种不同的格式,其中 1 向 UI 抛出异常?
- etl - ETL 测试中数百万行的数据验证
- python - Tensorflow 2 在最后 x 次调用中获得“WARNING:tensorflow:x
触发了 tf.function 回溯。”