Data structure and algorithm (C # implementation) Series---demo (ii)

Source: Internet
Author: User
Tags readline rpn calculator tostring
Data | structure | algorithm data structure and algorithm (C # implementation) Series---demo (ii.)

Heavenkiller (original)

public static void Showgeneraltree_travel ()

{

IEnumerator Tmpienum;

Tree.traversaltype traveltype=0;



---------------------Tips----------------------------

Console.WriteLine ("Please choose a" No.) Of a item you want to travel: ");

Console.WriteLine ("1.BreadthFirst-----breadth Traversal");

Console.WriteLine ("2.PreDepthFirst-----pre-sequence traversal");

Console.WriteLine ("3.InDepthFirst----sequence traversal");

Console.WriteLine ("4.PostDepthFirst----subsequent traversal");

Switch (Console.ReadLine ())

{

Case "1"://show Stack

Traveltype=tree.traversaltype.breadth;

Console.WriteLine ("Breadth Traversal");

Break

Case "2"://sortedlist

traveltype=tree.traversaltype.predepth;

Console.WriteLine ("Pre-sequence traversal");

Break

Case "3":

traveltype=tree.traversaltype.indepth;

Console.WriteLine ("Sequence Traversal");

Break

Case "4":







traveltype=tree.traversaltype.postdepth;

Console.WriteLine ("subsequent traversal");

Break



Default

Break

}

Constructing a generalized tree generaltree

Generaltree a=new generaltree ("A");

Generaltree b=new generaltree ("B");

Generaltree c=new generaltree ("C");

Generaltree d=new generaltree ("D");

Generaltree e=new generaltree ("E");

Generaltree f=new generaltree ("F");

A.attacksubtree (B);

A.attacksubtree (C);

B.attacksubtree (D);

B.attacksubtree (E);

A.attacksubtree (F);

Show the operation

Console.WriteLine ("A.attacksubtree (B)");

Console.WriteLine ("A.attacksubtree (C)");

Console.WriteLine ("B.attacksubtree (D)");

Console.WriteLine ("B.attacksubtree (E)");

Console.WriteLine ("A.attacksubtree (F)");

//--------------------------------------------------------



A.settraversaltype (Traveltype);//Set Traversal type

Tmpienum=a.getenumerator ();



Console.WriteLine ("Begin to Depthfist travel:");



while (Tmpienum.movenext ())

{

Console.WriteLine (TmpIEnum.Current.ToString ());

}





}



public static void Showstack_rpncalculator ()

{

Read a expression string and push every character into the stack in queue.

Console.WriteLine ("This is performance to stack,you can input a string like this ' 123*+ ', then this subprogramme can Compu Te it and get the result ' 7 ', which is RPN calculator. ");

Console.WriteLine ("Please input a expression string:");

String Strexpression=console.readline ();

Char [] Tmpchars=strexpression.tochararray (0,strexpression.length);

Stack stackrpn=new stack ();







int numa,numb;

foreach (char tmp in Tmpchars)

{

Switch (TMP)

{



Case ' * ':

numa= (int) stackrpn.pop ();

numb= (int) stackrpn.pop ();

Stackrpn.push (NUMA*NUMB);

Break

Case ' + ':

numa= (int) stackrpn.pop ();

numb= (int) stackrpn.pop ();

Stackrpn.push (NUMA+NUMB);

Break

Default

Stackrpn.push (Int32.Parse (TMP). ToString ()));

Break



}







}

Console.WriteLine ("The Result is:{0}", Stackrpn.pop (). ToString ());





}







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.