我创建了一种确定两个字符串是否已排序的方法。我所有的测试用例都可以工作,除了一个。代码如下。
class Program
{
static void Main(string[] args)
{
bool test1 = isSorted("test1", "test2");
bool test2 = isSorted("4576", "4567");
bool test3 = isSorted("test10", "test11");
bool test4 = isSorted("abdc", "abcd");
}
public static bool isSorted(string MyFirstString, string MySecondString)
{
string MyFirstCutString = MyFirstString.ToLower();
string MySecondCutString = MySecondString.ToLower();
if (MyFirstString.Length > MySecondString.Length)
MyFirstCutString = MyFirstCutString.Substring(0, MySecondString.Length);
else if (MySecondString.Length > MyFirstString.Length)
MySecondCutString = MySecondCutString.Substring(0, MyFirstCutString.Length);
for (int i = 0; i < MyFirstCutString.Length; i++)
{
if (getNumberic(MyFirstCutString[i]) > getNumberic(MySecondCutString[i]))
return false;
}
return true;
}
public static int getNumberic(char myLetter)
{
switch (myLetter)
{
case 'a':
return 1;
case 'b':
return 2;
case 'c':
return 3;
case 'd':
return 4;
case 'e':
return 5;
case 'f':
return 6;
case 'g':
return 7;
case 'h':
return 8;
case 'i':
return 9;
case 'j':
return 10;
case 'k':
return 11;
case 'l':
return 12;
case 'm':
return 13;
case 'n':
return 14;
case 'o':
return 15;
case 'p':
return 16;
case 'q':
return 17;
case 'r':
return 18;
case 's':
return 19;
case 't':
return 20;
case 'u':
return 21;
case 'v':
return 22;
case 'w':
return 23;
case 'x':
return 24;
case 'y':
return 25;
case 'z':
return 26;
case 'O':
return 27;
case '1':
return 28;
case '2':
return 29;
case '3':
return 30;
case '4':
return 31;
case '5':
return 32;
case '6':
return 33;
case '7':
return 34;
case '8':
return 35;
case '9':
return 36;
default:
return 1000;
}
}
}
测试结果如下:
test1 result is true
test2 result is false
test3 result is false (NOT GOOD!)
test4 result is true
我看不出我的逻辑有任何缺陷。