A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Roman Numeral in Beef page! Here, you'll find the source code for this program as well as a description of how the program works.
using System;
namespace RomanNumeral;
class Program
{
public static void Usage()
{
Console.WriteLine("Usage: please provide a string of roman numerals");
Environment.Exit(0);
}
public static void Error()
{
Console.WriteLine("Error: invalid string of roman numerals");
Environment.Exit(0);
}
public static Result<int> RomanNumeral(StringView str)
{
int total = 0;
int prevDigit = 0;
for (char8 ch in str)
{
int digit = 0;
switch (ch)
{
case 'I': digit = 1;
case 'V': digit = 5;
case 'X': digit = 10;
case 'L': digit = 50;
case 'C': digit = 100;
case 'D': digit = 500;
case 'M': digit = 1000;
default: return .Err;
}
total += digit;
// If there is a previous digit and digit is greater than previous digit,
// subtract two times previous digit from total to compensate for addition of
// previous digit
if (prevDigit > 0 && digit > prevDigit)
{
total -= 2 * prevDigit;
prevDigit = 0;
}
prevDigit = digit;
}
return .Ok(total);
}
public static int Main(String[] args)
{
if (args.Count < 1)
{
Usage();
}
switch (RomanNumeral(args[0]))
{
case .Ok(let result):
Console.WriteLine(result);
case .Err:
Error();
}
return 0;
}
}
Roman Numeral in Beef was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.