در محیط application در درس ساختمان داده:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ImportantInteger
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_ClickPostfix_Click(object sender, EventArgs e)
{
try
{
string Postfix1;
Postfix1 = txt_EnterStringPostfix.Text;
txt_ShowResultIntegerPostfix.Text = " Result String Postfix : " + SearchDataPostfix(Postfix1);
}
catch (Exception ecp)
{
MessageBox.Show(ecp.Message);
}
}
//==========================================================================================
public double Calculate1(string Symbol, double Integer1, double Integer2)
{
double Result = 0;
if (Symbol.CompareTo("+") == 0)
Result = Integer1 + Integer2;
if (Symbol.CompareTo("-") == 0)
Result = Integer1 - Integer2;
if (Symbol.CompareTo("*") == 0)
Result = Integer1 * Integer2;
if (Symbol.CompareTo("/") == 0)
Result = Integer1 / Integer2;
return Result;
}
//---------------------------------------------------------------------
public bool isNumber1(string S)//Sign=S
{
if (S.CompareTo("0") >= 0 && S.CompareTo("9") <= 0)
return true;
else
return false;
}
//-------------------------------------------------------------------
public double SearchDataPostfix(string Postfix)
{
double Capacity, Integer1, Integer2;
string Sign;
Stack<double> OprandStack = new Stack<double>();
for (int p = 0; p < Postfix.Length; p++)
{
Sign = Postfix.Substring(p, 1);
if (isNumber1(Sign))
{
OprandStack.Push(Convert.ToDouble(Sign));
}
else
{
Integer2 = OprandStack.Pop();
Integer1 = OprandStack.Pop();
Capacity = Calculate1(Sign, Integer1, Integer2);
OprandStack.Push(Capacity);
}
}
Capacity = OprandStack.Pop();
return Capacity;
}
//========================================================================
private void btn_ClickPerefix_Click(object sender, EventArgs e)
{
try
{
string Prefix;
Prefix = txt_EnterStringPrefix.Text;
txt_ShowResultIntegerPerefix.Text = " Result String Prefix : " + SearchDataPrefix(Prefix);
}
catch (Exception ecp)
{
MessageBox.Show(ecp.Message);
}
}
//-------------------------------------------------------------------
public double SearchDataPrefix(string Prefix)
{
double Capacity, Integer1, Integer2;
string Sign;
Stack<double> OprandStack = new Stack<double>();
for (int p = (Prefix.Length - 1); p >= 0; p--)
{
Sign = Prefix.Substring(p, 1);
if (isNumber1(Sign))
{
OprandStack.Push(Convert.ToDouble(Sign));
}
else
{
Integer2 = OprandStack.Pop();
Integer1 = OprandStack.Pop();
Capacity = Calculate1(Sign, Integer1, Integer2);
OprandStack.Push(Capacity);
}
}
Capacity = OprandStack.Pop();
return Capacity;
}
}
}
در محیط console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ImportantIntegerConsol
{
class Program
{
static void Main(string[] args)
{
string Postfix;
Console.WriteLine("Enter a Postfix Expression :");
Postfix = Console.ReadLine();
Console.WriteLine("Evaluted is :" + evaluate(Postfix));
Console.Read();
}
//------------------------------------------------------------------
static double Compute(string Symbol, double Operand1, double Operand2)
{
double Result = 0;
if (Symbol.CompareTo("+") == 0)
Result = Operand1 + Operand2;
if (Symbol.CompareTo("-") == 0)
Result = Operand1 - Operand2;
if (Symbol.CompareTo("*") == 0)
Result = Operand1 * Operand2;
if (Symbol.CompareTo("/") == 0)
Result = Operand1 / Operand2;
return Result;
}
//-------------------------------------------------------------------------
static bool isNumber(string s)//Symbol=s
{
if (s.CompareTo("0") >= 0 && s.CompareTo("9") <= 0)
return true;
else
return false;
}
//------------------------------------------------------------------------
static double evaluate(string Postfix)
{
double Value, Operand1, Operand2;
string Symbol;
Stack<double> OpStack = new Stack<double>();
for (int p = 0; p < Postfix.Length; p++)
{
Symbol = Postfix.Substring(p, 1);
if (isNumber(Symbol))
{
OpStack.Push(Convert.ToDouble(Symbol));
}
else
{
Operand2 = OpStack.Pop();
Operand1 = OpStack.Pop();
Value = Compute(Symbol, Operand1, Operand2);
OpStack.Push(Value);
}
}
Value = OpStack.Pop();
return Value;
}
}
}
بااحترام موضوعات مشابه:
علاقه مندی ها (Bookmarks)