对两个字符串的所有整数值应用数学函数



给出了两个字符串。我想对字符串中的每个数字应用一个数学函数并将其保存在新字符串中。规则:

"19834675"; // keypc
"28374651";// keyserial
(1)sb[0]=max(keypc[0],keyserial[0])  the first element of final string
max(1,2)=2   1 is first of"19834675" , 2 is first of "28374651"
(2)sb[1]=Math.Floor((keypc[1]+keyserial[1])/3) the second element of final string  // 
(3)sb[2]= (keypc[1]+keyserial[1]) The remainder is divided by 10 
8+3=11---->1 , 8 is the third char of "19834675" and 3  the third char of "28374651"

我的尝试(1(:

我将以下代码用于 (1(

string keypc    ="19834675";
string keyserial="28374651";
StringBuilder sb = new StringBuilder(keypc,keypc.Length);
if (sbkeypc[0] < sbkeyserial[0])
{
sb[0] = sbkeyserial[0];
}else
{
sb[0] = sbkeypc[0];
}

我的尝试(2(:问题是为什么以下代码不起作用?! 以及如何做(3(?

double inttemp=Math.Floor((Char.GetNumericValue(keypc[1])+Char.GetNumericValue(keyserial[1]))/3);
sb[1]= Convert.ToChar(Convert.ToInt32(inttemp));
Console.WriteLine(Convert.ToChar(Convert.ToInt32(inttemp))); // error here --->return ""
Console.WriteLine(sb.ToString()); // error here: the length of sb reduced

我认为这就是你想要的:

你可以在这里看到它的实际应用

using System;
using System.Text;
public class Program
{
public static void Main()
{
string keypc = "19834675";
string keyserial = "28374651";
StringBuilder sb = new StringBuilder(keypc, keypc.Length);
sb[0] = MinChar(keypc[0], keyserial[0]);
sb[1] = FloorChar(keypc[1], keyserial[1]);
for (int i=2; i<keypc.Length; i++)
{
sb[i] = RemainderChar(keypc[i], keyserial[i]);
}
Console.WriteLine(sb.ToString());
}
public static char MinChar(char c1, char c2)
{
double val1 = Char.GetNumericValue(c1);
double val2 = Char.GetNumericValue(c2);
double val3 = Math.Min(val1, val2);
string str = val3.ToString();
return str[0];
}
public static char FloorChar(char c1, char c2)
{
double val1 = Char.GetNumericValue(c1);
double val2 = Char.GetNumericValue(c2);
double val3 = Math.Floor((val1+val2)/3);
string str = val3.ToString();
return str[0];
}
public static char RemainderChar(char c1, char c2)
{
double val1 = Char.GetNumericValue(c1);
double val2 = Char.GetNumericValue(c2);
Console.WriteLine("{0} {1}", val1, val2);
double val3 = (val1+val2) % 10;
Console.WriteLine("" + val3);
string str = val3.ToString();
return str[0];
}
}

最新更新