- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
出于培训原因,我想编写一个小计算器。为什么要计算 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
数字。但是请注意,您并没有像 extract_first_integer()
中的早期 return val;
那样打破循环。当您继续匹配下一个数字时,"10-6"
中的 -6
将被匹配。而 10 - (-6)
显然是 16
你也可能有未定义的行为
strtol
。变量 str
未使用,应改为在 str_end
参数中传递const char*
("10-6"
) 传递给需要 char*
的函数3 个extract...
函数的性能也不佳,因为它们都需要从输入字符串的开头开始迭代。要解决此问题,您应该返回当前数字的位置并从该位置开始下一个功能。这样你就可以使用相同的函数来解析整数而不是写两个
此外,您的名字倒置了。这两个整数称为 operands连接两个操作数的东西叫做 operator ,不是操作数。为什么只读取整数时返回 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);
但这只适用于带有 binary operator 的简单情况和 2 个这样的操作数。对于更复杂的情况,您需要 tokenizer将输入流拆分为标记列表
关于计算错误,用+代替-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864631/
我可以只用 JavaScript 编写我的网站,并确保我的代码对任何人隐藏吗?在这方面,Node.js 是否可以像 Apache 一样通过互联网提供商访问? 最佳答案 您的两个问题的答案都是是。 No
正文应仅包含 bool 而不是 json 对象或数据。 我已经尝试将 bool 转换为 JSON 中的类型。 request.httpMethod = "PUT" let sessio
假设我们有这个html内容,我们愿意用正则表达式得到Content1, Content2,.. Content1 Content2 Content3 Content4 如果我使用下面的行 preg_m
1、LUA获取utf8字符串长度 复制代码 代码如下: --- 获取utf8编码字符串正确长度的方法 -- @param str -- @return number f
我刚刚观察到 if 而不是 -> , 我写 =>在函数的类型签名定义中,它不会导致编译时错误。示例代码: mysum :: Num a => [a] => a -- Notice => after t
所以我试图替换字符串中的任何非字母数字字符,包括空格。我找到了一个可行的解决方案,但感觉很糟糕。我不需要两个单独的替换函数来完成此操作,但我不知道如何正确合并它们。我在网上找到的所有文档都没有解决这个
我有一个字符串 'abc.132131.001.3' 。我想将每次出现的 '.' 替换为 '~'. 我用过 str.replace(/[.*?^${}()|[\]\\]/g, "\~$&"); 但是这
我有这个; let subs = []; for ( const item of items ) { // array for ( const sub of item ) { //
考虑下面来自 this AngularJS tutorial 的代码片段: app.factory('Auth', function ($firebaseSimpleLogin, FIREBASE
出于培训原因,我想编写一个小计算器。为什么要计算 10-6 = 16 而不是 10-6 = 4? 我得到了错误: Assertion Failed! Expression: calc("10-6")
代码如下: /// <summary> /// 将指定字符串按指定长度进行剪切, &nbs
假设我有以下示例: 示例一 $('.my_Selector_Selected_More_Than_One_Element').each(function() { $(this).stuff()
自 Flutter 1.12 发布以来,我的以下代码用于重新启动应用程序: final MyAppState state = context.ancestorStateOfType(const Typ
这行是什么意思: bool operator() (const song& s); I am not able to understand that line with operator. Is op
我在使用 mimetype="text/plain"的 django 模板时遇到了一些问题。 首先,url 的 s3 部分以 :80 结尾,然后实际图像 url 以 '%2f' 代替每个斜杠呈现。 o
目前,如果任意(OR)条件为true,.is()的结果将返回true,如何我是否让它使用AND,即仅在满足所有条件时返回true? if ($('#search-form #valid_only').
我用 C 语言创建了一个非常简单的链表程序。 #include #include int main(){ struct Int{ int num; struct
我有以下无法更改的 HTML 输出: link1;;;link 我怎样才能摆脱;所以结果变成: 链接1;链接2 这是我最好的尝试: var test = new String($(this).html
我有以下查询,它给出了正确的结果,但我想使用不存在而不是不存在。 select cust_name from customer where cust_id not in (select c
我使用 SilverStripe 3.5.6 进行自定义搜索,它将所有关键字分解为一个数组,并且仅返回包含所有单词的结果,而不返回包含其中一个单词的结果。 这只是脚本的一小部分,但这就是我使用过滤器功
我是一名优秀的程序员,十分优秀!