Simple expression calculation implemented by stack

Source: Internet
Author: User

[Csharp]
Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Text. RegularExpressions;
 
Simple namespace expression
{
Class Program
{
Static void Main (string [] args)
{
Stack numbs = new Stack ();
Stack ops = new Stack ();
 
String expression = "5 + 10-15*20*2 ";
 
Calculate (numbs, ops, expression );
 
Console. WriteLine (numbs. Pop ());
}
 
Public static bool IsNumeric (string input)
{
Bool flag = true;
 
String pattern = "^ \ d + $ ";
 
Regex validate = new Regex (pattern );
 
If (! Validate. IsMatch (input ))
{
Flag = false;
}
 
Return flag;
}
 
Public static void Calculate (Stack n, Stack o, string exp)
{
String ch, token = "";
 
For (int p = 0; p <exp. Length; p ++)
{
Ch = exp. Substring (p, 1 );
If (IsNumeric (ch ))
{
Token + = ch;
}
 
If (ch = "" | p = (exp. Length-1 ))
{
If (IsNumeric (token ))
{
N. Push (token );
Token = "";
}
}
Else if (ch = "+" | ch = "-" | ch = "*" | ch = "/")
{
O. Push (ch );
}
 
If (n. Count = 2)
{
Compute (n, o );
}
}
}
 
Public static void Compute (Stack n, Stack o)
{
Int oper1, oper2;
 
String delimiter;
 
Oper1 = Convert. ToInt32 (n. Pop ());
Oper2 = Convert. ToInt32 (n. Pop ());
 
Topology = Convert. ToString (o. Pop ());
Switch)
{
Case "+ ":
N. Push (oper1 + oper2 );
Break;
Case "-":
N. Push (oper1-oper2 );
Break;
Case "*":
N. Push (oper1 * oper2 );
Break;
Case "/":
N. Push (oper1/oper2 );
Break;
}
 
}
}
}

Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Text. RegularExpressions;

Simple namespace expression
{
Class Program
{
Static void Main (string [] args)
{
Stack numbs = new Stack ();
Stack ops = new Stack ();

String expression = "5 + 10-15*20*2 ";

Calculate (numbs, ops, expression );

Console. WriteLine (numbs. Pop ());
}

Public static bool IsNumeric (string input)
{
Bool flag = true;

String pattern = "^ \ d + $ ";

Regex validate = new Regex (pattern );

If (! Validate. IsMatch (input ))
{
Flag = false;
}

Return flag;
}

Public static void Calculate (Stack n, Stack o, string exp)
{
String ch, token = "";

For (int p = 0; p <exp. Length; p ++)
{
Ch = exp. Substring (p, 1 );
If (IsNumeric (ch ))
{
Token + = ch;
}

If (ch = "" | p = (exp. Length-1 ))
{
If (IsNumeric (token ))
{
N. Push (token );
Token = "";
}
}
Else if (ch = "+" | ch = "-" | ch = "*" | ch = "/")
{
O. Push (ch );
}

If (n. Count = 2)
{
Compute (n, o );
}
}
}

Public static void Compute (Stack n, Stack o)
{
Int oper1, oper2;

String delimiter;

Oper1 = Convert. ToInt32 (n. Pop ());
Oper2 = Convert. ToInt32 (n. Pop ());

Topology = Convert. ToString (o. Pop ());
Switch)
{
Case "+ ":
N. Push (oper1 + oper2 );
Break;
Case "-":
N. Push (oper1-oper2 );
Break;
Case "*":
N. Push (oper1 * oper2 );
Break;
Case "/":
N. Push (oper1/oper2 );
Break;
}

}
}
}


 

Related Article

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.