コード例 #1
0
ファイル: Form1.cs プロジェクト: willbushie/IT-Calculator
        // button press event to convert the input base from textBox1 to the desired output base into textBox2
        // this method uses over 20 if statements, will look at streamlining this code in the future
        private void button1_Click(object sender, EventArgs e)
        {
            // create instance of converionOperation
            conversionOperation operation = new conversionOperation(comboBox2.Text, comboBox1.Text, textBox1.Text);

            // execute the operation
            textBox2.Text = operation.operate();
        }
コード例 #2
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();
            }
        }