首页 > 开发 > 综合 > 正文

C#堆栈

2024-07-21 02:25:56
字体:
来源:转载
供稿:网友

using system;

namespace zh.dataframe.abstractdata
{
    objectstackadt object堆栈#region objectstackadt object堆栈
    /**//// <summary>
    /// object堆栈(先进后出)
    /// </summary>
    public class stackadt
    {
        private object[] s;
        private int n;
        public stackadt(int maxn)
        {
            s=new object[maxn];n=0;
        }
        public bool isempty()
        {
            return (n==0);
        }
        /**//// <summary>
        /// 推入
        /// </summary>
        /// <param name="item"></param>
        public void push(object item)
        {
            s[n++]=item;
        }
        /**//// <summary>
        /// 推出
        /// </summary>
        /// <returns></returns>
        public object pop()
        {
            object t=s[--n];
            s[n]=null;
            return t;
        }

    }
    #endregion

    nodestack(int) 链表int堆栈#region nodestack(int) 链表int堆栈
/**//// <summary>
/// 链表int堆栈
/// </summary>
    public class nodestack
    {
       
        private  class node
        {
            public int item;public node next;
            public node(int item1,node next)
            {
                item=item1;this.next=next;
            }
        }
        private node head;
        public nodestack()
        {
            head=null;
        }
        public bool isempty()
        {
           return (head==null);
        }
        public void push(int item)
        {
            head=new node(item,head);
        }
        public int pop()
        {
            int v=head.item;
            node t=head.next;
            head=t;
            return v;
        }
    }
    #endregion

    nodestack(object) 链表object堆栈#region nodestack(object) 链表object堆栈
/**//// <summary>
/// 链表object堆栈
/// </summary>
    public class onodestack
    {
       
        private  class node
        {
            public object item;public node next;
            public node(object item1,node next)
            {
                item=item1;this.next=next;
            }
        }
        private node head;
        public onodestack()
        {
            head=null;
        }
        public bool isempty()
        {
            return (head==null);
        }
        public void push(object item)
        {
            head=new node(item,head);
        }
        public object pop()
        {
            object v=head.item;
            node t=head.next;
            head=t;
            return v;
        }
    }
    #endregion

    doublestack double堆栈#region  doublestack double堆栈
    /**//// <summary>
    /// double堆栈
    /// </summary>
    public class intstack
    {
        private double[] s;
        private int n;
        public intstack(int maxn)
        {
            s=new double[maxn];n=0;
        }
        public bool isempty()
        {
            return n==0;
        }
        public void push(double item)
        {
            s[n++]=item;
        }
        public double pop()
        {
            return s[--n];
        }
       
    }
    #endregion

    charstack 字符堆栈#region charstack 字符堆栈
    /**//// <summary>
    /// 字符堆栈
    /// </summary>
    public class charstack
    {
        private char[] ss;
        private int n;
        public charstack(int maxn)
        {
            ss=new char[maxn];n=0;
        }
        public bool isempty()
        {
            return n==0;
        }
        public void push(char item)
        {
            ss[n++]=item;
        }
        public char pop()
        {
            return ss[--n];
        }
    }
    #endregion

    infixtopostfix 中缀法转后缀法#region infixtopostfix 中缀法转后缀法
    /**//// <summary>
    /// 中缀法转后缀法
    /// </summary>
    public class infixtopostfix
    {
        public static char[] getinfixtopostfix(string str)
        {
            char[] a=str.tochararray();
            int n=a.length;   
            charstack s=new charstack(n);
            char[] b=new char[2*tonum(a)];
            int n=0;
            for(int i=0;i<n;i++)
            {

                if(a[i]==')')
                {
                    b[n++]=' ';
                    b[n++]=s.pop();
                   
                }
                if((a[i]=='+')||(a[i]=='*')||(a[i]=='-')||(a[i]=='/'))
                    s.push(a[i]);
                if((a[i]>='0')&&(a[i]<='9')||a[i]=='.')
                {
                    if((a[i-1]>='0')&&(a[i-1]<='9')||a[i-1]=='.')
                    {
                        b[n++]=a[i];
                    }
                    else
                    {
                        b[n++]=' ';
                        b[n++]=a[i];
                       
                    }
                }
           
            }
            return b;
        }
        private static int tonum(char[] c)
        {
            int index=c.length;
            int index1=c.length;
            for(int i=0;i<index;i++)
            {
                if((c[i]=='(')||(c[i]==')'))
                {
                    index1=index1-1;
                }
            }
            return index1;
        }
    }
    #endregion

