【问题标题】:C# - Difficulty in Trying to Find the Error(s) in my String Bubble-Sort AlgorithmC# - 难以在我的字符串冒泡排序算法中查找错误
【发布时间】:2019-05-29 03:42:02
【问题描述】:

我最近一直在尝试编写一种算法,它可以接受来自用户的任意数量的字符串,将它们合并为一个字符串,然后利用冒泡排序的概念按字母升序对它们进行排序。但是我最近在试图确定我的错误所在时遇到了麻烦,因为当我的代码编译和运行时没有弹出任何错误时,最终结果很奇怪。它只是显示输入的第一个字符串未排序,其余字符串显示为空白。如果有人能帮助我解决这个问题,我将不胜感激。

{
           int number_of_strings = 0;
           string[] array_of_strings;
           char[] array_of_char;
           char[] temporary_array;
           char[] temporary_character = new char[1];
           string[] suffix = new string[1];
           int length = 0;
           int counter = 0;
           int position = 0;
           int swap = 0;
           string[] temporary_hold = new string[1];

           Console.WriteLine("Please enter the number of strings you would like to enter: ");
           Console.Write("> ");
           number_of_strings = Int32.Parse(Console.ReadLine());

           array_of_strings = new string[number_of_strings];

           for(counter = 0; counter < number_of_strings; counter++)
           {
               suffix = AddSuffix((counter + 1));

               Console.WriteLine("Please enter the {0}{1} string:", (counter + 1), suffix[0]);
               Console.Write("> ");
               temporary_hold[0] = Console.ReadLine();

               array_of_strings[counter] = temporary_hold[0];

               length = length + array_of_strings[counter].Length; 
           }

           array_of_char = new char[length]; 

           for(counter = 0; counter < number_of_strings; counter++)
           {
               temporary_array = array_of_strings[counter].ToCharArray();

               for( ;position < temporary_array.Length; position++)
               {
                   array_of_char[position] = temporary_array[position]; 
               }

               position = position + temporary_array.Length;
           }

           counter = 0;

           do
           {
               if(array_of_char[counter] > array_of_char[counter + 1])
               {
                   swap = 1;
                   temporary_character[0] = array_of_char[counter + 1];
                   array_of_char[counter + 1] = array_of_char[counter];
                   array_of_char[counter] = temporary_character[0];
               }
               else
               {
                   swap = 0;
               }   

                counter++;
           } while(swap != 0);

           Console.WriteLine("The resultant string is as follows:");

           for(counter = 0; counter < array_of_char.Length; counter++)
           {
               Console.Write("{0} ", array_of_char[counter]);
           };

           Console.ReadKey();
       }
   }

   static string[] AddSuffix(int number)
       {
           int greatness = 0;

           if(number > 10)
           {
               greatness = 1;
           };

           string input = string.Empty;
           string[] suffix = new string[1];
           input = number.ToString();

            if(input[0] == '1')
            {
                suffix[0] = "st";
            }
            else if(input[0] == '2')
            {
                suffix[0] = "nd";
            }
            else if(input[0] == '3')
            {
                suffix[0] = "rd";
            }
            else
            {
                suffix[0] = "st";
            }   

            if(greatness == 1)
            {
                if(input[1] == '1')
                {
                    suffix[0] = "st";
                }
                else if(input[1] == '2')
                {
                    suffix[0] = "nd";
                }
                else if(input[1] == '3')
                {
                    suffix[0] = "rd";
                }
                else
                {
                    suffix[0] = "st";
                }   
            };

                return suffix;
       }

我使用的是标准库,Main方法的签名是:static void Main(string[] args)

【问题讨论】:

  • 您不能合并字符串然后期望对它们进行排序。合并字符串是一个对象,您可以对一项进行排序。
  • 我不明白你的评论。我的主要目标是将用户输入的所有字符串写入一个字符串,然后对该字符串的所有元素进行排序,该字符串包含之前字符串中的所有元素。
  • 那你需要将合并后的字符串解析或拆分成一个数组。

标签: c# string bubble-sort


【解决方案1】:

我认为这样的事情对你有用,(使用 linq)

void Main()
{
    Console.WriteLine("Please enter the number of strings you would like to enter: ");
    Console.Write("> ");
    var number_of_strings = Int32.Parse(Console.ReadLine());
    var strings = new string[number_of_strings];
    for (int i = 0; i < number_of_strings; i++)
    {
        Console.WriteLine($"Please enter the {AddOrdinal(i+1)} string");
        strings[i] = Console.ReadLine();
    }
    var str = string.Join("", strings);
    var strSorted = string.Concat(str.OrderBy(x=>x));
    Console.WriteLine(strSorted);
}

public static string AddOrdinal(int num)
{
    if (num <= 0) return num.ToString();

    switch (num % 100)
    {
        case 11:
        case 12:
        case 13:
            return num + "th";
    }

    switch (num % 10)
    {
        case 1:
            return num + "st";
        case 2:
            return num + "nd";
        case 3:
            return num + "rd";
        default:
            return num + "th";
    }

}

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2018-05-04
    • 2018-08-11
    • 2013-07-13
    • 2016-12-02
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多