- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码遇到问题。它似乎只适用于单位数字 int。我不知道如何创建一个适用于大于 9 的 int 的函数。此外,我不知道如果字符串为空,如何结束程序。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//stack type
struct node
{
int head;
int *array;
unsigned capacity;
};
struct node* createNode();
int isEmpty();
char pop();
void push(struct node* stack, char op);
int evaluatePostfix();
int main() {
char exp[1000]; // = "5 1 2 + 4 * + 3 -";
printf("Input string:\t");
fgets(exp, 1000, stdin);
for(int i = 1 ; i <= strlen(exp); i++) {
if(exp[i] == '\n') {
exp[i] = '\0';
}
else if (exp[0] == '\n') {
printf("stack is empty\n");
exit(0);
}
}
printf ("%s = %d\n", exp, evaluatePostfix(exp));
return 0;
}
struct node* createNode(unsigned capacity) {
struct node* stack = (struct node*) malloc(sizeof(struct node));
if (!stack) return NULL;
(*stack).head = -1;
(*stack).capacity = capacity;
(*stack).array = (int*) malloc((*stack).capacity *sizeof(int));
if (!(*stack).array) return NULL;
return stack;
}
int isEmpty(struct node *stack) {
return (*stack).head == -1 ;
}
char pop(struct node* stack) {
if (!isEmpty(stack))
return (*stack).array[(*stack).head--] ;
return '$';
}
void push(struct node* stack, char op) {
(*stack).array[++(*stack).head] = op;
}
// The main function that returns value of a given postfix expression
int evaluatePostfix(char* exp) {
// Create a stack of capacity equal to expression size
struct Stack* stack = createStack(strlen(exp));
struct node *stack = createNode(strlen(exp));
if (!stack) return -1;
// Scan all characters one by one
for (int i = 0; exp[i]; ++i){
// If the scanned character is an operand or number,
// push it to the stack.
if ((exp[i])== ' ') continue;
else if (isdigit(exp[i]))
push(stack, exp[i] - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int val1 = pop(stack);
int val2 = pop(stack);
switch (exp[i])
{
case '+': push(stack, val2 + val1); break;
case '-': push(stack, val2 - val1); break;
case '*': push(stack, val2 * val1); break;
case '/': push(stack, val2/val1); break;
}
}
}
return pop(stack);
}
最佳答案
我无法为您写出全部内容,但可以为您指明正确的方向。首先,当有人说“库函数 XYZ() 对你有帮助”时,你应该去阅读手册页中有关该函数的信息。例如,从 Linux shell 运行:man atoi
以了解 atoi
函数。
对于您的特定问题,它可以归结为解析字符串并将其转换为数字和运算符。因此,一些有用的库函数是:
strtok
:从较长的字符串中提取分隔的字符串标记。您可以使用它来获取每个单独的输入。atoi
:这可以将数字的字符串表示形式转换为其等效的整数。不允许进行错误检查。strtol
:可以执行与 atoi
相同的操作(以及更多),并且还允许进行错误检查。考虑到这些信息,下面是一个可能对您有用的代码片段:
int evaluatePostfix(char* exp)
{
char *token;
long int number;
/* strtok will keep extracting the next token delimited by space */
while (token = strtok(exp, " ")) {
/* Now parse the token and process it */
if (is_operator(token)) {
/* do operator processing */
} else {
number = strtol(token, NULL, 10);
/* do number processing */
}
}
/* strtok returns NULL when no more tokens. So
we are done when the while loop exits */
}
请注意,上面的代码不会对 strtol
进行错误检查。您可能想这样做。阅读 strtol
的手册页以了解如何对其进行错误检查。
关于c - 使用链表的 RPN 计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020956/
我尝试将中缀表示法表达式转换为后缀表示法 (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 字符,但我的代码仍然不起作用:
我是一名优秀的程序员,十分优秀!