    postfix 计算中缀法表达式#region postfix 计算中缀法表达式
    /**//// <summary>
    /// 计算中缀法表达式
    /// </summary>
    public class postfix
    {
        public static double getstringpostfix(string str)
        {
            char[] a= str.tochararray();
            int n=a.length;
            intstack s=new intstack(n);

            for(int i=0;i<n;i++)
            {
                if(a[i]=='+')
                {
                    s.push(s.pop()+s.pop());
                }
                if(a[i]=='-')
                {
                    double num1=s.pop();
                    double num2=s.pop();
                 
                    s.push(num2-num1);
                }
                if(a[i]=='/')
                {
                    double num1=s.pop();
                    double num2=s.pop();
                    s.push(num2/num1);
                }
               

                if(a[i]=='*')
                    s.push(s.pop()*s.pop());
                if((a[i]>='0')&&(a[i]<='9'))//(0)防止s.pop索引超界
                    s.push(0);
                while((a[i]>='0')&&(a[i]<='9'))//计算合并多余两位的数字
                {

                    s.push(10*s.pop()+(a[i++]-'0'));//a[i++]-'0'把字符转化为数字
                }
                a[i]=='.'#region a[i]=='.'
                if(a[i]=='.')
                {
                    
                    double num1=s.pop();                    
                    i=i+1;                    
                    string f=null;
                    while((a[i]>='0')&&(a[i]<='9'))
                    {
                        f=f+a[i++];
                    }
                    int n=f.length;
                    double nf=convert.toint32(f);
                    for(int j=0;j<n;j++)
                    {
                        nf=0.1*nf;
                    }
                    s.push(num1+nf);
                }       
                #endregion
               
            }   
            return s.pop();
        }
        public static double getcharpostfix(char[] ch)
        {                       
            char[] a= ch;
            int n=a.length;
            intstack s=new intstack(n);

            for(int i=0;i<n;i++)
            {
                if(a[i]=='+')
                {
                    s.push(s.pop()+s.pop());
                }
                if(a[i]=='-')
                {
                    double num1=s.pop();
                    double num2=s.pop();
                 
                    s.push(num2-num1);
                }
                if(a[i]=='/')
                {
                    double num1=s.pop();
                    double num2=s.pop();
                    s.push(num2/num1);
                }
               

                if(a[i]=='*')
                    s.push(s.pop()*s.pop());
                if((a[i]>='0')&&(a[i]<='9'))//(0)防止s.pop索引超界
                    s.push(0);
                while((a[i]>='0')&&(a[i]<='9'))//计算合并多余两位的数字
                {

                    s.push(10*s.pop()+(a[i++]-'0'));//a[i++]-'0'把字符转化为数字
                }
                a[i]=='.'#region a[i]=='.'
                if(a[i]=='.')
                {
                    
                    double num1=s.pop();                    
                    i=i+1;                    
                    string f=null;
                    while((a[i]>='0')&&(a[i]<='9'))
                    {
                        f=f+a[i++];
                    }
                    int n=f.length;
                    double nf=convert.toint32(f);
                    for(int j=0;j<n;j++)
                    {
                        nf=0.1*nf;
                    }
                    s.push(num1+nf);
                }       
                #endregion
               
            }   
            return s.pop();
        }
    }
    #endregion


}

中国最大的web开发资源网站及技术社区,
上一篇:C# 文件操作

下一篇:c#链表类

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表