Chinaunix首页 | 论坛 | 博客
  • 博客访问: 497189
  • 博文数量: 161
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1947
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-25 01:20
文章分类

全部博文(161)

文章存档

2011年(44)

2010年(47)

2009年(48)

2008年(22)

我的朋友

分类: C/C++

2008-07-30 02:50:25

#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;
}

阅读(1541) | 评论(0) | 转发(0) |
0

上一篇:c语言链表

下一篇:protel99se 一

给主人留下些什么吧!~~