Click here to Skip to main content
15,898,134 members
Home / Discussions / C#
   

C#

 
GeneralRe: creating dynamic array Pin
nuttynibbles11-Feb-10 16:22
nuttynibbles11-Feb-10 16:22 
GeneralRe: creating dynamic array Pin
Dave Kreskowiak11-Feb-10 18:14
mveDave Kreskowiak11-Feb-10 18:14 
GeneralRe: creating dynamic array Pin
nuttynibbles11-Feb-10 19:03
nuttynibbles11-Feb-10 19:03 
GeneralRe: creating dynamic array Pin
i gr811-Feb-10 20:09
i gr811-Feb-10 20:09 
GeneralRe: creating dynamic array Pin
Dave Kreskowiak12-Feb-10 1:15
mveDave Kreskowiak12-Feb-10 1:15 
GeneralRe: creating dynamic array Pin
Dave Doknjas12-Feb-10 11:35
Dave Doknjas12-Feb-10 11:35 
GeneralRe: creating dynamic array Pin
Dave Kreskowiak13-Feb-10 4:16
mveDave Kreskowiak13-Feb-10 4:16 
QuestionI need critique on this code Pin
Sundeepan Sen11-Feb-10 14:23
Sundeepan Sen11-Feb-10 14:23 
Just got back into writing code after 10 years. This is a infix to postfix converter. Can someone please critique this...

thanks

using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace Infix2Postfix
{
    public class Program
    {
        static void Main(string[] args)
        {
            #region variable declarations
            //declare and initialize the string to accept an expression in infix notation
            String stringInfix = "a+b/c-d";

            StringBuilder SbPostfix = new StringBuilder();
            Stack<char> stackGeneral = new Stack<char>();
            Stack<char> stackOperator = new Stack<char>();
            char[] tempArray = stringInfix.ToCharArray();

            Array.Reverse(tempArray);
            Regex regexAlphaNum = new Regex("[a-zA-Z0-9]");
            char temp;

            #endregion

            #region logic

            foreach (char ch in tempArray)
                stackGeneral.Push(ch);

            foreach (char ch in stackGeneral)
            {
                if (regexAlphaNum.IsMatch(Convert.ToString(ch)))
                    SbPostfix.Append(ch);
                else if (stackOperator.Count == 0)
                    stackOperator.Push(ch);
                else if (stackOperator.Count != 0)
                    switch (ch)
                    {
                        case '+':
                            temp = stackOperator.Peek();
                            if (temp == '*' || temp == '/')
                            {
                                while (stackOperator.Count > 0)
                                    SbPostfix.Append(stackOperator.Pop());
                                stackOperator.Push(ch);
                            }
                            else
                                stackOperator.Push(ch);
                            break;
                        case '-':
                            temp = stackOperator.Peek();
                            if (temp == '*' || temp == '/')
                            {
                                while (stackOperator.Count > 0)
                                    SbPostfix.Append(stackOperator.Pop());
                                stackOperator.Push(ch);
                            }
                            else
                                stackOperator.Push(ch);
                            break;
                        case '*':
                            temp = stackOperator.Peek();
                            if (temp == '*' || temp == '/')
                            {
                                while (stackOperator.Count > 0)
                                    SbPostfix.Append(stackOperator.Pop());
                                stackOperator.Push(ch);
                            }
                            else
                                stackOperator.Push(ch);
                            break;
                        case '/':
                            temp = stackOperator.Peek();
                            if (temp == '*' || temp == '/')
                            {
                                while (stackOperator.Count > 0)
                                    SbPostfix.Append(stackOperator.Pop());
                                stackOperator.Push(ch);
                            }
                            else
                                stackOperator.Push(ch);
                            break;
                    }

            }
            while (stackOperator.Count > 0)
                SbPostfix.Append(stackOperator.Pop());

            #endregion

            #region output to screen
            Console.WriteLine("Equation in infix notation: {0}", stringInfix);
            Console.Write("Equation in postfix/RPN notation: {0}", SbPostfix);
            Console.ReadKey();
            #endregion

        }
    }
}

AnswerRe: I need critique on this code Pin
PIEBALDconsult11-Feb-10 14:37
mvePIEBALDconsult11-Feb-10 14:37 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 16:11
Sundeepan Sen11-Feb-10 16:11 
JokeRe: I need critique on this code Pin
Dan Mos11-Feb-10 14:38
Dan Mos11-Feb-10 14:38 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 16:13
Sundeepan Sen11-Feb-10 16:13 
JokeRe: I need critique on this code Pin
Dan Mos11-Feb-10 16:19
Dan Mos11-Feb-10 16:19 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 16:20
Sundeepan Sen11-Feb-10 16:20 
AnswerRe: I need critique on this code Pin
Ennis Ray Lynch, Jr.11-Feb-10 15:48
Ennis Ray Lynch, Jr.11-Feb-10 15:48 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 15:51
Sundeepan Sen11-Feb-10 15:51 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 15:55
Sundeepan Sen11-Feb-10 15:55 
GeneralRe: I need critique on this code Pin
Dan Mos11-Feb-10 16:02
Dan Mos11-Feb-10 16:02 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 16:12
Sundeepan Sen11-Feb-10 16:12 
GeneralRe: I need critique on this code Pin
Dan Mos11-Feb-10 16:13
Dan Mos11-Feb-10 16:13 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 16:15
Sundeepan Sen11-Feb-10 16:15 
GeneralRe: I need critique on this code Pin
Dan Mos11-Feb-10 16:17
Dan Mos11-Feb-10 16:17 
GeneralRe: I need critique on this code Pin
PIEBALDconsult12-Feb-10 5:12
mvePIEBALDconsult12-Feb-10 5:12 
GeneralRe: I need critique on this code Pin
AspDotNetDev11-Feb-10 16:32
protectorAspDotNetDev11-Feb-10 16:32 
GeneralRe: I need critique on this code Pin
Sundeepan Sen11-Feb-10 16:34
Sundeepan Sen11-Feb-10 16:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.