c - 计算错误,用+代替-
问题描述
出于培训原因,我想编写一个小计算器。为什么计算 10-6 = 16 而不是 10-6 = 4?
我得到了错误:
Assertion Failed!
Expression: calc("10-6") == 4 && "could not do substraction"
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <ctype.h>
double calc(char * input);
double extract_first_integer(char * input);
double extract_last_integer(char * input);
char extract_operand(char * input);
int main()
{
assert(calc("10-6") == 4 && "could not do substraction");
return 0;
}
double calc(char * input){
double num1 = extract_first_integer(input);
double num2 = extract_last_integer(input);
char operand = extract_operand(input);
printf("operand is %c\n", operand);
switch (operand)
{
case '-':
printf("num1 - num2: %f\n", num1-num2); // output: 16 instead of 4
return num1 - num2;
break;
}
}
double extract_first_integer(char * input){
char *str = input, *p = str;
double val;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
val = strtol(p, &p, 10); // Read number
return val;
} else {
// Otherwise, move on to the next character.
p++;
}
}
}
double extract_last_integer(char * input){
char *str = input, *p = str;
double val;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
val = strtol(p, &p, 10); // Read number
} else {
// Otherwise, move on to the next character.
p++;
}
}
return val;
}
char extract_operand(char * input){
if (strstr(input, "-")) return '-';
}
解决方案
在extract_last_integer()
你有
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
val = strtol(p, &p, 10); // Read number
} else {
// Otherwise, move on to the next character.
p++;
}
}
它增加p
直到它遇到第一个数字或-
/+
后跟一个数字。所以它会匹配第一个10
数字。但是请注意,您并没有像早期那样打破循环。当您继续匹配下一个数字时,将匹配in 。显然是 16return val;
extract_first_integer()
-6
"10-6"
10 - (-6)
您也可能有未定义的行为
- 将相同的指针传递给
strtol
. 该变量str
未使用,应改为在str_end
参数中传递 - 将
const char*
("10-6"
) 传递给期望的函数char*
这 3 个extract...
函数的性能也不好,因为它们都需要从输入字符串的开头进行迭代。要解决此问题,您应该返回当前数字的位置并从该位置启动下一个函数。这样,您可以使用相同的函数来解析整数,而不是编写两个
此外,你的名字倒了。这两个整数称为操作数,连接两个操作数的东西称为运算符,而不是操作数。double
当你只读取整数时为什么要返回?
因此,在修复了这些点之后,我们将拥有
int extract_operand(char * input, size_t *lastChar);
char extract_operator(char * input, size_t *lastChar);
size_t lastPos;
int num1 = extract_operand(input, &lastPos);
char operand = extract_operator(input + lastPos, &lastPos);
int num2 = extract_operand(input + lastPos, &lastPos);
推荐阅读
- reactjs - 在反应中使用箭头函数进行绑定
- javascript - addEventListener 在被点击之前执行
- c# - AnyStore1.exe 中出现“System.TypeInitializationException”类型的未处理异常
- oracle - %type 可以与 Object 类型一起使用吗?是否有可能是因为我在尝试这样做时遇到了错误
- android - Android Kotlin - 如何从另一个 Activity 调用 Activity 方法
- amazon-web-services - AWS boto3 - 调用 GetObject 操作时出现 InvalidToken 错误
- php - 将嵌套的 Json 发送到 Symfony 表单
- python - Python Try后不执行Except?
- javascript - 在 javascript 中更改时锚定目标到 iframe 不起作用(在新选项卡中打开)
- javascript - jQuery隐藏切换表单字段