示例#1
0
        // method to add a node to the LinkedListStrings
        public void addNode(NodeString newNode)
        {
            // put the newNode at the front if the linked list is empty
            if (this.length == 0)
            {
                // set the head & tail to newNode
                this.head = newNode;
                this.tail = newNode;

                // update the list's length
                this.length++;
            }
            else if (this.length > 0)
            {
                // link the current tail to newNode - call setNodeAfter()
                this.tail.setNodeAfter(newNode);

                // link the newNode to current tail - call setNodeBefore()
                newNode.setNodeBefore(this.tail);

                // set the tail to newNode
                this.tail = newNode;

                // update the list's length
                this.length++;
            }
        }
示例#2
0
        /*
         * This method stores the operations compelted and results into temp linked list.
         * This linked list is then read in later by a different method to write the lines for the text file(s).
         * This is done to store all of the items into memory and once the session is exited or
         * the user decides to start a new session, the application will then write all the
         * necessary components to the correct files.
         *
         * This method is specially for conversion operations.
         */
        public static void storeForLogFileConversion(LinkedListStrings LL, string originalNum, string inputBase, string outputBase, string answer)
        {
            // build the string from the inputs
            string writeString = $"conv {inputBase} to {outputBase} {originalNum} = {answer}";

            // create a new nodeToAdd
            NodeString nodeToAdd = new NodeString(writeString);

            // place the writeString into the passed linked list
            LL.addNode(nodeToAdd);
        }
示例#3
0
        /*
         * This method stores the opeartions completed and results into a temp linked list.
         * This linked list is then read in later by a different method to write the lines to the text file(s).
         * This is done to store all of the items into memory and once the session is exited or the user
         * decides to start a new session, the application will then write all the necessary compnents to the correct files.
         * Great rescourse used heavily:
         * https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-create-a-file-or-folder
         *
         * This method is specifically for binary arithmetic operations.
         */
        public static void storeForLogFileArithmetic(LinkedListStrings LL, string num1, string num2, string operation, string answer)
        {
            // build the string from the inputs
            string writeString = $"arth {num1} {operation} {num2} = {answer}";

            // create a new nodeToAdd
            NodeString nodeToAdd = new NodeString(writeString);

            // place the writeString into the passed linked list
            LL.addNode(nodeToAdd);
        }
示例#4
0
        // this method returns the string contents of a node given an index
        public String contentsAtIndex(int index)
        {
            // int value to keep track of the current location
            int currLocation = 0;
            // node value to denote the current node being checked
            NodeString currNode = this.head;

            // loop through the linked list until the proper location is found
            while (currLocation < index)
            {
                // advance the node until location is found
                currNode = currNode.getNodeAfter();
                // advance currentLocation
                currLocation++;
            }
            // return the value at that location as an int
            return(currNode.getContents());
        }
示例#5
0
 // method to set the nodeAfter
 public void setNodeBefore(NodeString nodeBefore)
 {
     this.nodeBefore = nodeBefore;
 }
示例#6
0
 // method to set the nodeAfter
 public void setNodeAfter(NodeString nodeAfter)
 {
     this.nodeAfter = nodeAfter;
 }
示例#7
0
 // instance method for NodeString class
 public NodeString(string contents)
 {
     this.nodeBefore = null;
     this.nodeAfter  = null;
     this.contents   = contents;
 }
示例#8
0
 // instance method for a LinkedListStrings object - starts out empty
 public LinkedListStrings()
 {
     this.head   = null;
     this.tail   = null;
     this.length = 0;
 }
