簡體   English   中英

在C中輸入,拆分和排序

[英]Inputting, splitting, and sorting in C

我是python程序員。 我的女朋友正在上C班。 這讓我感到沮喪,這是如此簡單,我無法在網上找到也無法弄清楚。 讓我們開始追逐。 我有一個簡單的Python程序,在嘗試轉換為C時需要幫助。

lst = input("Enter a list of numbers with a space in between each number\n")

newList = lst.split(" ")

#selection sort has been pre defined
x = newList.selectSort()
print(x)

抱歉,這是在我的手機上完成的。

她的任務不只是這個。 它添加了多個可以協同工作的功能。 我只需要知道這是如何工作的即可將整個程序組合在一起。

首先,您必須定義列表中number of item然后才能input它們。 然后,您必須將它們存儲在array並手動執行sorting過程。

我沒有定義function就完成了排序過程。 如果要使用函數,只需傳遞數組並返回排序后的數組即可。

#include <stdio.h>

int main()
{
   int n, c, d, position, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);
   int array[n];
   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);

   return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

// Macro for sorting
#define sort(name, data_set, len, comparator, inverse) \
    name##_sort(data_set, len, comparator, inverse)

#define SORT_DEFINE(name, data_type) \
\
    /* Sort data set
           @data_set   data set to sort
           @len        length of data set
           @comparator comparator to compare two elements, return positive value when first element is bigger
           @inverse    whether the result should be inversed
         */\
    void name##_sort(data_type *data_set, int len, int (*comparator)(data_type, data_type), bool inverse) \
    { \
        int i; \
        int j; \
        bool change = true; \
        int ret; \
        data_type tmp; \
\
        for (i = 0; change && i < len - 1; i++) \
        { \
            change  = false; \
            for (j = 0; j < len - 1 - i; j++) \
            { \
                ret = comparator(data_set[j], data_set[j + 1]); \
                if ((!inverse && ret > 0) || (inverse && ret < 0)) \
                { \
                    change = true; \
                    tmp = data_set[j]; \
                    data_set[j] = data_set[j + 1]; \
                    data_set[j + 1] = tmp; \
                } \
            } \
        } \
    }

/*  Split string
    @content origin string content
    @delim   delimiter for splitting
    @psize   pointer pointing at the variable to store token size
    @return  tokens after splitting
 */
const char **split(char *content, const char *delim, int *psize)
{
    char *token;
    const char **tokens;
    int capacity;
    int size = 0;

    token = strtok(content, delim);
    if (!token)
    {
        return NULL;
    }

    // Initialize tokens
    tokens = malloc(sizeof(char *) * 64);
    if (!tokens)
    {
        exit(-1);
    }
    capacity = 64;

    tokens[size++] = token;

    while ((token = strtok(NULL, delim)))
    {
        if (size >= capacity)
        {
            tokens = realloc(tokens, sizeof(char *) * capacity * 2);
            if (!tokens)
            {
                exit(-1);
            }
            capacity *= 2;
        }
        tokens[size++] = token;
    }

    *psize = size;

    return tokens;
}

// Define sort function for data_type = const char *
SORT_DEFINE(str, const char *);

// Define sort function for data_type = int
SORT_DEFINE(int, int)

int intcmp(int v1, int v2)
{
    return v1 - v2;
}

int main(int argc, char *argv[])
{
    char buff[128];
    const char **tokens;
    int size;
    int i;
    int *ints;

    // Get input from stdin
    fgets(buff, 128, stdin);

    // Split string
    tokens = split(buff, " \t\n", &size);

    ints = malloc(sizeof(int) * size);

    // Sort strings [min -> max]
    sort(str, tokens, size, strcmp, false);

    // Print strings and transfer them to integers
    for (i = 0; i < size; i++)
    {
        printf("[%02d]: <%s>\n", i, tokens[i]);
        ints[i] = atoi(tokens[i]);
    }   

    // Sort integers [max -> min]
    sort(int, ints, size, intcmp, true);

    // Print integers
    for (i = 0; i < size; i++)
    {
        printf("[%02d]: <%d>\n", i, ints[i]);
    }

    free(ints);

    free(tokens);

    return 0;
}

使用宏SORT_DEFINE()sort()和函數split()完成您自己的工作。 main()函數只是一個演示如何使用它們的演示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM