码迷,mamicode.com
首页 > 其他好文 > 详细

栈的实现,进制数转换以及符号匹配

时间:2015-05-08 15:04:23      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

//seqstack.h
#ifndef _SEQSTACK_H
#define _SEQSTACK_H

#define STACK_SIZE 20
typedef int ElemType;  //若要使用功能5 请将int 改为char
#include<iostream>
#include<assert.h>
using namespace std;

typedef struct Stack
{
  ElemType *base;
  int capacity;
  int top;
}Stack;

/////////////////////////////////
bool Isfull(Stack *sta)
{
  return sta->top < sta->capacity;
}

bool IsEmpty(Stack *sta)
{
  return sta->top == 0;
}

bool Init(Stack *sta)
{
	sta->base = (ElemType *)malloc(sizeof(ElemType)*STACK_SIZE);
	assert(sta->base != NULL);
     
	 sta->top = 0;
	 sta->capacity = STACK_SIZE;
   
	 return true;
}

bool push_stack(Stack *sta,ElemType x)
{
   sta->base[sta->top++] = x;
   return true;

}

bool pop_stack(Stack *sta)
{
  if(IsEmpty(sta))
  {
	 cout<<"the stack is empty"<<endl;
	 return false;
  }
  cout<<sta->base[sta->top-1];
  sta->top--;

  return true;
}

void show_stack(Stack *sta)
{
  int i = 0;
  cout<<"top:";
  while(i < sta->top)
  {
    cout<<sta->base[i]<<"-";
	i++;
  }
  cout<<">base"<<endl;
}

bool n_chage(Stack *sta,ElemType n) //n为你要转化的进制数
{                          
	sta->top = 0;//清空原来的栈
   int obj;
   int a;
   cout<<"请输入需要转化的数字:";
   cin>>obj;
   while(obj != 0)
   {
	 a = obj%n;
     push_stack(sta,a);
	 obj = obj /n;
   }
   while(sta->top != 0)
   {
     pop_stack(sta);
   }
   return true;
}

bool pipei(char a,char b)
{
  if( a== '(' && b == ')')
	  return true;
  if(a == '[' && b ==']')
		  return true;
 if(a == '{' && b=='}')
         return true;
 return false;
}

bool string_pipei(Stack *sta)
{
  sta->top = 0;
  int count;
  char str[100];
  char *p =str;
   cout<<"请输入要匹配的字符串";
   cin>>str;
   while(*p != '\0')
   {
     if(*p =='[')
	 {
		 push_stack(sta,*p);
	     count =1;
	 }
	 if(*p =='(')
	 {
		 push_stack(sta,*p);
	     count =1;
	 }
	 if(*p =='{')
	 {
		 push_stack(sta,*p);
	     count =1;
	 }
	 p++;
	if(pipei(sta->base[sta->top-1],*p))
		 pop_stack(sta); 
   }
   if(sta->top == 0 && count == 1)
	   cout<<"  匹配"<<endl;
   else
	   cout<<"不匹配"<<endl;
   return true;
}

#endif
///////////////////


//////main函数实现部分

#include"seqstack.h"
void main()
{

  Stack mystack;
  int select;
  int item;
  Init(&mystack);
  while(select)
  {
    cout<<"*[0] quit        [1] pushstack   *"<<endl;
	cout<<"*[2] popstack    [3] show_stack  *"<<endl;
	cout<<"*[4] n_chage     [5] string_pipei*"<<endl;
	cout<<"请选择";
	cin>>select;
	switch(select)
	{
	  case 1:
		  cout<<"请输入要入栈的数以-1结束"<<endl;
			  while(cin>>item,item != -1)
			  {
			    push_stack(&mystack,item);
			  }
			  break;
	  case 2:
		      pop_stack(&mystack);
			  cout<<endl;
		     break;
	  case 3:
		     show_stack(&mystack);
		     break;
	  case 4:
		    cout<<"请输入进制数(不超过10):";
		    cin>>item;
		    n_chage(&mystack,item);
			cout<<endl;
		     break;
	  case 5:
		  //若要使用此函数,请请看看数据类型
           string_pipei(&mystack);
		   break;
	  default:
		  break;
	}
  }  
}
技术分享技术分享技术分享

栈的实现,进制数转换以及符号匹配

标签:

原文地址:http://blog.csdn.net/zr1076311296/article/details/45577691

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!