我在WinForms中处理罗马数字。
我需要将它们转换成阿拉伯语。
示例:XCIV为94。 我是c#的初学者。 我使用双向链表来处理任务。 我现在没有在我的教育中学到更高的东西。
如何从循环中提取:
for (int i = 0; i < Check.Length; i++)
{
l.Add(Check[i], source);
}
包含整个列表的Listelement并保存到var。
以下是代码:
Form1.cs的
private void runBtn_Click(object sender, EventArgs e)
{
string source = inputBox.Text;
RomanNumbers rn = new RomanNumbers();
if(rn.CheckRomanNumber(source))
{
OutputBox.Text = RomanNumbers.Calculate();
}
else
{
OutputBox.Text = "Wrong format!";
}
}
RomanNumbers.cs
using System;
namespace RomanNumbersToArabic
{
public class RomanNumbers
{
public static int ArabicValue { get; private set; }
public static bool TransformResult { get; private set; }
static RomanNumbers()
{
}
public bool CheckRomanNumber(string source)
{
bool result = false;
List l = new List();
CheckExept[] Check = l.CreateList(source);
if (Check == null)
{
return false;
}
for (int i = 0; i < Check.Length; i++)
{
l.Add(Check[i], source);
}
return result;
}
public static string Calculate()
{
string result = "";
// code //
return result;
}
public static bool ValidateExpression()
{
bool result = false;
string[] exceptionsWithValuesArray = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "DM", "M" };
// code //
return result;
}
}
}
List.cs
using System;
namespace RomanNumbersToArabic
{
class List
{
private ListElement head;
private ListElement tail;
public List()
{
}
public int Count { get; private set; }
public CheckExept[] CreateList(string source)
{
source = StringFormat(source);
CheckExept[] Check = new CheckExept[source.Length];
int checkCounter = 0;
for (int i = 0; i < source.Length; i++)
{
Check[checkCounter] = new CheckExept();
Check[checkCounter] = Validate(i, source);
if (Check[checkCounter] == null)
{
return null;
}
if (Check[checkCounter].IsException)
{
i++;
}
checkCounter++;
}
Array.Resize(ref Check, checkCounter);
return Check;
}
private CheckExept Validate(int index, string stringValue)
{
Number[] listOfNumbers = Number.CreateNumbersList();
Exeption[] ListOfException = Exeption.CreateExeptionsList();
CheckExept Check = new CheckExept();
Check.IsLetter = false;
Check.IsException = true;
bool isLastElement = false;
if (CheckWrongItems(listOfNumbers, stringValue))
{
return null;
}
if (index == stringValue.Length - 1)
{
isLastElement = true;
Check.IsLetter = true;
Check.IsException = false;
}
else
{
stringValue = stringValue.Substring(index, 2);
}
if(!isLastElement)
for (int i = 0; i < ListOfException.Length; i++)
{
if (stringValue == ListOfException[i].Letter)
{
Check.IsLetter = false;
Check = new CheckExept(stringValue, ListOfException[i].Value, Check.IsLetter, Check.IsException);
break;
}
else
{
Check.IsLetter = true;
}
}
if (Check.IsLetter)
{
Check.IsException = false;
Check = GetLetter(listOfNumbers, Check, stringValue.Substring(index, 1));
}
return Check;
}
private CheckExept GetLetter(Number[] listOfNumbers, CheckExept Check, string stringValue)
{
for (int i = 0; i < listOfNumbers.Length; i++)
{
if (stringValue == listOfNumbers[i].Letter)
{
Check.IsLetter = true;
Check.StringValue = stringValue;
Check.IntValue = listOfNumbers[i].Value;
return Check;
}
else
{
Check.IsLetter = false;
}
}
return null;
}
private bool CheckWrongItems(Number[] listOfNumbers, string stringValue)
{
bool isWrong = true;
for (int i = 0; i < stringValue.Length; i++)
{
isWrong = true;
for (int j = 0; j < listOfNumbers.Length; j++)
{
if (stringValue[i].ToString() == listOfNumbers[j].Letter)
{
isWrong = false;
break;
}
}
if (isWrong)
{
return isWrong;
}
}
return isWrong;
}
public void Add(CheckExept check, string source)
{
ListElement newElement = new ListElement();
newElement = new ListElement(check.StringValue, check.IntValue, check.IsLetter, check.IsException);
if (head == null)
{
head = newElement;
tail = newElement;
return;
}
tail.Next = newElement;
newElement.Prev = tail;
tail = newElement;
Count++;
}
private string StringFormat(string value)
{
value = value.Trim().ToUpper();
value = value.Replace(" ", "");
return value;
}
}
}