Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solución en C# #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions eltoby/RomanNumerals/RomanNumerals.Data/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RomanNumerals.Data")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RomanNumerals.Data")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("be4a5b92-3a02-4ced-8c09-980b792b4a31")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;
using RomanNumerals.Objects;
namespace RomanNumerals.Data
{
public class ReplacementPatternCatalog
{
static ReplacementPatternCatalog instance;
List<ReplacementPattern> patterns;
private ReplacementPatternCatalog()
{
patterns = new List<ReplacementPattern>();

RomansCatalog romCatalog =RomansCatalog.GetInstance();
foreach (Roman r in romCatalog.GetAll())
{
Roman higher = romCatalog.Higher(r);
if (r.Repeatable)
{
if (higher == null)
continue;

StringBuilder patternBuilder = new StringBuilder();
patternBuilder.Append(r.Letter[0], 4);
patterns.Add(new ReplacementPattern(patternBuilder.ToString(), string.Concat(r.Letter, higher.Letter)));
}
else
{
Roman lower = romCatalog.Lower(r);
if (lower == null)
continue;

StringBuilder patternBuilder = new StringBuilder();
patternBuilder.Append(r.Letter);
patternBuilder.Append(lower.Letter);
patternBuilder.Append(r.Letter);

StringBuilder replacementBuilder = new StringBuilder();
replacementBuilder.Append(lower.Letter);
replacementBuilder.Append(higher.Letter);
patterns.Add(new ReplacementPattern(patternBuilder.ToString(), replacementBuilder.ToString()));
}
}
}

public static ReplacementPatternCatalog GetInstance()
{
if (instance == null)
instance = new ReplacementPatternCatalog();
return instance;
}

public ReplacementPattern[] GetAll()
{
return patterns.ToArray();
}

}
}
54 changes: 54 additions & 0 deletions eltoby/RomanNumerals/RomanNumerals.Data/RomanNumerals.Data.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{E0E74485-391E-417C-89DD-BC83FCB35168}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RomanNumerals.Data</RootNamespace>
<AssemblyName>RomanNumerals.Data</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ReplacementPatternCatalog.cs" />
<Compile Include="RomansCatalog.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RomanNumerals.Objects\RomanNumerals.Objects.csproj">
<Project>{1A6A329E-C543-4FFC-83EE-CEA7E9D55DBD}</Project>
<Name>RomanNumerals.Objects</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
67 changes: 67 additions & 0 deletions eltoby/RomanNumerals/RomanNumerals.Data/RomansCatalog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Text;
using RomanNumerals.Objects;
namespace RomanNumerals.Data
{
public class RomansCatalog
{
static RomansCatalog instance;
List<Roman> romans;
private RomansCatalog()
{
romans = new List<Roman>();
romans.Add(new Roman("I", 1, true));
romans.Add(new Roman("V", 5, false));
romans.Add(new Roman("X", 10, true));
romans.Add(new Roman("L", 50, false));
romans.Add(new Roman("C", 100, true));
romans.Add(new Roman("D", 500, false));
romans.Add(new Roman("M", 1000, true));
}

public static RomansCatalog GetInstance()
{
if (instance == null)
instance = new RomansCatalog();
return instance;
}

public Roman[] GetAll()
{
return romans.ToArray();
}

public Roman Higher(Roman number)
{
bool isNext = false;
foreach (Roman r in romans)
if (isNext)
return r;
else
if (r == number)
isNext = true;

return null;
}
public Roman Lower(Roman number)
{
Roman previous = null;
foreach (Roman r in romans)
if (r == number)
return previous;
else
previous = r;

return null;
}

public Roman GetOne(char letter)
{
foreach (Roman r in romans)
if (r.Letter == letter.ToString())
return r;
return null;
}
}
}
76 changes: 76 additions & 0 deletions eltoby/RomanNumerals/RomanNumerals.Logic/Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Text;
using RomanNumerals.Data;
using RomanNumerals.Objects;
namespace RomanNumerals.Logic
{
public class Converter
{

public string ConvertToRoman(int number)
{
char[] partes = number.ToString().ToCharArray();
Array.Reverse(partes);
int potencia = 0;
StringBuilder stbRoman = new StringBuilder();

foreach(char c in partes)
{
int part = int.Parse(c.ToString()) * (int)Math.Pow(10, potencia);
if (part > 0)
stbRoman.Insert(0, getRomanDigit(part));
potencia++;
}
return stbRoman.ToString();
}

string getRomanDigit(int number)
{

string preview = getRomanDigitPreview(number);
foreach (ReplacementPattern r in ReplacementPatternCatalog.GetInstance().GetAll())
preview = r.Replace(preview);

return preview;
}
string getRomanDigitPreview(int number)
{
RomansCatalog catalog = RomansCatalog.GetInstance();
Roman[] romans = catalog.GetAll();

Roman previous = null;
foreach (Roman n in romans)
if (number == n)
return n.Letter;
else
if (number > n)
previous = n;
else
break;

return previous.Letter + getRomanDigitPreview(number - previous.Value);



}

public int ConvertToNumber(string roman)
{
string start = "";
string result = roman;
while (start != result)
{
start = result;
foreach (ReplacementPattern r in ReplacementPatternCatalog.GetInstance().GetAll())
result = r.ReverseReplace(result);
}

int number = 0;
foreach (char c in result)
number += RomansCatalog.GetInstance().GetOne(c).Value;

return number;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RomanNumerals.Logic")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RomanNumerals.Logic")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2f73563c-5340-428e-9a7d-3716376e7626")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{A3FCDAB1-31B4-4A60-92D5-F8B63D51229B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RomanNumerals.Logic</RootNamespace>
<AssemblyName>RomanNumerals.Logic</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Converter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RomanNumerals.Data\RomanNumerals.Data.csproj">
<Project>{E0E74485-391E-417C-89DD-BC83FCB35168}</Project>
<Name>RomanNumerals.Data</Name>
</ProjectReference>
<ProjectReference Include="..\RomanNumerals.Objects\RomanNumerals.Objects.csproj">
<Project>{1A6A329E-C543-4FFC-83EE-CEA7E9D55DBD}</Project>
<Name>RomanNumerals.Objects</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading