Skip to content

Commit

Permalink
Added operator handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbin44 committed Sep 20, 2020
1 parent 8345e9a commit 7bbb0c3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
5 changes: 3 additions & 2 deletions Conveyer/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
109 changes: 101 additions & 8 deletions Conveyer/OutputQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,119 @@ namespace Conveyer
{
class OutputQueue
{
static List<string> queue = new List<string>();
public OutputQueue(string item)
static List<char> queue = new List<char>(); //Queue is ALL the ingoing chars, including +-*/ A-Z 0-9
static List<string> result = new List<string>();//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);
}
Expand Down
22 changes: 20 additions & 2 deletions Conveyer/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7bbb0c3

Please sign in to comment.