Reverse Polish Notation





1.00/5 (9 votes)
Sep 28, 2006
1 min read

33864

305
How Processor solve the arithmetic expressions
Download source files - 97.0 Kb
Introduction
I made this application for my semester project for the subject of the Computer Architecture in which i decide to made a project which will use my capabilites for Data Structure for implement on the Computer Architecture so i decide to made a project for the Addressing ModesWhat is This:
It is an application which shows the addrssing mode works according to the Reverse Polish notation rulse.Domain of Project:
In this project there are the follwing restrictions
- 0-9 digits you can use for expression
- for -ve digit u will use the # special character for exaple for -1+2 will use as #1+2
- you can not use it for increment or decrement expression as ++1 is invalid
- You can enter only 0 to 3 addressing modes
Techniques For the Project
In this project i use the following techniques
- Self exception class
class ProgramException : Exception
{
public ProgramException()
{
}
public ProgramException(string message)
: base(message)
{
}
public ProgramException(string message, Exception inner)
: base(message, inner)
{
}
}
class postfix
{
public string conversion(string infix)
//geting the infix expression and returning
//the reverse polish notation/post fix expression
{
int c = 0;
string rpn = "";//initaly string will empty
int size = infix.Length, i = 0;
//geting the length of the experasion(infix string)
//and i variable will be use for counting in while loop
stack stk = new stack(size);
//crating a stack according to the size of expression
while (i < size)
//this loop is used for examining the expression
{
char ch = infix[i];
//geting a single character of the string for expresion
//Console.WriteLine(ch);
//return "a";
if (ch == '(' || ch == '{' || ch == '[' || ch == ')'
|| ch == '}' || ch == ']')
{
if (ch == '(' || ch == '{' || ch == '[')
{
c++;
stk.push(ch);
}
else if (ch == ')' || ch == '}' || ch == ']')
{
if (ch == ')')
{
while (true)
{
char sp = stk.pop();
if (sp == '(' || sp=='e')
break;
rpn = rpn + "" + sp;
}
}
else if (ch == '}')
{
while (true)
{
char sp = stk.pop();
if (sp == 'e')
break;
if (sp == '{')
break;
rpn = rpn + "" + sp;
}
}
else
{
while (true)
{
char sp = stk.pop();
if (sp == '[' || sp=='e')
break;
//Console.WriteLine(sp);
rpn = rpn + "" + sp;
sp = stk.pop();
}
}
}
}
else if (ch == '#')
{
rpn = rpn + "" + ch;
rpn = rpn + "" + infix[++i];
}
else if (ch >= '0' && ch <= '9')//for operand
{
rpn = rpn + "" + ch;//appending operand with the string
}
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
//for operator examining
{
while (true)
//make it infinite because dont know the operator precedence
{
char sp = stk.pop();
//geting the latest store operand in the que
//for checking the operator precedence and type(+,-,*or/ )
if (ch == '+' || ch == '-')
//actions related to plus and minus operator
{
if (sp == 'e' || sp == '(' || sp == '{' || sp == '[')
//if the que will empty
{
if (sp == '(' || sp == '{' || sp == '[')
{
stk.push(sp);
}
stk.push(ch);
//pushing the latest read operator in the expression
break;
//breaking the infinite loop for geting
//the next operator in the expression
}
else if (sp == '/' || sp == '*')//for equal precedence
{
rpn = rpn + "" + sp;
//When same precedence then solve from left to right
stk.push(ch);
//now pushing the latest same presendece operator
break;
//after it will break obviously before
//it lies a low precedence
}
else if (sp == '+' || sp == '-')//for low precedence
{
//stk.push(sp);
//because it will not append with post fix
//string this time and its presedence
//is less so again push in stack
rpn = rpn + "" + sp;
stk.push(ch);//pushing high precedecne operator
break;
//after it will break obviously before
//it lies a low precedence
}
else
//appending with array because these
//operator have high precedence then the latest(ch) operator
rpn = rpn + "" + sp;//appending with string
//rpn = rpn + "" + sp;
//appending the operator with the post fix string
}
else if (ch == '*' || ch == '/')//for division and multiplication
{
if (sp == 'e' || sp == '(' || sp=='{' || sp=='[')
//when stack will empty means this operator
//will perform after the first save operator
{
if (sp == '(' || sp == '{' || sp == '[')
stk.push(sp);
stk.push(ch);//push that operator
break;
//obviously for next operator and stack
//is now waiting for next opertaor
}
else if (sp == '/' || sp == '*')//for equal precedence
{
rpn = rpn + "" + sp;
//When same precedence then solve from left to right
stk.push(ch);
//now pushing the latest same presendece operator
break;
//after it will break obviously before
//it lies a low precedence
}
else if (sp == '+' || sp == '-')//for low precedence
{
stk.push(sp);
//because it will not append with post fix
//string this time and its presedence
//is less so again push in stack
stk.push(ch);//pushing high precedecne operator
break;
//after it will break obviously before
//it lies a low precedence
}
else
//appending with array because these operator
//have high precedence then the latest(ch) operator
rpn = rpn + "" + sp;//appending with string
}
}
}
i++;
//incrementing for next character in the string and termenating the loop
}
while (true)
//for appending the remaining operator
//in the que it is infinite because dont
//know the number of the remaining operator in the que
{
char sp = stk.pop();//get the remaining operator from the que
if (sp == 'e')//checking the que for empty
break;//terminating the loop because now que is empty
if ( sp == '[')
Console.WriteLine("Pakistan6");
rpn = rpn + "" + sp;//appending the operators with the post fix expression
}
return rpn;//returnin the reverse polish notation or post fix expression
}
}
#region For Key Pressing protected static void myHandler(object sender, ConsoleCancelEventArgs args) { // Announce that the event handler has been invoked. Console.WriteLine("\nThe read operation has been interrupted."); // Announce which key combination was pressed. Console.WriteLine(" Key pressed: {0}", args.SpecialKey); // Announce the initial value of the Cancel property. Console.WriteLine(" Cancel property: {0}", args.Cancel); // Set the Cancel property to true to prevent the process from terminating. Console.WriteLine("Setting the Cancel property to true..."); args.Cancel = true; // Announce the new value of the Cancel property. Console.WriteLine(" Cancel property: {0}", args.Cancel); Console.WriteLine("The read operation will resume...\n"); } #endregion
Hetrogenius Testing
class Heterogeneous_Brackets_Validity { public int check(string exp) { stack stk = new stack(exp.Length); int i = 0; while (i < exp.Length) { char ch = exp[i]; if (ch == '(' || ch == '{' || ch == '[') stk.push(ch); else if (ch == ')' || ch == '}' || ch == ']') { char sp = stk.pop(); if (sp == 'e') return 0; else if (ch == ')' && sp == '(') { } else if (ch == '}' && sp == '{') { } else if (ch == ']' && sp == '[') { } else return 0; } i++; } if(stk .pop ()=='e') return 1; else return 0; } }
How to use it?
first u by pressing the right arrow u will enter in the process then u will give the express for example 1+2 and then u will give the addressing mode for example 0,1,2 or 3
Output:
You can see the output on the console window but also in the C drive with the name of output file
Instruction:
Please if u have any file of .txt by the name of output in C drive first cut that file from there because it will be change with the output of the expression
And there are so many others techniques are used in the project.
Please reply me with your useful comments related to the project.
At this email address smarty4u13@yahoo.com