public void evaluate(string postexp, string infix)
        {
            FileStream   aFile = new FileStream("C:/Output.txt", FileMode.Create, FileAccess.Write);
            StreamWriter sw    = new StreamWriter(aFile);

            byte[]   chr = new byte[100];
            int      i = 0, size = postexp.Length;
            intstack stk = new intstack(size);

            while (i < size)
            {
                char ch = postexp[i];
                if (ch == '#')
                {
                    double operand = Convert.ToInt32(postexp[++i]) - 48;
                    stk.push(-1 * operand);
                }
                else if (ch >= '0' && ch <= '9')
                {
                    double operand = Convert.ToInt32(ch) - 48;
                    stk.push(operand);
                }
                else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
                {
                    double op1 = stk.pop();
                    double op2 = stk.pop();
                    if (ch == '+')
                    {
                        sw.WriteLine("ADD {0},{1},{2}", "reg", op2, op1);
                        double reg = op2 + op1;
                        stk.push(reg);
                    }
                    else if (ch == '-')
                    {
                        sw.WriteLine("SUB {0},{1},{2}", "reg", op2, op1);
                        double reg = op2 - op1;
                        stk.push(reg);
                    }
                    else if (ch == '*')
                    {
                        sw.WriteLine("MUL {0},{1},{2}", "reg", op2, op1);
                        double reg = op2 * op1;
                        stk.push(reg);
                    }
                    else if (ch == '/')
                    {
                        sw.WriteLine("DIV {0},{1},{2}", "reg", op2, op1);
                        double reg = op2 / op1;
                        stk.push(reg);
                    }
                }
                i++;
            }
            sw.WriteLine(" ");
            sw.WriteLine(" ");
            sw.WriteLine("\t\t\tResult:\t{0}", stk.pop());
            sw.WriteLine("\n\n\t\tPrepeared By:\n\t\t\t\tBenjamin Ngarambe");
            sw.Close();
        }
        //this function will take the postfix expression then solve that expression according to the one address mode
        public void evaluate(string postexp, string infix)
        {
            FileStream   aFile = new FileStream("C:/Output.txt", FileMode.Create, FileAccess.Write);//It is open in create mode because if there is any file by the name of output is already (in case of second expevaluation)remove that file first
            StreamWriter sw    = new StreamWriter(aFile);

            byte[]   chr = new byte[100];
            int      i = 0, size = postexp.Length;
            intstack stk = new intstack(size);

            while (i < size)
            {
                char ch = postexp[i];
                if (ch == '#')//for negative sign
                {
                    double operand = Convert.ToInt32(postexp[++i]) - 48;
                    stk.push(-1 * operand);
                }
                else if (ch >= '0' && ch <= '9')//for converting into the digit from character
                {
                    double operand = Convert.ToInt32(ch) - 48;
                    stk.push(operand);
                }
                else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')//for operators
                {
                    double op1 = stk.pop();
                    double op2 = stk.pop();
                    if (ch == '+')
                    {
                        sw.WriteLine("LOAD {0}", op2);
                        double reg = op2;
                        sw.WriteLine("ADD {0}", op1);
                        reg += op1;
                        sw.WriteLine("STOR {0}", "stack");
                        stk.push(reg);
                    }
                    else if (ch == '-')
                    {
                        sw.WriteLine("LOAD {0}", op2);
                        double reg = op2;
                        sw.WriteLine("SUB {0}", op1);
                        reg -= op1;
                        sw.WriteLine("STOR {0}", "stack");
                        stk.push(reg);
                    }
                    else if (ch == '*')
                    {
                        sw.WriteLine("LOAD {0}", op2);
                        double reg = op2;
                        sw.WriteLine("MUL {0}", op1);
                        reg *= op1;
                        sw.WriteLine("STOR {0}", "stack");
                        stk.push(reg);
                    }
                    else if (ch == '/')
                    {
                        sw.WriteLine("LOAD {0}", op2);
                        double reg = op2;
                        sw.WriteLine("DIV {0}", op1);
                        reg /= op1;
                        sw.WriteLine("STOR {0}", "stack");
                        stk.push(reg);
                    }
                }
                i++;
            }
            sw.WriteLine(" ");
            sw.WriteLine(" ");
            sw.WriteLine("\t\t\tMOV  Result:\t{0}", stk.pop());
            sw.WriteLine("\n\n\t\tPrepeared By:\n\t\t\t\tBenjamin Ngarambe");
            sw.Close();
        }
Пример #3
0
        //taking postfix expression into postexp and infix in infix(infix will take just for store in the output)
        public void evaluate(string postexp, string infix)
        {
            FileStream   aFile = new FileStream("C:/Output.txt", FileMode.Create, FileAccess.Write);
            StreamWriter sw    = new StreamWriter(aFile);

            sw.WriteLine("\t\tAllmighty increase my knowlege");
            sw.WriteLine("\tSharing Knowledge is wisdom & hiding knowledge is curse\n");
            sw.WriteLine("\t\tExpression:\t{0}", infix);
            int      i = 0, size = postexp.Length;                       //size have the length of the postfix expression and i variable will count it
            intstack stk = new intstack(size);                           //creating the stack for storing the operand after conversion into integer from character

            while (i < size)                                             //read untill the expression will end
            {
                char ch = postexp[i];                                    //geting the character of the expression
                if (ch == '#')                                           //for minus operand
                {
                    double operand = Convert.ToInt32(postexp[++i]) - 48; //subtract 48 for converting the character into the integer
                    stk.push(-1 * operand);
                }
                else if (ch >= '0' && ch <= '9')//checking for the operand
                {
                    double operand = Convert.ToInt32(ch) - 48;
                    sw.WriteLine("PUSH {0}", operand);
                    stk.push(operand);
                }
                else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')//checking for the operator
                {
                    double op1 = stk.pop();
                    double op2 = stk.pop();
                    if (ch == '+')
                    {
                        sw.WriteLine("ADD");
                        double reg = op2 + op1;
                        stk.push(reg);
                    }
                    else if (ch == '-')
                    {
                        sw.WriteLine("SUB");
                        double reg = op2 - op1;
                        stk.push(reg);
                    }
                    else if (ch == '*')
                    {
                        sw.WriteLine("MUL");
                        double reg = op2 * op1;
                        stk.push(reg);
                    }
                    else if (ch == '/')
                    {
                        sw.WriteLine("DIV");
                        double reg = op2 / op1;
                        stk.push(reg);
                    }
                }
                i++;//incrementing for reading the next character in the postfix string and termenating the loop
            }
            sw.WriteLine(" ");
            sw.WriteLine(" ");
            sw.WriteLine("\t\t\tPOP  Result:\t{0}", stk.pop());
            sw.WriteLine("\n\n\t\tPrepeared By:\n\t\t\t\tBenjamin Ngarambe");
            sw.Close();//closing the file
        }