如何在堆栈中输入2位数或更多(包括后缀功能) [英] How to push 2 digits or more in a stack (infix to postfix funtion included)

查看:89
本文介绍了如何在堆栈中输入2位数或更多(包括后缀功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被分配制作一个计算器,在使用堆栈(推送,弹出函数)的同时评估并将公式从中缀转换为后缀。

我已完成大部分代码(它运行但仍然需要一些工作)



但是我不知道如何让它在堆栈中推送2位或更多位并评估它们像234 + 15 * 12





I am assigned to make a calculator that evaluates and convert the equation from infix to postfix while using a stack (push, pop functions).
I've finished most of the code (it runs but still need some work)

but I have no idea how to make it push 2 or more digits in the stack and evaluate them Like 234+15*12


#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>



int InfixToPostfix();
void push(char);
char pop();
void calculate();
int priority(char);
char stack[20];
int top=-1;
char postfix[20];

int main()
{
	while(1)
		calculate();
	getch();
	return 0;
}

int InfixToPostfix()
{	
	char infix[20],ch;
	int k=0,size;
    printf("----------------------------------------------\nPlease enter the value you wish to calculate\n");
	gets(infix);
	size=strlen(infix);
	for(int i =0;i<size;i++)
	{
		ch=infix[i];
		if(isdigit(ch))
			postfix[k++]=ch;
		else if(ch=='(')
			push(ch);
		else if (ch==')')
		{
			while((stack[top] != '('))
				postfix[k++]= pop();
			char remove = pop();
		}
		else 
		{
			while(priority(stack[top]) >= priority(ch))
				postfix[k++]=pop();
			   push(ch);
		}
	}
	while(top != -1)
		postfix[k++]=pop();
	    postfix[k] = '\0'; 
        printf("\nGiven Infix Expn: %s  Postfix Expn: %s\n", infix, postfix);
		return postfix[k];
}

void push(char x)
{
	stack[++top]=x;
}

char pop()
{
	return stack[top--];
}
void calculate()
	{
		int i=0;
		int op1,op2;
		char z;
		InfixToPostfix();
		do{

		    z=postfix[i++];
			if(isdigit(z))
				push(z - 48); 
				

			else{
	    switch(z)
	  {
	    case '+':
			op1=pop();
	        op2=pop();
	        
	        push(op2+op1);
	        break;

	    case '-':

	        op1=pop();
	        op2=pop();
	        
	        push(op2-op1);
	        break;

	    case '*':

	        op1=pop();
	        op2=pop();
	        
	        push(op2*op1);
	        break;

	    case '/':
			
	        op1=pop();
	        op2=pop();
	        
	        push(op2/op1);
	        break;
		case '^':
		    op1=pop();
			op2=pop();
			push((int)pow((double)op2,op1));
			break;
	      }
    	}
	}while(z != '\0');
		 
  printf("\n Result after Evaluation: %d\n", stack[top]);
}

int priority(char x)
{
	if(x=='(')
		return 0;
	if(x== '+'|| x=='-')
		return 1;
	if(x=='*' || x=='/')
		return 2;
	if(x=='^')
	return 3;
	return -1;
	
}





我的尝试:



在互联网上看我猜



What I have tried:

Looking on the internet i guess

推荐答案

你不推数字:你将它们转换为数字并推送它们。

基本上,你正在做的是标记用户输入:

You don't push digits: you convert them to numbers and push those.
Basically, what you are doing is "tokenizing" the user input:
234+15*12

成为5个代币:

Becomes 5 tokens:

Number  :  234
Operator:    +
Number  :   15
Operator:    *
Number  :   12

所以你需要把五个物品推到你的堆栈上。

看看这个: 3.9。中缀,前缀和后缀表达式 - 使用算法和数据结构解决问题 [ ^ ] - 一旦您对输入进行了标记化,它就会显示处理此算法的算法。

So there are five items you need to push onto your stack.
Have a look at this: 3.9. Infix, Prefix and Postfix Expressions — Problem Solving with Algorithms and Data Structures[^] - it shows the algorithm for handling this once you have tokenized the input.


这篇关于如何在堆栈中输入2位数或更多(包括后缀功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