Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I need critique on this code

I need critique on this code

Scheduled Pinned Locked Moved C#
data-structuresregex
21 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sundeepan Sen
    wrote on last edited by
    #1

    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);
    
    P D E 3 Replies Last reply
    0
    • S Sundeepan Sen

      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);
      
      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I don't think you need tempArray. How does it handle malformed input? Here's[^] my infix-to-postfix code; there are others on here.

      S 1 Reply Last reply
      0
      • S Sundeepan Sen

        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);
        
        D Offline
        D Offline
        Dan Mos
        wrote on last edited by
        #3

        Here I am criticizing it :-D It's too long and too late at night for me :zzz: . Now it's joke time :cool

        S 1 Reply Last reply
        0
        • S Sundeepan Sen

          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);
          
          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          I would fire you on the spot for putting regions in a method and not maintaining consistent capitalization of variables.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

          S 2 Replies Last reply
          0
          • E Ennis Ray Lynch Jr

            I would fire you on the spot for putting regions in a method and not maintaining consistent capitalization of variables.

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

            S Offline
            S Offline
            Sundeepan Sen
            wrote on last edited by
            #5

            lol...I know exactly which variable name you are referring to.

            1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              I would fire you on the spot for putting regions in a method and not maintaining consistent capitalization of variables.

              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

              S Offline
              S Offline
              Sundeepan Sen
              wrote on last edited by
              #6

              Why are regions in a method a bad idea? I ask this risking sounding like a fool.

              D A 2 Replies Last reply
              0
              • S Sundeepan Sen

                Why are regions in a method a bad idea? I ask this risking sounding like a fool.

                D Offline
                D Offline
                Dan Mos
                wrote on last edited by
                #7

                Regions are intended to be used in GROUPING methods not splitting one method's body. [EDIT] Example:

                #region Events//put here all/most of the event(s) handling code

                #region Drawing//put here all the drawing methods

                #region YouNameIt// put here all the...

                [/EDIT]

                S P 2 Replies Last reply
                0
                • P PIEBALDconsult

                  I don't think you need tempArray. How does it handle malformed input? Here's[^] my infix-to-postfix code; there are others on here.

                  S Offline
                  S Offline
                  Sundeepan Sen
                  wrote on last edited by
                  #8

                  It only handles inputs in a*b-c/d format. The code is very basic, written in an non elegant way. I will look at your code, it might give me some insights.

                  1 Reply Last reply
                  0
                  • D Dan Mos

                    Regions are intended to be used in GROUPING methods not splitting one method's body. [EDIT] Example:

                    #region Events//put here all/most of the event(s) handling code

                    #region Drawing//put here all the drawing methods

                    #region YouNameIt// put here all the...

                    [/EDIT]

                    S Offline
                    S Offline
                    Sundeepan Sen
                    wrote on last edited by
                    #9

                    Ahh...I understand. Thanks!

                    D 1 Reply Last reply
                    0
                    • D Dan Mos

                      Here I am criticizing it :-D It's too long and too late at night for me :zzz: . Now it's joke time :cool

                      S Offline
                      S Offline
                      Sundeepan Sen
                      wrote on last edited by
                      #10

                      LOL-i.pop();

                      D 1 Reply Last reply
                      0
                      • S Sundeepan Sen

                        Ahh...I understand. Thanks!

                        D Offline
                        D Offline
                        Dan Mos
                        wrote on last edited by
                        #11

                        for nothing

                        S 1 Reply Last reply
                        0
                        • D Dan Mos

                          for nothing

                          S Offline
                          S Offline
                          Sundeepan Sen
                          wrote on last edited by
                          #12

                          Oh c'mon my friend...your statement helped me more than any other reading I did today :-D

                          D 1 Reply Last reply
                          0
                          • S Sundeepan Sen

                            Oh c'mon my friend...your statement helped me more than any other reading I did today :-D

                            D Offline
                            D Offline
                            Dan Mos
                            wrote on last edited by
                            #13

                            Ohh, well, in that case, glad that I could help :)

                            1 Reply Last reply
                            0
                            • S Sundeepan Sen

                              LOL-i.pop();

                              D Offline
                              D Offline
                              Dan Mos
                              wrote on last edited by
                              #14

                              and i.push() :-D

                              S 1 Reply Last reply
                              0
                              • D Dan Mos

                                and i.push() :-D

                                S Offline
                                S Offline
                                Sundeepan Sen
                                wrote on last edited by
                                #15

                                Sounds like candy...haha

                                1 Reply Last reply
                                0
                                • S Sundeepan Sen

                                  Why are regions in a method a bad idea? I ask this risking sounding like a fool.

                                  A Offline
                                  A Offline
                                  AspDotNetDev
                                  wrote on last edited by
                                  #16

                                  Also, if you are using a region in a method, it is a sure sign you should put some of your logic into another method and just call that method from your original method.

                                  [Forum Guidelines]

                                  S P 2 Replies Last reply
                                  0
                                  • A AspDotNetDev

                                    Also, if you are using a region in a method, it is a sure sign you should put some of your logic into another method and just call that method from your original method.

                                    [Forum Guidelines]

                                    S Offline
                                    S Offline
                                    Sundeepan Sen
                                    wrote on last edited by
                                    #17

                                    Thank you for your reply and the link! :-O

                                    A 1 Reply Last reply
                                    0
                                    • S Sundeepan Sen

                                      Thank you for your reply and the link! :-O

                                      A Offline
                                      A Offline
                                      AspDotNetDev
                                      wrote on last edited by
                                      #18

                                      You are welcome. FYI, the link is part of my generic signature and was not targeted at you specifically. Still, if you haven't read it yet, it is advisable that you do so. :)

                                      [Forum Guidelines]

                                      1 Reply Last reply
                                      0
                                      • D Dan Mos

                                        Regions are intended to be used in GROUPING methods not splitting one method's body. [EDIT] Example:

                                        #region Events//put here all/most of the event(s) handling code

                                        #region Drawing//put here all the drawing methods

                                        #region YouNameIt// put here all the...

                                        [/EDIT]

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #19

                                        That may be only a personal or company standard. One can use regions wherever one darn well wants to. I generally use them only to group methods and properties, but I may also use seperate files (partial classes) for that purpose if the code becomes large enough. In my RPN transformer I use regions to mark off less-important code that the reader may not be interested in -- it should at least help with documentation and readability -- I don't want to read through a bunch of error-handling code when I'm trying to get a feel for the process flow of a method.

                                        1 Reply Last reply
                                        0
                                        • A AspDotNetDev

                                          Also, if you are using a region in a method, it is a sure sign you should put some of your logic into another method and just call that method from your original method.

                                          [Forum Guidelines]

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #20

                                          Yes, but the additional method calls could become a bottleneck in a tight loop. And the resultant code could be even less readable. You need to examine each case on its own.

                                          A 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups