C#反向范围ASCII算法方法 [英] C# A Reverse Range ASCII Algorithm Method

查看:87
本文介绍了C#反向范围ASCII算法方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间ConsoleApplication10
{
班级计划
{
公共静态无效的Main(string [] args)
{

Console.Write("Geben Sie bitte eine zuverschlüsselndeNummer ein:");
字符串newWords = Console.ReadLine();
字符串newWords2 = newWords.ToUpper();
Console.Write("Welchen AbstandmöchtenSie?");
int num1 = Convert.ToInt32(Console.ReadLine());
十进制选择= Convert.ToDecimal(num1);
Console.Write(WählenSieZählenvorwärts(1)oderrückwärtszählen(2)");
字符串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(arrayNewWords中的char Newchar)
{
十进制a =转换(Newchar,choice,opt);
int b = Convert.ToInt32(a);
char c = Convert.ToChar(b);
字符串结果= c.ToString();
//Console.Write(value);
Console.Write(result);
}
Console.ReadLine();
}


公共静态小数转换(char Newchar,十进制选择,字符串opt)
{
十进制NewcharDecimal = Convert.ToInt32(Newchar);
十进制TotalTotal = 0;
如果(opt =="1")
{
十进制TotalValuePlus = NewcharDecimal +选择;
而((TotalValuePlus> 90)||(TotalValuePlus< 64))
{
如果(TotalValuePlus> = 90)
{
TotalValuePlus-= 90;

}
否则(TotalValuePlus< = 64)
{
TotalValuePlus + = 64;
}
}
如果((TotalValuePlus< = 90)&&(TotalValuePlus> = 65))
{
TotalTotal = TotalValuePlus;
//返回TotalTotal;
}
} 
否则,如果(opt =="2")
{
十进制TotalValueMinus = NewcharDecimal-选择;
而((TotalValueMinus> 90)||(TotalValueMinus< 64))
{
如果(TotalValueMinus> = 90)
{
TotalValueMinus + = 90;

}
否则(TotalValueMinus< = 64)
{
TotalValueMinus-= 64;
}
}
如果((TotalValueMinus< = 90)&&(TotalValueMinus> = 65))
{
TotalTotal = TotalValueMinus;
//返回TotalTotal;
}
}
返回TotalTotal;
} 
}
} 

粗体字是需要更改的IF ELSE方法.

我需要使用变量调节器将算法方法从65 = A转换为90 = Z的算法方法,该变量告诉程序必须在ascii表Table中倒数.

例如,当我计算A CHAR = 65 DEC并插入数字-3 DEC时,在我的头上,答案应为ascii表Table中的X CHAR = 88 DEC.

有人知道如何解决这个问题或该算法是什么?

非常感谢所有帮助.谢谢


解决方案

我的大脑太好了!  我想您只是想让这些信件绕起来".在应用偏移量之后.

为此,您需要字母的索引mod 26.

//首先将字母转换为从0到25的索引.
int letterIndex = Newchar-(int)'A';
//然后添加偏移量(您已将其称为choice)
int newLetterIndex = letterIndex +选择;
//并取模,(一种方法是两次使用%并在两者之间加N使其为非负数)
int AdjustedIndex =(((newLetterIndex%26)+ 26)%26;
//然后将索引添加到"A"以获取字符.
char result ='A'+ AdjustedIndex; 

如果您现在只想要ASCII码,那就是(int)结果


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;
}
}
}

the BOLD words is the IF ELSE method needed to be Change.

I need help for a Algorithm method reversing from the range 65 =A to 90 = Z with a variable adjuster which tells that the program must count backwards in the ascii table Table. 

for example in my head when i calculate A CHAR = 65 DEC and i insert the number - 3 DEC the answer should be X CHAR= 88 DEC in the ascii table Table. 

Does someone know how to figure this out or what is the algorithm for that?

All help is much appreciated.. THANK YOU


解决方案

Ouch my brain!  I think you are just trying to get the letters to "wrap around" after applying an offset.

To do that, you need the letter's index mod 26.

// first convert the letter to an index from 0 to 25.
int letterIndex = Newchar - (int)'A';
// then add the offset (which you have called choice)
int newLetterIndex = letterIndex + choice;
// and take the modulus, (one way is to use % twice and add N in between to make it non-negative)
int adjustedIndex = ((newLetterIndex % 26) + 26) % 26;
// then add the index to 'A' to get the character.
char result = 'A' + adjustedIndex;

and if you just want the ASCII code at this point, that's (int)result


这篇关于C#反向范围ASCII算法方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