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

字符串排序问题 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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)