Skip to content

Commit

Permalink
Interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCancio committed Jan 14, 2021
1 parent df77c48 commit 97819a6
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 0 deletions.
32 changes: 32 additions & 0 deletions PatternsComportamentais/Interpreter/CentenasExpressao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace Interpreter
{
public class CentenasExpressao : Expressao
{
public override string Cinco()
{
return "D";
}

public override string Nove()
{
return "CM";
}

public override string Quatro()
{
return "CD";
}

public override string Um()
{
return "C";
}

public override int Multiplicar()
{
return 100;
}
}
}
17 changes: 17 additions & 0 deletions PatternsComportamentais/Interpreter/Contexto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Interpreter
{
public class Contexto
{
public Contexto(string input)
{
Input = input;
}

public string Input { get; set; }
public int Output { get; set; }


}
}
32 changes: 32 additions & 0 deletions PatternsComportamentais/Interpreter/DezenasExpressao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace Interpreter
{
public class DezenasExpressao : Expressao
{
public override string Cinco()
{
return "L";
}

public override string Nove()
{
return "XC";
}

public override string Quatro()
{
return "XL";
}

public override string Um()
{
return "X";
}

public override int Multiplicar()
{
return 10;
}
}
}
42 changes: 42 additions & 0 deletions PatternsComportamentais/Interpreter/Expressao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
namespace Interpreter
{
public abstract class Expressao
{
public abstract string Um();
public abstract string Quatro();
public abstract string Cinco();
public abstract string Nove();
public abstract int Multiplicar();

public void Interpertador(Contexto contexto)
{
if (contexto.Input.Length == 0)
{
return;
}

if (contexto.Input.StartsWith(Nove()))
{
contexto.Output += (9 * Multiplicar());
contexto.Input = contexto.Input[2..];
}
else if (contexto.Input.StartsWith(Quatro()))
{
contexto.Output += (4 * Multiplicar());
contexto.Input = contexto.Input[2..];
}
else if (contexto.Input.StartsWith(Cinco()))
{
contexto.Output += (5 * Multiplicar());
contexto.Input = contexto.Input[1..];
}

while (contexto.Input.StartsWith(Um()))
{
contexto.Output += (1 * Multiplicar());
contexto.Input = contexto.Input[1..];
}
}
}
}
8 changes: 8 additions & 0 deletions PatternsComportamentais/Interpreter/Interpreter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
32 changes: 32 additions & 0 deletions PatternsComportamentais/Interpreter/MilharesExpressao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace Interpreter
{
public class MilharesExpressao : Expressao
{
public override string Cinco()
{
return " ";
}

public override string Nove()
{
return " ";
}

public override string Quatro()
{
return " ";
}

public override string Um()
{
return "M";
}

public override int Multiplicar()
{
return 1000;
}
}
}
26 changes: 26 additions & 0 deletions PatternsComportamentais/Interpreter/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;

namespace Interpreter
{
class Program
{
static void Main(string[] args)
{
string romano = "MCMXXVIII";
Contexto contexto = new Contexto(romano);

List<Expressao> expressoes = new List<Expressao>();
expressoes.Add(new MilharesExpressao());
expressoes.Add(new CentenasExpressao());
expressoes.Add(new DezenasExpressao());

foreach (Expressao expressao in expressoes)
{
expressao.Interpertador(contexto);
}

Console.WriteLine($"{romano} == {contexto.Output}");
}
}
}
32 changes: 32 additions & 0 deletions PatternsComportamentais/Interpreter/UnidadeExpressao.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace Interpreter
{
public class UnidadeExpressao : Expressao
{
public override string Cinco()
{
return "V";
}

public override string Nove()
{
return "IX";
}

public override string Quatro()
{
return "IV";
}

public override string Um()
{
return "I";
}

public override int Multiplicar()
{
return 1;
}
}
}
6 changes: 6 additions & 0 deletions PatternsComportamentais/PatternsComportamentais.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibility", "Ch
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Command", "Command\Command.csproj", "{0F54B039-B529-4FB1-BEE8-87D210A08952}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Interpreter", "Interpreter\Interpreter.csproj", "{3854DE72-2962-43F0-8E6C-3112F6AEE85A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{0F54B039-B529-4FB1-BEE8-87D210A08952}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F54B039-B529-4FB1-BEE8-87D210A08952}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F54B039-B529-4FB1-BEE8-87D210A08952}.Release|Any CPU.Build.0 = Release|Any CPU
{3854DE72-2962-43F0-8E6C-3112F6AEE85A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3854DE72-2962-43F0-8E6C-3112F6AEE85A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3854DE72-2962-43F0-8E6C-3112F6AEE85A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3854DE72-2962-43F0-8E6C-3112F6AEE85A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 97819a6

Please sign in to comment.