- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正尝试在 C 中创建一个逆波兰符号解释器,使用使用 struct
实现的堆栈。它应该采用所有个位数的值 (0-9) 和运算符 +
、-
、*
和 /
,并通过退出程序拒绝所有其他人。
我试图捕获整个表达式,因为它是作为字符串输入的,类型为 char,但是当我使用 isdigit()
时,它总是返回一个非零函数(IE ,它不是数字),即使它对用户来说是数字。我相信这是因为字符串是 char 类型的事实,但我认为我不能使用其他任何东西,否则在输入运算符时我会收到错误消息。
错误如下:假设我在函数中输入"11+"
。在调试中,我可以看到这出现在 watch 中。继续执行程序,我看到 isdigit ()
返回了 1 而不是 0,因此 if 语句条件满足,程序以 exit(1) 退出; IDE 没有提供具体的错误消息。
有没有办法实现仅将字符串的“数字”转换为 int 类型,还是我必须做其他事情?
这是函数。虽然它仍然是原始的和未完成的,但它显示了错误:
void parseRPN(TopStack *st)
{
char Input[50];
int i;
do{
printf("please enter an expression in single-digit integers"
"using Reverse Polish notation:");
scanf("%s",&Input);
if (sizeof(Input)/sizeof(int)>=50)
{
printf("that expression was too large for the RPN engine to handle!"
"please break it down into smaller sub-tasks.\n");
fflush(stdin);
continue;
}
break;
}while(true);
for (i=0;i<50;i++)
{
int ErrorDetect=isdigit(Input[i]);
if (ErrorDetect==0 && (Input[i]) != '+' || '-' || '*' || '/')
{
printf("Error: Invalid operand to RPN\nExiting...");
exit(1);
}
else printf("great success!");
}
}
最佳答案
当然是无意中使用了 ||
@J. Piquard也对此发表评论
// if (ErrorDetect==0 && (Input[i]) != '+' || '-' || '*' || '/')
if (ErrorDetect==0 && Input[i] != '+' || Input[i] != '-' ||
Input[i] != '*' || Input[i] != '/')
或者类似的东西。为清楚起见,建议添加 ()
。我认为 OP 需要不同的逻辑。
if (ErrorDetect || (Input[i] != '+' && Input[i] != '-' &&
Input[i] != '*' && Input[i] != '/'))
可能存在其他编码问题,例如:
char Input[50];
// scanf("%s",&Input);
scanf("%49s",&Input);
关于在 C : RPN interpreter 中将字符串的数字成员(char 类型)转换为 int 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40687025/
我尝试将中缀表示法表达式转换为后缀表示法 (RPN)。这是函数: String createRPN(String infix) { Stack *stack = node_alloc(1);
目前,我正在制作一个计算器,输入数学表达式并使用RPn进行计算。因此,我使用中缀到后缀转换器函数来转换它。计算器的工作原理是将数字插入堆栈并检测运算符。但我的计算器有一个缺陷,它无法处理负数除法,例如
我有一个程序应该将表达式转换为反向波兰表示法,然后在执行计算后显示答案。现在它不能正确执行计算。例如,如果我输入 5+2+5,它出于某种原因只注册了 5+2。谁能告诉我我做错了什么? #include
环境安装: pycuda安装: 开源项目信息 fasterRCNN的训练 faster rcnn onnx实践 第3步测试结果: 第4步测试结果: 单张图片测试代码: 多张图片预测代码: 第5步做了修
我的代码遇到问题。它似乎只适用于单位数字 int。我不知道如何创建一个适用于大于 9 的 int 的函数。此外,我不知道如果字符串为空,如何结束程序。 这是我的代码: #include #inclu
我试图解决的一本 C 编程书中的问题是这个,我很难理解,尤其是 pop() 函数在这种情况下是如何工作的: For example, 1 + 2 would be written 1 2 + in R
我正在尝试创建一个简单的 RPN 解析器,它只接受单位数字值和 +-*/运算符。我使用堆栈来存储原始输入,但在打印输出时遇到问题。 当我运行调试时,它给出错误消息“程序收到信号 SIGSEGV,段错误
我正在尝试将数学表达式转换为 RPN,然后对它们执行符号微分,但是我遇到了一些函数,例如 sin() cos() tan()... ln() sqrt() 等。我的表达式解析器仅适用于更简单的情况,例
我正在设计一个程序来对流式数据执行某些操作。操作由运行时提供的 RPN(逆向抛光表示法)表达式定义。数据从文件源流式传输,一次一个固定大小的部分。操作引用一些当前输入的数据部分,例如 data[1]。
我目前正在为一个必须通过一堆整数和各种函数运行的类构建 RPN 计算器。它还必须通过 cin 语句获取输入,然后将其分类为整数或操作数,然后将其压入堆栈或从类中启动适当的函数进行计算。 其中大部分我已
你好)这是我将中缀表达式转换为后缀表达式的代码,但是我只是不明白如何评估我得到的后缀表达式,我将非常感谢任何提示。我不是要代码,尽管这会有所帮助。 #include #include #inclu
我不明白该怎么做?有人可以解释一下如何将 ac+ ac+* 转换为二叉树形式吗?我需要将此表达式转换为该树的完整括号字符串表示形式。 最佳答案 您需要按照处理后缀输入的方式构建树。但是,当您遇到操作时
我已经实现了以下“tree sizer”,但在某些情况下它会失败,下面的示例在应该返回大小 4 时返回大小 2,任何人都可以帮助我。我已经写了好几次了,没有用,一直失败。 def getRPNdept
我所说的使用是指它在许多计算器中的使用,例如 HP35- 我的猜测(和困惑)是 - postfix 实际上更有效地存储内存 -(所以发表评论 here)。 (混淆 - 两者的评估算法与堆栈相似) 当时
我遇到了一个任务,它让你检查作为参数传递给你的方法/函数的字符串是否是反向波兰表示法意义上的正确语句。它可以包含小写字母、运算符号和整数。有没有比分别读取每个字符并实际尝试计算整个表达式更快的方法来检
有什么方法可以将“正常”数学符号解释为逆波兰符号 (RPN)..? 例如1) 2 + 3*4 - 1 = 234*+1-2) 5 (4-8) = 548- 你可以假设遵循 BODMAS 规则并且必须首
给出了原始配方here (您也可以尝试您的程序的正确性)。 附加规则: 1.程序应该从标准输入读取并写入标准输出。 2. 程序应该向调用系统/程序返回零。 3. 程序应使用 gcc -O2 -lm -
我正在尝试使用 foldr 编写一个反向波兰符号评估器,这是我目前所拥有的: step :: [Int] -> String -> [Int] step (x * y):ys "*" = (x:y:y
数学表达式通常用中缀符号表示。出于评估目的,我们可以将其更改为后缀(反向抛光)表示法(使用像 Shunting-Yard 这样的算法),然后使用堆栈评估后缀表示法。 我发现计算器使用这种技术,但是今天
有人可以解释一下为什么我在 myString 上使用 strok() 后总是出现乱码吗?我的输出图片位于底部。我尝试将 myString 中的每个元素初始化为 NULL 字符,但我的代码仍然不起作用:
我是一名优秀的程序员,十分优秀!