forked from LucasCancio/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df77c48
commit 97819a6
Showing
9 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..]; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters