Thread: Psuedocode

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    Psuedocode

    Does anyone know how this psuedocode can be made into C#

    if (partsNumber[i] is six characters in length and chars 1 and 2 are alpha
    and chars 3-6 are numeric
    Print Something here



    I will use a loop to iterate thru i. I just skipped that

    EXAMPLE AA1234

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Use a regular expression (regexp).

    Something like

    Code:
     '^[A-Z]{2}[0-9]{4}$'
    
    [A-Z]{2}  =  starts with 2 letters
    [0-9]{4}  =  ends with 4 numbers
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I wish I could thought of that on my own
    I'll give it a try.

    Thanks for the insight

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    heres what I have but it doesn't work and I don't know why

    Code:
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace Chapter6Problem6
    {
        class Program
        {
            const string INPUT_FILE = "\\Projects\\Chapter6Problem6\\Chapter6Problem6\\inventory.txt";
            const string OUTPUT_FILE = "\\Projects\\Chapter6Problem6\\Chapter6Problem6\\list.txt";
    
            static int records = 8;
            static uint i;
            static string lineIn;
    
            static int[] recordCode = new int[records + 1];
            static string[] partNumber = new string[records + 1];
            static string[] partDescription = new string[records + 1];
            static int[] balance = new int[records + 1];
    
            static StreamReader fileIn;
            static StreamWriter fileOut;
    
            static void Main()
            {
                if (OpenFiles())
                {
                    InputData();
                    ValidateData(lineIn,i);
                    CloseFiles();
                }
            }
    
            static bool OpenFiles()
            {
                bool successOpeningFile = true;
    
                           if (File.Exists(INPUT_FILE))
                {
                    fileIn = File.OpenText(INPUT_FILE);
                    Console.WriteLine("{0} was opened!", INPUT_FILE);
                }
                else
                {
                    Console.WriteLine("Error: {0} does not exist!", INPUT_FILE);
                    successOpeningFile = false;
                }
    
                fileOut = File.CreateText(OUTPUT_FILE);
    
                if (File.Exists(OUTPUT_FILE))
                    Console.WriteLine("{0} was created!", OUTPUT_FILE);
                else
                {
                    Console.WriteLine("Error: {0} could not be created!", OUTPUT_FILE);
                    successOpeningFile = false;
                }
    
                return successOpeningFile;
    
            }
    
            static void InputData()
            {
                while ((lineIn = fileIn.ReadLine()) != null)
                {
                    ParseLineIn(lineIn, i);
                    i++;
                }
            }
    
            static void ParseLineIn(string lineIn, uint i)
            {
                string[] words = new string[2];
    
                lineIn = lineIn.Trim();
                while (Regex.IsMatch(lineIn, "[ ] {2}"))
                    lineIn = lineIn.Replace("  ", " ");
                words = lineIn.Split(' ');
                recordCode[i] = int.Parse(words[0]);
                partNumber[i] = words[1];
                partDescription[i] = words[2];
                balance[i] = int.Parse(words[3]);
            }
    
            static void ValidateData(string partnumber, uint i)
            {
                while(recordCode[i] == 11 && balance[i] == 0)
                {
                    while (Regex.IsMatch(partnumber, "[A-Z] {2} [0-9] {4}"))
                        Display();
                }
            }
            
            static void Display()
            {
                int i;
    
                fileOut.WriteLine("Record Code         Part #           Description                Balance");
                fileOut.WriteLine("-----------------------------------------------------------------------");
    
                for(i = 0; i <= records; i++)
                    fileOut.WriteLine("{0}           {1}            {2}                      {3}", recordCode[i], partNumber[i], partDescription[i], balance[i]);
                fileOut.WriteLine("-----------------------------------------------------------------------");
                
            }
    
            static void CloseFiles()
            {
                fileIn.Close();
                fileOut.Close();
            }
    
        }
    }

    the program compiles and the text is created but no output for some reason

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You put spaces in the regex string for some reason. That makes the match something totally different.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    ParseLineIn does something totally different
    ValidateData is the method that I'm trying to figure out

    ParseLineIn what it does if the input file encounters more than one space it will disregard that space and make just one space between data I think something like that

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I think this is one for my teacher I know next to nothing about Regular expressions good idea though

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm not talking about ParseLineIn() specifically. Your ValidateDate() method has the erroneous spaces in the regex string as well.

    Quote Originally Posted by artistunknown
    ...it will disregard that space and make just one space between data I think something like that
    You speak as if not only did you not write that method, but you don't even understand what it does.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
    the trim() method takes the spaces out of the Input File lineIn
    then while(Regex.IsMatch(lineIn,[ ] {2}) sees if there is two spaces in the Input file
    if there is then the Replace("  ", " ") takes the 2 spaces out and replaces it with one 
    then the split() method splits the string into substrings 
    then the Parse() method converts the string to integers 
    
    something like that

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
    I'm not sure but I think you can write the regular expression more than one way I know what your talking about the spaces I don't claim to understand it but I think there's different ways to write the code where the spaces don't matter I've look up regular expressions on the internet and most of them don't have spaces but its written differently too the space between the last ] {2}
    don't matter it means check for 2 spaces it works that's all I know Sorry I don't want to come off sounding rude So I apologize

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    I think I was interpretting the problem wrong here's the problem

    A parts inventory record contains the following fields:

    1. record code (only code 11 is valid)
    2. part number (six characters; two alpha and four numeric, EX. AA1234
    3. part description
    4. inventory balance

    Design an algorithm that will read the file of parts inventory records, validate the record code and the part number on each record, and print the details of all valid inventory records that have an inventory balance equal to zero.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    anyway it doesn't say how to validate the part number so I did something a little different here's my code:

    Code:
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace Chapter6Problem6
    {
        class Program
        {
            const string INPUT_FILE = "\\Projects\\Chapter6Problem6\\Chapter6Problem6\\inventory.txt";
            const string OUTPUT_FILE = "\\Projects\\Chapter6Problem6\\Chapter6Problem6\\list.txt";
    
            static int records = 7;
            static uint i;
            static string lineIn;
    
       
            static string[] words = new string[2];
            static string[] recordCode = new string[records + 1];
            static string[] partNumber = new string[records + 1];
            static string[] partDescription = new string[records + 1];
            static string[] balance = new string[records + 1];
    
            static StreamReader fileIn;
            static StreamWriter fileOut;
    
            static void Main()
            {
                if (OpenFiles())
                {
                    InputData();
                 
                    Display();
                   
                    CloseFiles();
                }
            }
    
            static bool OpenFiles()
            {
                bool successOpeningFile = true;
    
                           if (File.Exists(INPUT_FILE))
                {
                    fileIn = File.OpenText(INPUT_FILE);
                    Console.WriteLine("{0} was opened!", INPUT_FILE);
                }
                else
                {
                    Console.WriteLine("Error: {0} does not exist!", INPUT_FILE);
                    successOpeningFile = false;
                }
    
                fileOut = File.CreateText(OUTPUT_FILE);
    
                if (File.Exists(OUTPUT_FILE))
                    Console.WriteLine("{0} was created!", OUTPUT_FILE);
                else
                {
                    Console.WriteLine("Error: {0} could not be created!", OUTPUT_FILE);
                    successOpeningFile = false;
                }
    
                return successOpeningFile;
    
            }
    
           static void InputData()
            {
                
    
                while ((lineIn = fileIn.ReadLine()) != null)
                {
                    ParseLineIn(lineIn, i);
                    i++;
                }
            }
    
            static void ParseLineIn(string lineIn, uint i)
            {
               
    
                lineIn = lineIn.Trim();
                while (Regex.IsMatch(lineIn, "[ ] {2}"))
                    lineIn = lineIn.Replace("  "," ");
                words = lineIn.Split(' ');
                recordCode[i] = words[0];
                partNumber[i] = words[1];
                partDescription[i] = words[2];
                balance[i] = words[3];        
                
            }
    
            static void Display()
            {
              
    
                fileOut.WriteLine("Record Code         Part #           Description                Balance");
                fileOut.WriteLine("-----------------------------------------------------------------------");
    
                for (i = 0; i <= records; i++)
                {
                    if (partNumber[i].Length == 6)
                        if(Convert.ToInt32(recordCode[i]) == 11) 
                            if(Convert.ToInt32(balance[i]) == 0)
                                fileOut.WriteLine("{0}                  {1}           {2,12}               {3,3}", recordCode[i], partNumber[i], partDescription[i], balance[i]);
                }
                fileOut.WriteLine("-----------------------------------------------------------------------");
                
            }
    
            static void CloseFiles()
            {
                fileIn.Close();
                fileOut.Close();
            }
    
        }
    }

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    here's the attachments if you want to look at them
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You're wrong about the regular expression spacing. Your code actually checks for three spaces, not two, and the rest of yours doesn't work because of spacing. The space in a regular expression is interpreted as a string literal space, and if you have a space before the repetition count, the repetition count applies to the number of spaces it looks for.

    Try this program and look at the results:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static Dictionary<string,string> testCases = new Dictionary<string,string>();
    
            static void SetupTestCases()
            {
                testCases.Add("7", "one digit");
                testCases.Add("76", "two digits");
                testCases.Add("7 ", "one digit and one space");
                testCases.Add("76 ", "two digits and one space");
                testCases.Add("7  ", "one digit and two spaces");
                testCases.Add("76  ", "two digits and two spaces");
            }
    
    
            static void TryExpression(string expression)
            {
                bool match = false;
                foreach (string testCase in testCases.Keys)
                {
                    if (Regex.IsMatch(testCase,expression))
                    {
                        Console.WriteLine(string.Format("Regular expression \"{0}\" matches {1}",expression, testCases[testCase]));
                        match = true;
                    }
                }
                if (!match)
                {
                    Console.WriteLine(string.Format("Regular expression \"{0}\" did not match any test case", expression));
                }
                Console.WriteLine();
            }
    
            static void Main(string[] args)
            {
                SetupTestCases();
                TryExpression("[0-9]");
                TryExpression("[0-9]{2}");
                TryExpression("[0-9] ");
                TryExpression("[0-9] {2}");
                TryExpression("^[0-9]$");
                TryExpression("^[0-9]{2}$");
                TryExpression("^[0-9] $");
                TryExpression("^[0-9] {2}$");
                Console.ReadLine();
            }
        }
    }
    There is a RegexOptions to ignore pattern whitespace, but this is C# specific, and not a feature of all regular expressions.
    Last edited by Cat; 08-19-2015 at 05:46 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by artistunknown View Post
    Does anyone know how this psuedocode can be made into C#
    Strictly speaking, it can't be. Because if it could, then it wouldn't be psuedocode any more.

Popular pages Recent additions subscribe to a feed