栈的应用

一.数制转换

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define SIZE 100
#define Length 10

int e;
typedef int SElemType;
typedef struct
{
	SElemType *base;
	SElemType *top;
	int size;
}Sqstack;

void InitStack(Sqstack &S)
{
	S.base=(SElemType *)malloc(SIZE*sizeof(SElemType));
	if(!S.base) exit(-1);
	S.top=S.base;
	S.size=SIZE;
}

int StackEmpty(Sqstack S)
{
	if(S.top==S.base) return 1;
	else return 0;
}

void push(Sqstack &S,SElemType e)
{
	if(S.top-S.base>=S.size)
	{
		S.base=(SElemType *)realloc(S.base,(SIZE+Length)*sizeof(SElemType));
		if(!S.base)  exit(-1);
		S.top=S.base+S.size;
		S.size+=Length;
	}
	*S.top++=e;
}

void pop(Sqstack &S)
{
	if(S.top==S.base) exit(-1);
	e=(*--S.top);
}


int main ()
{
	Sqstack S;
	int n,d;
	InitStack(S);
	while(~scanf("%d%d",&n,&d))
	{
		while(n)
		{
			push(S,n%d);
			n=n/d;
		}
		while(!StackEmpty(S))
		{
			pop(S);
			printf("%d",e);
		}
		printf("\n");
	}
	return 0;
}

 

  
#include<cstdio>   
#include<cstring>   
#include<stack>
#include<iostream>    
using namespace std;  
 
int main()  
{  
    stack<int>opnd;  
    int n,d; 
    while(cin>>n>>d)
	{
      while(n)
	  {
		opnd.push(n%d);
	 	n=n/d;
	  }
	  while(!opnd.empty())
	  {
	    cout<<opnd.top();
	    opnd.pop();
	  }
    	cout<<endl;
	}
	return 0;  
}  


 

二.表达式求值

  
#include<cstdio>   
#include<cstring>   
#include<stack>
#include<iostream>    
using namespace std;  

int operate(int a,char op,int b)  // 计算
{  
    if(op=='+')  
        return a+b;  
    if(op=='-')  
        return a-b;  
    if(op=='*')  
        return a*b;  
    if(op=='/')  
        return a/b;  
}
  
char precede(char a,char b)    // 比较优先级
{  

	if(a=='+'||a=='-') 
   {
     if(b=='+'||b=='-'||b==')'||b=='#') return '>';
	 if(b=='*'||b=='/'||b=='(') return '<';
   }

   if(a=='*'||a=='/')
   {
     if(b=='+'||b=='-'||b==')'||b=='*'||b=='/'||b=='#') return '>';
	 if(b=='(') return '<';
   }
	  
	if(a=='(')
	{
	 if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(') return '<';
	 if(b==')') return '=';
	}

	if(a==')')
      if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b==')') return '>';

	if(a=='#')
	 if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b=='(') return '<';

}  

int In(char a)  
{  
    if(a=='+'||a=='-'||a==')'||a=='('||a=='*'||a=='/'||a=='#')  
        return 0;  
    return 1;  
}  

 
int main()  
{  
    stack<int>opnd;  
    stack<char>optr;  
    char c,ans;  
    int num,flag;  
    
    num=flag=0;  
    optr.push('#');  
    c=getchar(); 
	
    while(c!='#'||optr.top()!='#')  
    {  
        if(In(c))  
        {  
            flag=1;  
            num=num*10+c-'0';   // 处理多位数
            c=getchar();  
        }  
        else  
        {  
           if(flag==1)  
           {  
                flag=0;  
                opnd.push(num);   //数字(多位数)入栈
                num=0;  
           }  
           ans=precede(optr.top(),c);  
           if(ans=='<')  
           {  
               optr.push(c);  
               c=getchar();  
		   }
          if(ans=='=')  
          {  
              optr.pop();  
              c=getchar();  
          }  
          if(ans=='>')  
          {  
             int num1,num2;  
             char op; 
			 
             num2=opnd.top();  
             opnd.pop();  

             num1=opnd.top();  
             opnd.pop();  
             
			 op=optr.top();  
             optr.pop();  
             
			 cout<<num1<<' '<<op<<' '<<num2<<endl;   //运算顺序   
             opnd.push(operate(num1,op,num2));  
           }  
        }  
    }  
    cout<<opnd.top()<<endl;  //最终结果      
    return 0;  
}  


#include<stdio.h>  
#include<stdlib.h>  
#include<malloc.h>  
#include<iostream>  
using namespace std;  
#define SIZE 100  
#define MAX 10  

char x,a,b;  
typedef char SElemType;  
typedef struct  
{  
	SElemType *base;  
	SElemType *top;  
	int size;  
}Sqstack;  

void InitStack(Sqstack &S)  
{  
	S.base=(SElemType *)malloc(SIZE*sizeof(SElemType));  
	if(!S.base) exit(-1);  
	S.top=S.base;  
	S.size=SIZE;  
}  

