#include <iostream.h> #include <stdlib.h> typedef int priority; typedef int Status; #define TRUE 1; #define FALSE 0; typedef char elemtype; /*栈元素类型,用链表连接*/ typedef struct node { elemtype chdata; struct node *next; }*pnode; /*定义栈*/ typedef struct stack { pnode top; int length; }chstack; /*初始化栈*/ void InitStack(stack &s) { s.top=NULL; s.length=0; } /*栈不为空,出*/ Status Push(stack &s,elemtype e) { pnode tem; tem=new node; if(!tem) return FALSE; tem->chdata=e; tem->next=s.top; s.top=tem; s.length++; return TRUE; } /*栈不为空,入*/ Status Pop(stack &s,elemtype &e) { pnode del; e=s.top->chdata; del=s.top; s.top=s.top->next; delete del; s.length--; return TRUE; } /*做第一个'#'*/ void Fstack(stack &s) { elemtype e; e='#'; if(!Push(s,e)) exit(0); } /*取得栈顶元素,e带回*/ bool Gettop(stack &s,elemtype &e) { if(s.length>=1) { e=s.top->chdata; return true; } return false; } /*定义优先级,返回*/ priority chpri(char e) { switch (e) { case '(': return 0; case ')': return 0; case '+': return 1; case '-': return 1; case '*': return 2; case '#': return -1; case '/': return 2; } } bool IsOperator(char e) { switch (e) { case '(': break; case ')': break; case '+': break; case '-': break; case '*': break; case '/': break; default: return false; } return true; } /*返回a>=b为真*/ bool PriCom(char a,char b) { int ai; int bi; ai=chpri(a); bi=chpri(b); return (ai>=bi? true:false); } /*不包括空格,主要转换部分*/ void conver(char suff[],char chconver[]) { stack s; char *p; char *psuff; char stacktop; char stackout; InitStack(s); Fstack(s); psuff=suff; p=chconver; while(p!='\0') { if(!IsOperator(*p)) *psuff++=*p; else { switch (*p) { case'(': Push(s,*p); break; case')': do { Gettop(s,stackout); *psuff++=stackout; }while(stacktop!='('); Gettop(s,stackout); *psuff++=stackout; break; default: Gettop(s,stacktop); if( PriCom(*p,stacktop ) ) Push(s,*p); while( !PriCom(*p,stacktop) ) { Pop(s,stackout); *psuff++=stackout; } Push(s,*p); }//endswitch
}//endelse
p++; }//endwhile
while(stackout!='#') { Gettop(s,stackout); *psuff++=stackout; } } void main() { int a[6]={1,2,3,4,5,6}; int *p; p=a; *p++=8; cout<<a[0]; cout<<*p; }
|