-------------------------------------------------------------------------------------------
push_c('#');
for (i=0; i<len; i++)
{ s = token[i];
listBox1->Items->Add(Char::ToString(s));
if ((s!='+') && (s!='-') && (s!='*') && (s!='/') && (s!='(') && (s!=')') && (s!='&') && (s!='|') && (s!='^'))
{
poster[k++] = s;
}
else if (s == ')')
{ while ((x = pop_c()) != '(')
poster[k++] = x;
}
else
{ listBox1->Items->Add("p(s)="+(p(s)));
listBox1->Items->Add("q(stack(top))="+q(stack_c[top]));
while ( p(s) >= q(stack_c[top]) )
{ x = pop_c();
poster[k++] = x;
}
push_c(s);
}
}
print_stack_c(stack_c, top);
while (stack_c[top] != '#')
{ x = pop_c();
poster[k++] = x;
}
delete [] stack_c ;
// delete [] poster;
return poster;
// return post;
---------------- 2012/11/04 by S. J. Shyu in BCB2010 --------------------------
#include <vcl.h>
#pragma hdrstop
# define max_size 100
#include "infix2pre_postfix.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ShowMessage("Transformation among infix, prefix and postfix \n by S. J. Shyu \n Nov. 2012 Ver 0.92");
}
String Stack[max_size];
String Stack_another[max_size];
int top = -1;
int top_another = -1;
void print_stack_another()
{ int i;
String data1 = "", data2 = "" ;
for (i = 0; i <= top; i++) data1 += Stack[i]+" ";
for (i = 0; i <= top_another; i++) data2 += Stack_another[i]+" ";
Form1->Memo1->Lines->Add("Stack 1 => "+data1);
Form1->Memo1->Lines->Add("Stack 2 => "+data2);
}
void push(int flag, String data)
{ switch (flag)
{ case (1):
if (top == max_size-1) Form1->Memo1->Lines->Add("Full");
else Stack[++top] = data;
break;
case (2):
if (top_another == max_size-1) Form1->Memo1->Lines->Add("Full");
else Stack_another[++top_another] = data;
break;
}
if (Form1->CheckBox1->Checked) print_stack_another();
}
String pop(int flag)
{ switch (flag)
{ case (1):
if (top == -1) Form1->Memo1->Lines->Add("Empty");
else return Stack[top--];
break;
case (2):
if (top_another == -1) Form1->Memo1->Lines->Add("Empty");
else return Stack_another[top_another--];
break;
}
}
int p(String op)
{ if ((op == '+') || (op == '-') ) return 3 ;
if ((op == '*') || (op == '/') ) return 4 ;
if ((op == '^') || (op == '&') || (op == '|') ) return 5 ;
if ((op == '(') ) return 8 ;
if (op == '#') return 0 ;
}
int q(String op)
{ if ((op == '+') || (op == '-') ) return 3 ;
if ((op == '*') || (op == '/') ) return 4 ;
if ((op == '^') || (op == '&') || (op == '|') ) return 5 ;
if ((op == '(')) return 1 ;
if (op == '#') return 0 ;
}
void get_fix(String x, int flag)
{ String a = pop(2);
a = (flag == 1) ? x+pop(2)+a : pop(2)+a+x;
push(2, a) ;
}
boolean IsOperand(String s)
{ if ( (s!="+") && (s!="-") && (s!="*") && (s!="/")
&& (s!="(") && (s!=")") && (s!="&") && (s!="|")
&& (s!="^") ) return true;
return false;
}
String postfixTOprefix(String postfix)
{ int n = postfix.Length();
int i;
String s, x;
for (i=1; i<=n; i++)
{ s = postfix[i];
if ( IsOperand(s) ) push(2, s);
else get_fix(s, 1);
}
return pop(2);
}
String postfixTOinfix(String postfix)
{ int n = postfix.Length();
int i;
String s, s1, s2, x, y, prev = "#";
for (i=1; i<=n; i++)
{ s = postfix[i];
if (IsOperand(s))
{ push(2, s);
push(1, "#");
}
else { s1 = pop(1);
s2 = pop(1);
x = pop(2);
y = pop(2);
if (p(s) > p(s1) && x.Length() > 1) x = "("+x+")";
if (p(s) > p(s2) && y.Length() > 1) y = "("+y+")";
if (Form1->CheckBox1->Checked)
Form1->Memo1->Lines->Add("p("+s+")="+IntToStr(p(s))+"("+s1+")="+
IntToStr(p(s1))+" p("+s2+")="+IntToStr(p(s2))+" s1="+s1+" s2="+
s2+" x="+x+" y="+y);
push(2, y+s+x);
push(1, s);
}
}
while (top != -1) x = pop(1);
return pop(2);
}
String prefixTOpostfix_reverse(String prefix)
{ int n = prefix.Length();
int i, counter = 0;
String s, post;
for (i=n; i>=1; i--)
{ s = prefix[i];
if ( IsOperand(s) )
push(2, s);
else { post = pop(2);
post += pop(2)+s;
if (Form1->CheckBox2->Checked) Form1->Memo1->Lines->Add(post);
push (2, post);
}
}
return pop(2);
}
String transformation(String infix, int flag)
{ int n = infix.Length();
int i, j;
String s, x;
push(1, "#");
for (i=1; i<=n; i++)
{ s = infix[i];
if ( IsOperand(s) ) push(2, s);
else if (s == ")") //將堆疊中第一個 ( 之前的運算子皆pop出並印出之
while ((x=pop(1))!="(") get_fix(x, flag);
else
{ while ( p(s) <= q(Stack[top]) )
{ x = pop(1);
get_fix(x, flag);
}
push(1, s);
}
}
while (Stack[top]!= "#")
{ x = pop(1);
get_fix(x, flag);
}
pop(1);
return pop(2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String infix = Edit1->Text;
String prefix = transformation(infix, 1);
Edit4->Text = prefix;
Memo1->Lines->Add("infix: "+infix);
Memo1->Lines->Add("prefix: "+prefix);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
String infix = Edit1->Text;
String postfix = transformation(infix, 2);
Edit5->Text = postfix;
Memo1->Lines->Add("infix: "+infix);
Memo1->Lines->Add("postfix: "+postfix);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
String prefix;
String postfix = Edit2->Text;
Memo1->Lines->Add("postfix: "+postfix);
prefix = postfixTOprefix(postfix);
Edit6->Text = prefix;
Memo1->Lines->Add("prefix: "+prefix);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
String postfix;
String prefix = Edit3->Text;
Memo1->Lines->Add("prefix: "+prefix);
postfix = prefixTOpostfix(prefix);
Memo1->Lines->Add("postfix: "+postfix);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
String postfix;
String prefix = Edit3->Text;
Memo1->Lines->Add("prefix: "+prefix);
postfix = prefixTOpostfix_reverse(prefix);
Edit8->Text = postfix;
Memo1->Lines->Add("postfix: "+postfix);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
String infix;
String postfix = Edit2->Text;
Memo1->Lines->Add("postfix: "+postfix);
infix = postfixTOinfix(postfix);
Edit7->Text = infix;
Memo1->Lines->Add("infix: "+infix);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
Edit1->Text = ListBox1->Items->operator [](ListBox1->ItemIndex);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Click(TObject *Sender)
{
Edit1->Text = ComboBox1->Items->Strings[ComboBox1->ItemIndex]; ;
//ComboBox1->Items->operator [](ComboBox1->ItemIndex);
}
//---------------------------------------------------------------------------
========================= Visual Studio 2022(by Liao, W. C.) ============================
#define MAX 100
static array<String^>^ stack = gcnew array<String^>(100);
static array<String^>^ stack_another = gcnew array<String^>(100);
static int top = -1;
static int top_another = -1;
int p(String^ op)
{
if ((op == "+") || (op == "-"))
return 3;
if ((op == "*") || (op == "/"))
return 4;
if ((op == "^") || (op == "&") || (op == "|"))
return 5;
if (op == "(")
return 8;
if (op == "#")
return 0;
return -99;
}
int q(String^ op)
{
if((op=="+") || (op == "-"))
return 3;
if((op==" * ") || (op=="/"))
return 4;
if((op=="^") || (op == "&") || (op == "|"))
return 5;
if(op=="(")
return 1;
if(op=="#")
return 0;
return -99;
}
bool IsOperand(String^ s)
{
if ((s != "+") && (s != "-") && (s != "*") && (s != "/") && (s != "(") && (s != ")") && (s != "&") && (s != "|") && (s != "^"))
return true;
else
return false;
}
void push(int flag, String^ data)
{
switch (flag)
{
case (1):
if (top == MAX - 1)
richTextBox2->Text += "Full\n";
else
stack[++top] = data;
break;
case (2):
if (top_another == MAX - 1)
richTextBox2->Text += "Full\n";
else
stack_another[++top_another] = data;
break;
}
}
String^ pop(int flag)
{
switch (flag)
{
case(1):
if (top == -1)
richTextBox2->Text+="Empty\n";
else
return stack[top--];
break;
case(2):
if (top_another == -1)
richTextBox2->Text += "Empty\n";
else
return stack_another[top_another--];
break;
}
}
void get_fix(String^ x, int flag)
{
String^ a = pop(2);
a = (flag == 1) ? x + pop(2) + a : pop(2) + a + x;
push(2, a);
}
String^ Infix2Prefix_Postfix(String^ infix, int flag)
{
int n = infix->Length;
String^ s, ^x;
push(1, "#");
for (int i = 0; i < n; i++)
{
s = infix[i].ToString();
if (IsOperand(s))
push(2, s);
else if (s == ")")
while ((x = pop(1)) != "(")
get_fix(x, flag);
else
{
while (p(s) <= q(stack[top]))
{
x = pop(1);
get_fix(x, flag);
}
push(1, s);
}
}
while (stack[top] != "#")
{
x = pop(1);
get_fix(x, flag);
}
pop(1);
return pop(2);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Text = "";
richTextBox2->Text = "";
String^ infix = textBox1->Text;
String^ postfix = Infix2Prefix_Postfix(infix, 2);
textBox3->Text = postfix;
richTextBox1->Text += "Infix: " + infix + "\n";
richTextBox1->Text += "Postfix: " + postfix + "\n";
}