char GetTop(Sqstack S)  
{  
	char e;  
	if(S.top==S.base) exit(-1);  
	e=*(S.top-1);  
	return e;  
}  

void push(Sqstack &S,SElemType e)  
{  
	if(S.top-S.base>=S.size)  
	{  
		S.base=(SElemType *)realloc(S.base,(SIZE+MAX)*sizeof(SElemType));  
		if(!S.base)  exit(-1);  
		S.top=S.base+S.size;  
		S.size+=MAX;  
	}  
	*S.top++=e;  
}  

void pop(Sqstack &S,SElemType &e)  
{  
	if(S.top==S.base) exit(-1);  
	e=*--S.top;  
}  


bool in(char a)    
{    
	if(a>='0'&&a<='9')  
		return true;  
	return false;  
}   

char pre(char a,char b)  
{  
	
	if(a=='+'||a=='-')   
	{  
		if(b=='+'||b=='-'||b==')'||b=='#') return '>';  
		if(b=='*'||b=='/'||b=='(') return '<';  
	}  
	if(a=='*'||a=='/')  
	{  
		if(b=='+'||b=='-'||b==')'||b=='*'||b=='/'||b=='#') return '>';  
		if(b=='(') return '<';  
	}  
	
	if(a=='(')  
	{  
		if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(') return '<';  
		if(b==')') return '=';  
	}  
	
	if(a==')')  
	{  
		if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b==')') return '>';  
	}  
	if(a=='#')  
	{  
		if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b=='(') return '<';  
	}  
}  

int operate(char a,char op,char b)    
{    
	int c,d;  
	c=a-'0';  
	d=b-'0';  
	
	if(op=='+')    
		return c+d;    
	if(op=='-')    
		return c-d;    
	if(op=='*')    
		return c*d;    
	if(op=='/')    
		return c/d;    
}    

int main ()  
{  
	char c;  
	Sqstack optr;  
	Sqstack opnd;  
	InitStack(optr);    
	push(optr,'#');  
	InitStack(opnd);    
	c=getchar();  
	while(c!='#'||GetTop(optr)!='#')  
	{  
		if(in(c))  
		{  
            push(opnd,c);  
            c=getchar(); 
		}  
		else  
		{  
            switch(pre(GetTop(optr),c))  
            {  
				
            case'<': push(optr,c);  
                c=getchar();  
                break;  
            case'=':pop(optr,c);  
                c=getchar();  
                break;  
            case'>':pop(optr,x);  
                pop(opnd,b);  
                pop(opnd,a);  
                push(opnd,operate(a,x,b)+'0');  
        
                break;  
            }  
		}  
	}  
	cout<<GetTop(opnd)-'0'<<endl;  
	return 0;  
}  



#include<stdio.h>    
#include<stdlib.h>    
#include<malloc.h>    
#include<iostream>    
using namespace std;    
#define SIZE 100    
#define MAX 10    

char x;
int a,b; 
 
typedef char SElemType;    
typedef struct    
{    
	SElemType *base;    
	SElemType *top;    
	int size;    
}Sqstack;              // 存字符型

typedef struct    
{    
	int *base;    
	int *top;    
	int size;    
}Sqstack1;            //存整形


void InitStack(Sqstack &S)    
{    
	S.base=(SElemType *)malloc(SIZE*sizeof(SElemType));    
	if(!S.base) exit(-1);    
	S.top=S.base;    
	S.size=SIZE;    
} 
void InitStack1(Sqstack1 &S)    
{    
	S.base=(int *)malloc(SIZE*sizeof(int));    
	if(!S.base) exit(-1);    
	S.top=S.base;    
	S.size=SIZE;    
}                       // 初始化   

char GetTop(Sqstack S)    
{    
	char e;    
	if(S.top==S.base) exit(-1);    
	e=*(S.top-1);    
	return e;    
}    
int GetTop1(Sqstack1 S)    
{    
	int e;    
	if(S.top==S.base) exit(-1);    
	e=*(S.top-1);    
	return e;    
}                       //取栈顶元素

void push(Sqstack &S,SElemType e)    
{    
	if(S.top-S.base>=S.size)    
	{    
		S.base=(SElemType *)realloc(S.base,(SIZE+MAX)*sizeof(SElemType));    
		if(!S.base)  exit(-1);    
		S.top=S.base+S.size;    
		S.size+=MAX;    
	}    
	*S.top++=e;    
}   
void push1(Sqstack1 &S,int e)    
{    
	if(S.top-S.base>=S.size)    
	{    
		S.base=(int *)realloc(S.base,(SIZE+MAX)*sizeof(int));    
		if(!S.base)  exit(-1);    
		S.top=S.base+S.size;    
		S.size+=MAX;    
	}    
	*S.top++=e;    
}                     // 入栈
                      

void pop(Sqstack &S,SElemType &e)    
{    
	if(S.top==S.base) exit(-1);    
	e=*--S.top;    
}    
void pop1(Sqstack1 &S,int &e)    
{    
	if(S.top==S.base) exit(-1);    
	e=*--S.top;    
}                    // 出栈


