C#一种反向范围ASCII算法方法



代码if (opt == 1)是十进制TotalValuePlus = NewcharDecimal + choice的正向计数,我正在为else if (opt == 2)寻找反向计数的方法。我似乎在ASCII表中找不到计数混响方法的方法。例如,在我的脑海中,当ASCII表中的A(char)=65(dec),用户输入3(dec)并选择选项2(这是减法)时,答案应该是X(char)=88(dec。

问题是

如何在数学算法方法中做到这一点这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
class Program
{
public static void Main(string[] args)
{

Console.Write("Geben Sie bitte eine zu verschlüsselnde Nummer ein: ");
string newWords = Console.ReadLine();
string newWords2 = newWords.ToUpper();
Console.Write("Welchen Abstand möchten Sie? ");
int num1 = Convert.ToInt32(Console.ReadLine());
decimal choice = Convert.ToDecimal(num1);
Console.Write("Wählen Sie Zählen vorwärts (1) oder rückwärts zählen (2) ");
string opt = Console.ReadLine();
newWords2 = newWords2.Replace("Ö", "OE");
newWords2 = newWords2.Replace("Ä", "AE");
newWords2 = newWords2.Replace("Ü", "UE");
newWords2 = newWords2.Replace("ß", "SS");
newWords2 = newWords2.Replace(" ", " ");

Char[] arrayNewWords = newWords2.ToCharArray();

foreach (char Newchar in arrayNewWords)
{
decimal a = Convertion(Newchar, choice, opt);
int b = Convert.ToInt32(a);
char c = Convert.ToChar(b);
string result = c.ToString();
// Console.Write(value);
Console.Write(result);
}
Console.ReadLine();
}

public static decimal Convertion(char Newchar, decimal choice, string opt)
{
decimal NewcharDecimal = Convert.ToInt32(Newchar);

decimal TotalTotal = 0;


if (opt == "1")
{
decimal TotalValuePlus = NewcharDecimal + choice;
while ((TotalValuePlus > 90) || (TotalValuePlus < 64))
{
if (TotalValuePlus >= 90)
{
TotalValuePlus -= 90;

}
else if (TotalValuePlus <= 64)
{
TotalValuePlus += 64;
}
}
if ((TotalValuePlus <= 90) && (TotalValuePlus >= 65))
{
TotalTotal = TotalValuePlus;
// return TotalTotal;
}

} 

else if (opt == "2")
{
decimal TotalValueMinus = NewcharDecimal - choice;
while ((TotalValueMinus > 90) || (TotalValueMinus < 64))
{
if (TotalValueMinus >= 90)
{
TotalValueMinus += 90;

}
else if (TotalValueMinus <= 64)
{
TotalValueMinus -= 64;
}
}
if ((TotalValueMinus <= 90) && (TotalValueMinus >= 65))
{
TotalTotal = TotalValueMinus;
// return TotalTotal;
}
}
return TotalTotal;
}
}
}

您的代码非常复杂。学习并理解以下内容:

public static char Offset(char c, int offset, char lowerBound, char upperBound)
{
var mod = (c + offset - lowerBound) % (upperBound - lowerBound + 1);
var newChar = (char)(mod >= 0 ? lowerBound + mod: upperBound + mod + 1);
Debug.Assert(lowerBound <= newChar && newChar <= upperBound);
return newChar;
}

需要考虑的事项:

  1. ASCII编码基于自然数,为什么要转换为decimal?此外,char可以隐式转换为int,您不需要显式转换任何内容
  2. 了解%运算符,以及模数在这种情况下如何有用。提示:删除代码中丑陋的while循环
  3. 为什么你要用一个不奇怪的论点来决定偏移量是正的还是负的?只需使用偏移本身的符号
  4. 在任何可能的情况下实现廉价的泛化(如果它不便宜,那么除非绝对必要,否则不要这样做);为什么要硬编码区间的下限和上限?将这种方法推广到任何有效区间都很容易
  5. 实现参数验证。如果该方法是公共的,则在提供的参数无效的情况下引发有意义的异常。如果该方法是内部或私有的,请考虑使用System.Diagnostics.Debug.Assert
  6. 断言方法发布条件。确保您的不变量已被保存,并且该方法按预期工作

UPDATE:通过以下扩展方法给出了该问题的更通用的解决方案:

public static T Offset<T>(this IList<T> source, int start, int offset)
{
var mod = (start + offset) % source.Count;
return source[mod >= 0 ? mod : source.Count + mod + 1];
}

阅读您的评论,这个版本可能更有用:

var str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var thirteenth = str.ToCharArray().Offset(0, 13);

相关内容

最新更新