Felix2048 2016-11-29 17:22 采纳率: 0%
浏览 1355

字符串排序问题 C语言

新手一枚,然后Debug了一晚上跪了...然后来求助了,谢谢各位

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

#define BUF_LEN 100 // Length of input buffer
#define INI_COUNT 5 // Initial count number of strings

void swap(char *a, char *b);
void recursion(char **pstr, int start, int end);
void quick_sort(char **pstr, int len);

int main(void)
{
    size_t str_len = 0, // Current string length
        str_count = 0, // Current count number of strings
        capacity = INI_COUNT; // Current capacity of strings
    char buf[BUF_LEN]; // Input Buffer
    char* ptr = NULL; // Pointer to check whether fgets works or not
    char** pString = (char**)calloc(capacity, sizeof(char*));   // Pointer to strings

    //Instruction
    printf("Enter strings to be sorted, one per line.\nPress Enter to end input.\n\n");

    // Read strings
    while(true)
    {
        ptr = fgets(buf, BUF_LEN, stdin);    // Read the string
        if(!ptr)     // Read error check
        {
            printf("Error reading string!\n");
            free(pString); // Free memory 
            pString = NULL;
            return 1;
        }

        if(*ptr == '\n')     // End read loop check by entering Enter
            break;

        if(str_count == capacity)    // Capacity check
        {
            capacity++; // Increase capacity by one
            pString = (char**)realloc(pString, capacity); // Reallocate memory
            if(!pString) // Reallocate memory error check
            {
                printf("Error reallocating memory!\n");
                free(pString); // Free memory 
                pString = NULL;
                return 2;
            }
        }

        str_len = strnlen_s(buf, BUF_LEN) + 1; 
        // Current string length of pstr and add one for '/0'
        pString[str_count] = (char*)malloc(str_len); 
        // Allocate memory for each string
        if(!pString[str_count])  // Read error check
        {
            printf("Error allocating memory!\n");
            size_t i = 0;
            for(;i < str_count; i++)     // Free memory
            {
                free(pString[i]);
                pString[i] = NULL;
            }
            free(pString);
            pString = NULL;
            return 3;
        }           

        strcpy_s(pString[str_count], str_len, buf); // Copy string from buffer
        str_count++;
    }

    // Sort strings
    int len = (int)str_count;
    quick_sort(pString, len);

    // Print strings & free memory
    size_t i = 0;
    for(; i < str_count; i++)
    {
        printf("%s\n", pString[i]);
        free(pString[i]);
        pString[i] = NULL;
    }
    free(pString);
    pString = NULL;
    return 0;
}

void swap(char *a, char *b)
{
    char *temp = a; 
    a = b;
    b = temp;
}

void recursion(char **pstr, int start, int end)
{
    if(start >= end)
        return;
    int left = start,
        right = end - 1,
        cmp = 0;

    while(left < right)
    {
        cmp = strcmp(pstr[left], pstr[end]);
        while(cmp < 0 && left < right)
        {
            left++;
            cmp = strcmp(pstr[left], pstr[end]);
        }

        cmp = strcmp(pstr[right], pstr[end]);
        while(cmp >= 0 && left < right)
        {
            right--;
            cmp = strcmp(pstr[right], pstr[end]);
        }

        swap(pstr[left], pstr[right]);
    }   

    cmp = strcmp(pstr[left], pstr[end]);
    if(cmp >= 0)
        swap(pstr[left], pstr[end]);
    else
        left++; 

    if (left) 
    {
        recursion(pstr, start, left - 1);
        recursion(pstr, left + 1, end);
    }    
    else
        recursion(pstr, left + 1, end);
}

void quick_sort(char **pstr, int len)
{
    recursion(pstr, 0, len - 1);
}
  • 写回答

1条回答 默认 最新

  • 大红番茄 2016-11-30 01:28
    关注

    拿走,不谢!
    #include "stdafx.h"
    #include
    #include
    #include
    //#include

    #define BUF_LEN 100 // Length of input buffer
    #define INI_COUNT 100 // Initial count number of strings

    void swap(char *a, char *b);
    void recursion(char **pstr, int start, int end);
    void quick_sort(char **pstr, int len);

    int _tmain(int argc, _TCHAR* argv[])
    {
    size_t str_len, // Current string length
    str_count, // Current count number of strings
    capacity; // Current capacity of strings
    char buf[BUF_LEN]; // Input Buffer
    char* ptr = NULL; // Pointer to check whether fgets works or not
    char** pString = NULL; // Pointer to strings

    //Instruction
    printf("Enter strings to be sorted, one per line.\nPress Enter to end input.\n\n");
    while(true)
    {
        str_len = 0;
        str_count = 0;
        capacity = INI_COUNT;
        pString = (char**)calloc(capacity, sizeof(char*)); 
        memset(buf, 0, sizeof(buf));
    
        while(true)
        {
            ptr = fgets(buf, BUF_LEN, stdin);                                                   // Read the string
            if(!ptr)                                                                            // Read error check
            {
                printf("Error reading string!\n");
                free(pString);                                                                  // Free memory 
                pString = NULL;
                return 1;
            }
    
            if(*ptr == '\n')                                                                    // End read loop check by entering Enter
            {
                printf("begin sort!\n");
                break;
            }
    
            if(str_count == capacity)                                                           // Capacity check
            {
                capacity++;                                                                     // Increase capacity by one
                pString = (char**)realloc(pString, capacity);                                   // Reallocate memory
                if(!pString)                                                                    // Reallocate memory error check
                {
                    printf("Error reallocating memory!\n");
                    free(pString);                                                              // Free memory 
                    pString = NULL;
                    return 2;
                }
            }
    
            str_len = strnlen_s(buf, BUF_LEN)+1;                                              // Current string length of pstr and add one for '/0'
            pString[str_count] = (char*)malloc(str_len);                                        // Allocate memory for each string
            if(!pString[str_count])                                                             // Read error check
            {
                printf("Error allocating memory!\n");
                size_t i = 0;
                for(;i < str_count; i++)                                                        // Free memory
                {
                    free(pString[i]);
                    pString[i] = NULL;
                }
                free(pString);
                pString = NULL;
                return 3;
            }           
    
            strcpy_s(pString[str_count], str_len, buf);                                         // Copy string from buffer
            str_count++;
        }   
    
    
        int len = (int)str_count;
        quick_sort(pString, len);
    
    
        // Print strings & free memory
        size_t i = 0;
        for(; i < str_count; i++)
        {
            printf("%s\n", pString[i]);
            free(pString[i]);
            pString[i] = NULL;
        }
    
        free(pString);
        pString = NULL;
    }
    return 0;
    

    }

    void swap(char *a, char *b)
    {
    int len_a = strlen(a);
    int len_b = strlen(b);
    char buf[BUF_LEN];
    memset(buf, 0, BUF_LEN);
    memcpy(buf, a, len_a);
    memset(a, 0, len_a);
    memcpy(a, b, len_b);
    memset(b, 0, len_b);
    memcpy(b, buf, len_a);
    }

    void quick_sort(char **pstr, int len)
    {
    //recursion(pstr, 0, len - 1);
    int i = 0;
    int j = 0;
    int cmp = 0;
    for (i = 0; i < len; i++)
    for (j = i + 1; j < len; j++)
    {
    cmp = 0;
    cmp = strcmp(pstr[i], pstr[j]);
    if (cmp > 0)
    {
    swap(pstr[i], pstr[j]);
    }

        }
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。