diff --git a/Conveyer/Controller.cs b/Conveyer/Controller.cs index dfcae44..2d7733d 100644 --- a/Conveyer/Controller.cs +++ b/Conveyer/Controller.cs @@ -12,14 +12,15 @@ class Controller static void Main(string[] args) { - // Check for correct file format and length + // Check for correct file format and length if (!(args.Length == 1 && args[0].EndsWith(".coy"))) { Console.WriteLine("Code not a valid file"); Console.ReadKey(); return; } - filePath = args[0]; + + filePath = args[0]; height = File.ReadLines(filePath).Count(); FileHandler fh = new FileHandler(filePath); diff --git a/Conveyer/OutputQueue.cs b/Conveyer/OutputQueue.cs index 7d7c0ff..e8e5c55 100644 --- a/Conveyer/OutputQueue.cs +++ b/Conveyer/OutputQueue.cs @@ -7,26 +7,119 @@ namespace Conveyer { class OutputQueue { - static List queue = new List(); - public OutputQueue(string item) + static List queue = new List(); //Queue is ALL the ingoing chars, including +-*/ A-Z 0-9 + static List result = new List();//Result is the final computation where +-*/ has been calculated and removed from the list. + + static Regex dReg = new Regex("[+/*\\-]"); //Reg for operators + static Regex cReg = new Regex("^[a-zA-Z0-9]"); //Reg for letters and numbers + + public OutputQueue(char item) { - Regex reg = new Regex("^[a-zA-Z0-9]"); - if (reg.IsMatch(item)) //If input is a char to print + if (cReg.IsMatch(item.ToString()) || dReg.IsMatch(item.ToString())) //If input is a char to print { queue.Add(item); } - else //Ignore string + } + + public static void Machine() + { + float number = 0.0f; + int startIndex = 0; //start index for current operator to offset the starting index after every operator + int lastOpIndex = 0;//Index of the last operator so that it can be used as a starting index to output all chars after the last operator. + + bool noMachines = true; + + int i = 0; + for (; i < queue.Count; i++)//Check if i contains any operators +-*/ + { + if (dReg.IsMatch(queue[i].ToString()))//If i is an operator, + - * / + { + noMachines = false; //If there is atleast one machine on the belt + lastOpIndex = i; + for (int j = startIndex; j < i; j++) //Iterate through all previous numbers + { + if (char.IsLetter(queue[j])) + { + result.Add(queue[j].ToString()); + continue; + } + + if (number == 0) + { + number = (float)char.GetNumericValue(queue[j]); + continue; + } + + if (queue[i] == '+') + { + if (cReg.IsMatch(queue[j].ToString())) + { + number += (float)char.GetNumericValue(queue[j]); + continue; + } + } + + if (queue[i] == '-') + { + if (cReg.IsMatch(queue[j].ToString())) + { + number -= (float)char.GetNumericValue(queue[j]); + continue; + } + } + + if (queue[i] == '*') + { + if (cReg.IsMatch(queue[j].ToString())) + { + number *= (float)char.GetNumericValue(queue[j]); + continue; + } + } + + if (queue[i] == '/') + { + if (cReg.IsMatch(queue[j].ToString())) + { + number /= (float)char.GetNumericValue(queue[j]); + continue; + } + } + } + result.Add(number.ToString()); + startIndex = i + 1; + number = 0; + } + } + + if(noMachines == true) + { + for (int s = 0; s < queue.Count; s++) + { + if (cReg.IsMatch(queue[s].ToString())) + { + result.Add(queue[s].ToString()); + } + } + } + else { - item = null; + //Add letters if there are any after the last operator + for (int t = lastOpIndex; t < queue.Count; t++) + { + if (char.IsLetter(queue[t])) //Add letters to array in correct order + result.Add(queue[t].ToString()); + } } + PrintResult(); } public static void PrintResult() { string output = ""; - for (int i = 0; i < queue.Count; i++) + for (int i = 0; i < result.Count; i++) { - output += queue[i]; + output += result[i]; } Console.WriteLine("Items on belt adds up to: " + output); } diff --git a/Conveyer/Scanner.cs b/Conveyer/Scanner.cs index 8735444..27bfa07 100644 --- a/Conveyer/Scanner.cs +++ b/Conveyer/Scanner.cs @@ -41,9 +41,27 @@ void Scan(int length, char[,] _map) { if (reg.IsMatch(_map[h, l].ToString())) //If current pos has a character to print, queue it { - OutputQueue oQueue = new OutputQueue(_map[h, l].ToString());//Add to output queue + OutputQueue oQueue = new OutputQueue(_map[h, l]);//Add to output queue } + if(_map[h, l] == '+') + { + OutputQueue oQueue = new OutputQueue(_map[h, l]);//Add to output queue + } + if (_map[h, l] == '-') + { + OutputQueue oQueue = new OutputQueue(_map[h, l]);//Add to output queue + } + if (_map[h, l] == '*') + { + OutputQueue oQueue = new OutputQueue(_map[h, l]);//Add to output queue + } + if (_map[h, l] == '/') + { + OutputQueue oQueue = new OutputQueue(_map[h, l]);//Add to output queue + } + + //Console.WriteLine("h = " + h); //Console.WriteLine("l = " + l); #region MazeCheck @@ -95,7 +113,7 @@ void Scan(int length, char[,] _map) if (_map[h, l + 1] == '~' || _map[h, l - 1] == '~' || _map[h + 1, l] == '~' || _map[h - 1, l] == '~') //End of conveyor belt { //Console.WriteLine("Mission Complete!"); - OutputQueue.PrintResult(); //Print all the letters on the conveoyr belt + OutputQueue.Machine(); //Check for machines and then print result scanFinished = true; break;