示例#9
0
        /*
         * This method reads in a file from the user and completes the operations requested.
         * This method reads the file in question, and rewrites the file based on the operations.
         * Each operation is completed and then stored into a linked list object. This is done to
         * allow easier rewrite of the file when all operations are complete.
         */
        public static void operateOnFile()
        {
            // call to choose the user file to operate on
            string fileLocation = chooseFile();

            // creation of LinkedListString object to keep track of line outputs (both arth and conv)
            LinkedListStrings lineOutputs = new LinkedListStrings();

            // reassign fileLocation for testing puproses
            //string logFiles = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "IT-Calculator Logs");
            //string fileLocation = System.IO.Path.Combine(logFiles, "testReadFile.txt");
            // read in the file
            using (System.IO.StreamReader stream = System.IO.File.OpenText(fileLocation))
            {
                // temp string value to hold the line contents
                string line;
                while ((line = stream.ReadLine()) != null)
                {
                    // char[] to allow for easier interaction on the line input
                    char[] lineChars = line.ToCharArray();

                    // determine if arth or conv (build the first chars into a string)
                    string operation = "";
                    for (int location = 0; location < 4; location++)
                    {
                        operation += lineChars[location];
                    }
                    // conditionals for arth
                    if (operation == "arth")
                    {
                        // go to location 5 to begin sorting correctly
                        string num1         = "";
                        string binOperation = "";
                        string num2         = "";

                        // int value to keep track of where in the char array currently are
                        int currLocation = 5;

                        // build num1
                        while (lineChars[currLocation].ToString() != " ")
                        {
                            // add to num1
                            num1 += lineChars[currLocation].ToString();
                            // advance currLocation
                            currLocation++;
                        }
                        // advance currLocation
                        currLocation++;

                        // build binOperation
                        while (lineChars[currLocation].ToString() != " ")
                        {
                            // add to binOperation
                            binOperation += lineChars[currLocation].ToString();
                            // advance currLocation
                            currLocation++;
                        }
                        // advance currLocation
                        currLocation++;

                        // build num2
                        while (currLocation < lineChars.Length)
                        {
                            if (lineChars[currLocation].ToString() != " ")
                            {
                                // add to num2
                                num2 += lineChars[currLocation].ToString();
                                // advance currLocation
                                currLocation++;
                            }
                            else
                            {
                                currLocation = lineChars.Length;
                            }
                        }

                        // create binaryOperation object to operate on and recieve an output
                        binaryOperation arithmeticOperation = new binaryOperation(num1, num2);
                        // store the output in a temp string value
                        string answer = arithmeticOperation.operate(binOperation);

                        // build the string to store inside of the linked list
                        string stringToStore = $"{operation} {num1} {binOperation} {num2} = {answer}";

                        // create node to store
                        NodeString nodeToAdd = new NodeString(stringToStore);
                        // add the node to the list
                        lineOutputs.addNode(nodeToAdd);
                    }
                    // conditionals for conv
                    else if (operation == "conv")
                    {
                        // go to location 5 to begin sorting correctly
                        string inputBase    = "";
                        string outputBase   = "";
                        string numtoConvert = "";

                        // int value to keep track of where in the char array currently are
                        int currLocation = 5;

                        // build inputBase
                        while (lineChars[currLocation].ToString() != " ")
                        {
                            // add to inputBase
                            inputBase += lineChars[currLocation].ToString();
                            // advance currLocation
                            currLocation++;
                        }
                        // advance currLocation
                        currLocation += 4;

                        // build outputBase
                        while (lineChars[currLocation].ToString() != " ")
                        {
                            // add to outputBase
                            outputBase += lineChars[currLocation].ToString();
                            // advance currLocation
                            currLocation++;
                        }
                        // advance currLocation
                        currLocation++;

                        // build numToConvert
                        while (currLocation < lineChars.Length)
                        {
                            if (lineChars[currLocation].ToString() != " ")
                            {
                                // add to numToConvert
                                numtoConvert += lineChars[currLocation].ToString();
                                // advance currLocation
                                currLocation++;
                            }
                            else
                            {
                                currLocation = lineChars.Length;
                            }
                        }

                        // create conversionOperation instance
                        conversionOperation convertOperation = new conversionOperation(inputBase, outputBase, numtoConvert);

                        // opreate on that instance
                        string answer = convertOperation.operate();

                        // build the string to store inside of the linked list
                        string stringToStore = $"{operation} {inputBase} to {outputBase} {numtoConvert} = {answer}";
                        // create node to store
                        NodeString nodeToAdd = new NodeString(stringToStore);
                        // add the node to the list
                        lineOutputs.addNode(nodeToAdd);
                    }
                    else
                    {
                        string     errorString = $"{line} = Label was incorrect on the line. Operation not complete.";
                        NodeString nodeToAdd   = new NodeString(errorString);
                        // add the node to the list
                        lineOutputs.addNode(nodeToAdd);
                    }
                }
                // close stream once loop is completed
                stream.Close();
            }

            // overwrite the file with the new information
            using (System.IO.StreamWriter stream = new System.IO.StreamWriter(fileLocation))
            {
                for (int location = 0; location < lineOutputs.length; location++)
                {
                    stream.WriteLine(lineOutputs.contentsAtIndex(location));
                }
                stream.Close();
            }
        }