TXT

stack

By Brenda Grant,2014-09-24 10:57
15 views 0
stack

using System;

    using System.Collections.Generic; using System.Text;

    using System.Collections;

    //?ÙÇèÒ??öËãÊõ?í?ïÊ?ÖĞ?üº?Ô?À???()????À???[ ]2ÖÖ??Í?ÀàĞ͵ÄÀ????? //?àĞ?Ò??ö?ÌĞò??ÊäÈë×Ö?û???ñÊ?µÄ?í?ïÊ???ÅĞ?Ï?í?ïÊ?ÖеÄÀ???ÊÆ?ñÕıÈ?

    Åä?Ô????ÌáÊ??ºÊ?ÓÃStackʵÏÖ??

    namespace Csun

    {

     class checkstring

     {

     string email;

     public checkstring(string email)

     {

     this.email = email;

     }

     public bool check()

     {

     bool flag = true;

     Stack<char> a = new Stack<char>();

     for (int i = 0; i < email.Length; i++)

     {

     if (email[i] == '[' || email[i] == '(')

     a.Push(email[i]);

     else

     {

     if (email[i] == ']')

     {

     if (a.Count == 0)

     {

     flag = false;

     break;

     }

     else if (a.Peek() != '[')

     {

     flag = false;

     break;

     }

     else if (a.Peek() == '[')

     {

     a.Pop();

     }

     }

     else

     {

     if (a.Count == 0)

     {

     flag = false;

     break;

     }

     else if (a.Peek() != '(')

     {

     flag = false;

     break;

     }

     else if (a.Peek() == '(')

     {

     a.Pop();

     }

     }

     }

     }

     if (flag == true && a.Count == 0)

     return true;

     return false;

     }

     }

     class Test

     {

     static void Main(string[] args)

     {

     while (true)

     {

     string s = Console.ReadLine();

     checkstring a = new checkstring(s);

     Console.WriteLine( a.check());

     }

     }

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com