使用stacks和C语言的Postfix评估
我在这里有一段时间遇到了一个类似的问题,但我认为这个问题是错误的。为了给大家一点背景知识,我的任务是创建一个C程序来解决表单中的后缀表达式 8 7-9*= 我认为我的问题是,我的教授给出了一些不正确的堆栈代码。我之所以这样说,是因为我经常遇到堆栈溢出(lol)错误,而我的堆栈还远远没有满。如果有帮助的话,我正在使用VisualStudio2005。这是我的密码:使用stacks和C语言的Postfix评估,c,postfix-notation,C,Postfix Notation,我在这里有一段时间遇到了一个类似的问题,但我认为这个问题是错误的。为了给大家一点背景知识,我的任务是创建一个C程序来解决表单中的后缀表达式 8 7-9*= 我认为我的问题是,我的教授给出了一些不正确的堆栈代码。我之所以这样说,是因为我经常遇到堆栈溢出(lol)错误,而我的堆栈还远远没有满。如果有帮助的话,我正在使用VisualStudio2005。这是我的密码: #include <stdio.h> ` #include <stdlib.h> #de
#include <stdio.h>
` #include <stdlib.h>
#define STACK_SIZE 20
typedef int Bit;
Bit contents[STACK_SIZE];
int top = 0;
void make_empty(void);
int is_empty(void);
int is_full(void);
void push(Bit i);
int pop(void);
void stack_overflow(void);
void stack_underflow(void);
int main(void) {
Bit bit;
char operation;
int operand;
Bit current;
int result;
while(scanf("%d",¤t)!= '=')
{
push(current);
}
scanf("%c", &operation);
while(operation != '=')
{
scanf("%d", &operand);
printf("%d\n",top);
//Pushes any number into the stack
if(operand==1||operand==2||operand==3||operand==4||operand==5||operand==6||operand==7||operand==8||operand==9||operand==0)
{
printf("entered number loop\n");
bit = operand;
if(top==20)
{
stack_overflow();
}
push(&bit);
}
//Performs subtraction operation
else if(operation == '-')
{
printf("entered minus loop\n");
if(top==1)
{
stack_underflow();
}
result = pop() - pop();
bit = result;
if(top==20)
{
stack_overflow();
}
push(&bit);
}
//Performs addition operation
else if(operation == '+')
{
if(top==1)
{
stack_underflow();
}
result = pop() + pop();
bit = result;
if(top==20)
{
stack_overflow();
}
push(&bit);
}
//Performs multiplication operation
else if(operation == '*')
{
if(top==1)
{
stack_underflow();
}
result = pop() * pop();
bit = result;
if(top==20)
{
stack_overflow();
}
push(&bit);
}
//Performs division operation
else if(operation == '/')
{
if(top==1)
{
stack_underflow();
}
result = pop() / pop();
bit = result;
if(top==20)
{
stack_overflow();
}
push(&bit);
}
else if(operation == '=')
{
if(top==0)
{
stack_underflow();
}
printf("%d\n",pop());
break;
}
}
return 0;
}
void make_empty(void) {
top = 0;
}
int is_empty(void) {
return top == 0;
}
int is_full(void) {
return top == STACK_SIZE;
}
void push(Bit i) {
if (is_full())
stack_overflow();
else
contents[top++] = i;
}
int pop(void) {
if (is_empty())
stack_underflow();
else
return contents[top--];
}
void stack_overflow(void) {
printf("Error: stack overflow!\n");
exit(EXIT_FAILURE);
}
void stack_underflow(void) {
printf("Error: stack underflow!\n");
exit(EXIT_FAILURE);
}
#包括
`#包括
#定义堆栈大小20
typedef int位;
位内容[堆栈大小];
int-top=0;
void使_为空(void);
int为空(void);
整数为全(空);
无效推力(位i);
int pop(无效);
无效堆栈溢出(无效);
空堆下溢(空);
内部主(空){
比特比特;
字符操作;
整数操作数;
位电流;
int结果;
而(扫描频率(“%d”,¤t)!='=')
{
推动(电流);
}
scanf(“%c”,操作(&O);
while(操作!='=')
{
scanf(“%d”,操作数(&O);
printf(“%d\n”,顶部);
//将任意数字推入堆栈
如果(操作数==1 | |操作数==2 | |操作数==3 | |操作数==4 | |操作数==5 | |操作数==6 |操作数==7 |操作数==8 | |操作数==9 |操作数==0)
{
printf(“输入的数字循环\n”);
位=操作数;
如果(顶部==20)
{
堆栈溢出();
}
推送(&bit);
}
//执行减法运算
else if(操作=='-')
{
printf(“输入的负循环\n”);
如果(顶部==1)
{
堆栈_下溢();
}
结果=pop()-pop();
位=结果;
如果(顶部==20)
{
堆栈溢出();
}
推送(&bit);
}
//执行加法运算
else if(操作=='+')
{
如果(顶部==1)
{
堆栈_下溢();
}
结果=pop()+pop();
位=结果;
如果(顶部==20)
{
堆栈溢出();
}
推送(&bit);
}
//执行乘法运算
else if(操作=='*')
{
如果(顶部==1)
{
堆栈_下溢();
}
结果=pop()*pop();
位=结果;
如果(顶部==20)
{
堆栈溢出();
}
推送(&bit);
}
//执行除法运算
else if(操作=='/'))
{
如果(顶部==1)
{
堆栈_下溢();
}
结果=pop()/pop();
位=结果;
如果(顶部==20)
{
堆栈溢出();
}
推送(&bit);
}
else if(操作=='=')
{
如果(顶部==0)
{
堆栈_下溢();
}
printf(“%d\n”,pop());
打破
}
}
返回0;
}
void使_为空(void){
top=0;
}
int为空(void){
返回顶部==0;
}
整数为满(空){
返回顶部==堆栈大小;
}
无效推送(位i){
如果(已满())
堆栈溢出();
其他的
内容[top++]=i;
}
int-pop(无效){
如果(是空的())
堆栈_下溢();
其他的
返回内容[顶部--];
}
无效堆栈溢出(无效){
printf(“错误:堆栈溢出!\n”);
退出(退出失败);
}
空烟囱下溢(空){
printf(“错误:堆栈下溢!\n”);
退出(退出失败);
}
现在我意识到我的代码现在有点野蛮,为此我道歉。话虽如此,我们将非常感谢您的任何帮助或意见,并提前向您表示感谢
好吧,在考虑了所有因素之后,我想我已经接近了。所有内容都正确地进入堆栈,并且所有内容都被正确地读取。但是,我的新实现包括将所有内容都设置为字符,然后在需要时转换整数。下面是我的源代码:
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 20
typedef int Bit;
char contents[STACK_SIZE];
int top = 0;
void make_empty(void);
int is_empty(void);
int is_full(void);
void push(char i);
char pop(void);
void stack_overflow(void);
void stack_underflow(void);
int main(void) {
char current = 'a';
char result = 'a';
char operation = 'a';
char char1;
char char2;
int number1;
int number2;
scanf("%c", ¤t);
//While program successfully scanned a number
while(current != '=')
{
//Performs subtraction operation
if(current == '-')
{
printf("entered if 2\n");
char1 = pop();
number1 = char1 - '0';
printf("%d\n", number1);
char2 = pop();
number2 = char2 - '0';
printf("%d\n", number2);
result = number1 - number2;
push(result);
}
//Performs addition operation
else if(current == '+')
{
printf("entered if 2\n");
char1 = pop();
number1 = char1 - '0';
printf("%d\n", number1);
char2 = pop();
number2 = char2 - '0';
printf("%d\n", number2);
result = number1 + number2;
push(result);
}
//Performs multiplication operation
else if(current == '*')
{
printf("entered if 2\n");
char1 = pop();
number1 = char1 - '0';
printf("%d\n", number1);
char2 = pop();
number2 = char2 - '0';
printf("%d\n", number2);
result = number1 * number2;
push(result);
}
//Performs division operation
else if(current == '/')
{
printf("entered if 2\n");
char1 = pop();
number1 = char1 - '0';
printf("%d\n", number1);
char2 = pop();
number2 = char2 - '0';
printf("%d\n", number2);
result = number1 / number2;
push(result);
}
else
{
push(current);
printf("%c\n", current);
}
scanf(" %c", ¤t);
}
//Prints result
printf("%c\n",pop());
return 0;
}
void make_empty(void) {
top = 0;
}
int is_empty(void) {
return top == 0;
}
int is_full(void) {
return top == STACK_SIZE;
}
void push(char i) {
if (is_full())
stack_overflow();
else
contents[top++] = i;
}
char pop(void) {
if (is_empty())
stack_underflow();
else
return contents[top--];
}
void stack_overflow(void) {
printf("Error: stack overflow!\n");
exit(EXIT_FAILURE);
}
void stack_underflow(void) {
printf("Error: stack underflow!\n");
exit(EXIT_FAILURE);
}
#包括
#包括
#定义堆栈大小20
typedef int位;
字符内容[堆栈大小];
int-top=0;
void使_为空(void);
int为空(void);
整数为全(空);
无效推送(chari);
字符pop(void);
无效堆栈溢出(无效);
空堆下溢(空);
内部主(空){
字符电流='a';
字符结果='a';
char操作='a';
char-char1;
char-char2;
整数1;
整数2;
scanf(“%c”,当前(&c));
//而程序成功扫描了一个数字
而(当前!='=')
{
//执行减法运算
如果(当前=='-')
{
printf(“如果输入2\n”);
char1=pop();
number1=char1-“0”;
printf(“%d\n”,编号1);
char2=pop();
number2=char2-'0';
printf(“%d\n”,数字2);
结果=编号1-编号2;
推(结果);
}
//执行加法运算
else if(当前=='+')
{
printf(“如果输入2\n”);
char1=pop();
number1=char1-“0”;
printf(“%d\n”,编号1);
char2=pop();
number2=char2-'0';
printf(“%d\n”,数字2);
结果=1号+2号;
推(结果);
}
//执行乘法运算
else if(当前=='*')
{
printf(“如果输入2\n”);
C
while(scanf("%d",¤t)!= '=')
while (scanf("%d",¤t) == 1)
push(current);
while(scanf("%d",¤t)!= '=') { push(current); }