bool in(char a)      
{      
	if(a>='0'&&a<='9')    
		return true;    
	return false;    
}     

char pre(char a,char b)    
{    
	
	if(a=='+'||a=='-')     
	{    
		if(b=='+'||b=='-'||b==')'||b=='#') return '>';    
		if(b=='*'||b=='/'||b=='(') return '<';    
	}    
	if(a=='*'||a=='/')    
	{    
		if(b=='+'||b=='-'||b==')'||b=='*'||b=='/'||b=='#') return '>';    
		if(b=='(') return '<';    
	}    
	
	if(a=='(')    
	{    
		if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(') return '<';    
		if(b==')') return '=';    
	}    
	
	if(a==')')    
	{    
		if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b==')') return '>';    
	}    
	if(a=='#')    
	{    
		if(b=='+'||b=='-'||b=='*'||b=='/'||b=='#'||b=='(') return '<';    
	}    
}    

int operate(int c,char op,int d)      
{      
	 
	if(op=='+')      
		return c+d;      
	if(op=='-')      
		return c-d;      
	if(op=='*')      
		return c*d;      
	if(op=='/')      
		return c/d;      
}      

int main ()    
{    
	int flag,sum;
	char c; 
	
	Sqstack optr;      // 运算符
	Sqstack1 opnd;    // 运算数
	
	InitStack(optr);      
	push(optr,'#');    
	
	InitStack1(opnd);      
	c=getchar();
    flag=sum=0;
	while(c!='#'||GetTop(optr)!='#')    
	{    
		if(in(c))    
		{    
			flag=1;
			sum=sum*10+c-'0';     // 处理多位数 
			c=getchar();   
		}    
		else    
		{    
			if(flag)
			{
			  flag=0;
			  push1(opnd,sum);
			  sum=0;
			}
			switch(pre(GetTop(optr),c))    
			{    
				
			case'<': push(optr,c);    
				c=getchar();    
				break;    
			case'=':pop(optr,c);    
				c=getchar();    
				break;    
			case'>':pop(optr,x);    
				pop1(opnd,b);    
				pop1(opnd,a);    
				push1(opnd,operate(a,x,b));    
				break;    
			}    
		}    
	}    
	cout<<GetTop1(opnd)<<endl;    
	return 0;    
}    




 三.括号匹配问题

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<iostream>
using namespace std;

#define S_SIZE 100   //栈的空间大小   
#define STACKINCREAMENT 10//增加空间 


char e;
struct SqStack{  
    char *base; //栈底   
    char *top;  //栈顶   
   int stacksize;   //栈当前的存储空间   
};  


void InitStack(SqStack &S)  
{
	S.base=(char *)malloc(S_SIZE*sizeof(char));  
S.stacksize=S_SIZE;  
S.top=S.base;//初始化空栈   
}  

int StackEmpty(SqStack S)  
{  
    if(S.base==S.top)  
        return 1;  
    else  
        return 0;  
}  

void push(SqStack &S,char e)  
{  
    if(S.top-S.base>=S.stacksize)  
    {
		S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREAMENT)*sizeof(char));  
    S.top=S.base+S.stacksize;  
    S.stacksize+=STACKINCREAMENT;
	}  
    *(S.top)=e;  
    S.top++;        
}  

void pop(SqStack &S,char &e)  
{  
    if(S.base!=S.top)  
    {
		S.top--;  
        e=*S.top;
	}  
}  

int main ()
{
  SqStack s;
  InitStack(s);
  char c[100];
  gets(c);
  int i=0;
  while(i<strlen(c))
  {
    switch(c[i])
	{
	case '(':
	case '[':
	case '{':push(s,c[i++]);break;
    case ')':
	case ']':
	case '}':pop(s,e);
		     if((e=='{' && c[i]=='}') ||(e=='[' && c[i]==']') || (e=='(' &&c[i]==')'))  
				 i++;
             else  
			 {
				 printf("括号匹配不成功\n");
			     exit(-1);
			 }
             break;  
	 default:i++;
	}
  }
 if(StackEmpty(s))  
     printf("括号匹配成功");  
 else  
     printf("括号匹配不成功");  
  printf("\n");  
 return 0;
}


 

#include<stdio.h>
#include<string.h>
#include<stack>

int main ()
{
  stack<char> s;
  char c[100];
  gets(c);
  int i=0;
   while(i<strlen(c))
   {
    switch(c[i])
	{
	case '(':
	case '[':
	case '{':s.push(c[i++]);break;
    case ')':
	case ']':
	case '}':if((s.top()=='{' && c[i]=='}') ||(s.top()=='[' && c[i]==']') || (s.top()=='(' &&c[i]==')'))  
			 {
				 i++;
			     s.pop();
			 }  
             else  
				 printf("括号匹配不成功\n");
             break;  

	 default:i++;
	}
  }
 
  if(s.empty())  
     printf("括号匹配成功");  
  else  
     printf("括号匹配不成功");  
  printf("\n"); 
 return 0;
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值