- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
大家好!我是 C++ 的新手(这里也是 stackoverflow 的新手),我需要各位专家的帮助。
我这里有一个代码,它应该向用户询问中缀表达式,然后将其转换为后缀并输出结果(后缀计算器)。但是,我无法立即将后缀转换为输出,因此一旦它显示后缀表达式,它就会在输出真正的答案之前再次请求后缀表达式(例如 1 2 + 之后有空格)。
没有错误或警告,但是当我运行程序时,计算机在显示后缀表达式后说“file.exe 已停止工作”。所以程序能够正确地将中缀表达式转换为后缀表达式,但在显示输出时仍然存在一些错误。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct node {
char data;
node *next;
};
node *top=NULL;
node *bottom=NULL;
node *key;
node *last;
node *before_last;
void push (const char Symbol) {
key = new node;
key->data = Symbol;
key->next = top;
top = key;
}
void push_for_output (node* &stack, int key) {
node* newNode = new node;
newNode->data = key;
newNode->next = stack;
stack = newNode;
}
const char pop() {
if (!top) {
cout << "Stack underflow\n" << endl;
return ' ';
}
node* key = top;
top = top->next;
char ch = key->data;
delete key;
return ch;
}
int pop_for_output (node* &stack) {
int key = stack->data;
node* nodeToDelete = stack;
stack = stack->next;
delete nodeToDelete;
return key;
}
bool isOperator (char *token) {
if (strcmp(token, "+") == 0) {
return true;
}
else if (strcmp(token, "-") == 0) {
return true;
}
else if (strcmp(token, "*") == 0) {
return true;
}
else if (strcmp(token, "/") == 0) {
return true;
}
else {
return false;
}
}
const bool is_empty() {
return !top;
}
int postfix(const char *infix) {
char infix_ch[100]={NULL};
char postfix_ch[100]={NULL};
node* stack = NULL;
strcpy(infix_ch,"(");
strcat(infix_ch, infix);
strcat(infix_ch,")");
char symbol[5]={NULL};
char temp[5]={NULL};
for(int i=0; i<strlen(infix_ch); i++) {
symbol[0]=infix_ch[i];
if(symbol[0]=='(')
push(symbol[0]);
else if(symbol[0]==')') {
symbol[0]=pop( );
while(symbol[0]!='(') {
strcat(postfix_ch, symbol);
symbol[0]=pop( );
}
}
else if(symbol[0]=='^' || symbol[0]=='*' || symbol[0]=='/' || symbol[0]=='+' || symbol[0]=='-') {
if(symbol[0]=='*' || symbol[0]=='/') {
temp[0]=pop( );
while(temp[0]=='^' || temp[0]=='*' || temp[0]=='/') {
strcat(postfix_ch, temp);
temp[0]=pop( );
}
push(temp[0]);
}
else if(symbol[0]=='+' || symbol[0]=='-') {
temp[0]=pop( );
while(temp[0]!='(') {
strcat(postfix_ch, temp);
temp[0]=pop( );
}
push(temp[0]);
}
push(symbol[0]);
}
else
strcat(postfix_ch, symbol);
}
cout << "Postfix: " << postfix_ch;
char postfix[80];
cout << "\nEnter postfix expression (include spaces between each operand and/or operator): ";
cin.getline(postfix, 80);
char *tokens = strtok(postfix, " ");
while (tokens != NULL) {
if (isOperator (tokens)) {
int operand2 = pop_for_output(stack);
int operand1 = pop_for_output(stack);
int result;
if (strcmp(tokens, "+") == 0) {
result = operand1 + operand2;
}
else if (strcmp(tokens, "-") == 0) {
result = operand1 - operand2;
}
else if (strcmp(tokens, "*") == 0) {
result = operand1 * operand2;
}
else if (strcmp(tokens, "/") == 0) {
result = operand1 / operand2;
}
push_for_output (stack, result);
}
else {
push_for_output (stack, atoi (tokens));
}
tokens = strtok(NULL, " ");
}
cout << pop_for_output(stack);
system("pause");
return 0;
}
int main( ) {
char infix_values[100]={NULL};
cout << "Enter the infix equation: ";
cin >> infix_values;
postfix(infix_values);
}
我是新手,我真的需要各位专家的帮助。如果你帮我改正我的程序,我将不胜感激。非常感谢,祝您有愉快的一天!
最佳答案
一个可能的问题是 pop_for_output()
函数永远不会像您在 pop()
中那样检查空/NULL 堆栈。如果输入了无效的后缀表达式,或者如果您的解析不正确,您很容易陷入引用 NULL 指针的情况,这可以很好地解释崩溃。
关于c++ - Infix to Postfix to Output (Postfix Calculator) 使用堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803859/
关闭。这个问题是 off-topic 。它目前不接受答案。 想改进这个问题? Update the question 所以它是 Stack Overflow 的 on-topic。 9年前关闭。 Im
我使用 postfix 作为“myDomain.com”的邮件服务器。此外,我已经为 gmail 安装了转发服务。到目前为止效果很好。但是当有人向我发送电子邮件时,例如一个可执行文件,gmail 会退
我已经回答了我自己的问题,稍后可能会更新这个问题以反射(reflect)我的出发点/我为获得解决方案所采取的步骤,但我想我会问一个我开始的问题和采取的结果我花费了不合理的大量时间进行研究,反复试验。请
是否有可能在 /etc/postfix/recipient_bcc_maps 中有多个收件人? ? /etc/postfix/recipient_bcc_maps : source@domain.tl
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3年前关闭。 Improve thi
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve th
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
似乎我的全能别名不起作用。 当我使用 postmap -q test@example.org mysql:/etc/postfix/mysql-virtual-alias-maps.cf 没有输出。
我正在编写一个程序,它读取 Infix 表示法,将其转换为 Postfix,然后评估该 Postfix。这是我的程序: #include #include #define SIZE 50
大家好!我是 C++ 的新手(这里也是 stackoverflow 的新手),我需要各位专家的帮助。 我这里有一个代码,它应该向用户询问中缀表达式,然后将其转换为后缀并输出结果(后缀计算器)。但是,我
我正在尝试让 postfix 将邮件发送到 gmail。我已经按照这篇文章进行了配置,但仍然给我一个错误: relay=smtp.gmail.com[173.194.66.108]:587, dela
我正在使用 postfix v3.3.0 设置新服务器 (Ubuntu 18.04.1 LTS)。 我遵循了 postfix 到只发送设置的标准部署。 http://www.postfix.org/S
我的客户是一家旅行社,有数千封选择加入的电子邮件(没有垃圾邮件)。他们每周向所有客户发送一次消息,但我遇到了一些供应商的问题,我想对所有传出域的整个服务器进行速率限制。 我发现一些配置可以限制每个域每
我一直在尝试设置 postfix,以便它可以向我网站上的用户发送(不需要接收)电子邮件(例如帐户验证电子邮件)。 我通过 mailutils 安装 postfix(安装 postfix 和其他东西)。
突然我打破了我的后缀: 我正在运行,CentOs,Postfix,Dovecot。 我的 Postfix main.cf 文件:http://pastebin.com/STLNRYUK hostnam
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我不确定我这样做是否正确。我正在尝试将中缀方程转换为后缀方程,例如: (3 + 4) * 2 在后缀中是: 4 3 + 2 * 如果可能的话,我试图用一种方法来完成这一切。 现在我收到一个 array
我是一名优秀的程序员,十分优秀!