【问题标题】:Sorting the last column in array of strings in C在C中对字符串数组中的最后一列进行排序
【发布时间】:2017-07-08 02:11:19
【问题描述】:

我有一个 .txt 文件,其中包含以下内容:

1    - Ground   2
2    - Ground   7
3    - City     1
4    - Hill     x
5    - City     3
6    - City     4
7    - Hill     6

任务是按特定顺序对这些游戏槽进行排序:文件的第一列是槽号,第二文件的列是插槽类型,文件的第三列是相关插槽右侧的插槽编号。

x 表示该插槽右侧没有插槽,这意味着插槽 4(Hill)是有序插槽中的最后一个插槽。所以程序必须搜索文本文件以找到x,然后查看该行上的插槽号,以查看左侧的插槽。完成此操作后,应输出以下内容:

(5,City)->(3, City)->(1, Ground)->(2,Ground)->(7,Hill)->(6,City)->(4,Hill)

这是我目前的代码:

此代码读取文本文件并打印初始插槽列表。任何帮助将不胜感激。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char *SLOTS_FILE_PATH = "slots.txt";

char slotsArr[7][100];
char newSlots[7][100];

int main()
{
     int i = 0;
     int j;
     char search;

     FILE *fx = fopen(SLOTS_FILE_PATH, "r+");   

         if (fx == NULL)
         {
            perror("Error opening slots file");
            i = -1;
         }
         else
         {
             while(fgets(slotsArr[i],
             sizeof(slotsArr[i]), fx) != NULL)
         {
             i++;
         }

             fclose (fx);
         }

     printf("\n-------Initial List of Slots-------\n");

     for (j = 0; j < i; j++)
     {
         printf("%s", slotsArr[j]);
     }

     printf("\n-------Sorted List of Slots-------\n");

     }

*********************这就是它输出的内容************************ *****

 -------Initial List of Slots-------
 1  - Ground    2
 2  - Ground    7
 3  - City      1
 4  - Hill      x
 5  - City      3
 6  - City      4
 7  - Hill      6

 -------Sorted List of Slots-------

【问题讨论】:

  • 您需要一个结构体,将数据正确读入结构体,qsort() 和相应的比较函数。
  • 您是否意识到您的数据实际上是一个(一组)链表? (或 D(A)G,或一棵树……)
  • 你了解struct类型了吗?几乎可以肯定,你应该在这个练习中使用一个。
  • 不,我们还没有介绍结构,我们的想法是在数组中搜索 x,然后向后比较数组并交换元素,直到回到开始。
  • 好的。即使没有结构,您也需要取得一些进展。例如,您需要两个并行数组,一个用于名称,一个用于后继编号。您将通过适当的读取操作填充它们(我可能会使用fgets() 来读取行,并使用sscanf() 来解析行)。然后你可以告诉你该做什么。请注意,您需要允许 x 条目。

标签: c arrays sorting multidimensional-array structure


【解决方案1】:

在我的浏览器中输入,所以会有错别字。 (我一只手上有绷带……)然而,指针:

char slotsArr[7][100];
char newSlots[7][100];
…

// It starts with x
char charToSearch = 'x'; // It doesn't matter, whether it is a digit or char. 
int sortedIndex = 7; // Start at the right side.
while( sortedIndex>0 )
{
  // search for the row by iterating over the array and comparing the last letter.
  for( int unsortedIndex=6; unsortedIndex>=0; unsortedIndex-- )
  {
    if( charToSearch == slotsArr[unsortedIndex][16] )
    {
      charToSearch = slotsArr[unsortedIndex][0]; // The next item's index
      sortedIndex--;
      // The format of the result array is not clear in your Q. 
      // However, this is not the problem. I put a number and the trimmed 
      // string into it. You can add the parenthesis and the - at output, 
      // if you want to.
      newSlots[sortedIndex][0]=charToSearch;
      newSlots[sortedIndex][1]=',';

      // Copy the string up to a space
      char src = slotsArray[unsortedIndex]+7;
      char *dest = newSlots[sortedIndex]+2;
      while( !isspace(src) )
      {
        *dst++ = *src++;
      }
      *dst='\0';
      break;
    }
  }
}

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 2012-09-13
    • 1970-01-01
    • 2013-09-22
    • 2012-08-31
    • 2014-11-14
    • 2021-08-16
    • 2011-09-04
    • 2014-08-10
    相关资源
    最近更新 更多