码迷,mamicode.com
首页 > 编程语言 > 详细

C根据排序字符串

时间:2017-11-19 14:53:28      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:tput   for   printf   dex   bsp   put   字符串数组   print   def   

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define STR_LEN_MAX 100

char **ppcStringArry = NULL;
int num = 0;

void OutPutTest1();
void OutPutTest2();
/* 字符串数组输出函数*/
void OutPut()
{
    int i = 0 ;
    for (; i < num; i++)
    {
        if (NULL != ppcStringArry[i])
        {
            //free(ppcStringArry[i]);
        }
        printf("index is %d, string is %s.\r\n", i, ppcStringArry[i]);
    }
    printf("\r\n");
    printf("%s\r\n", "----------------------------------");
}
/* 字符串数组排序输出函数*/
void SortOutPut()
{
    int m = 0;
    int n = 0;
    char * temp = NULL;
    for (m = 0; m < num -1; m++)
    {
        for (n = m + 1; n < num; n++)
        {
            if (strcmp(ppcStringArry[m], ppcStringArry[n]) > 0)
            {
                temp = ppcStringArry[m];
                ppcStringArry[m] = ppcStringArry[n];
                ppcStringArry[n] = temp;
            }
        }
    }
    OutPut();

}

int main()
{
    //OutPutTest1();
    OutPutTest2();
}
/* 使用fgets函数*/
void OutPutTest1()
{
    char acDate[STR_LEN_MAX] = {0};
    FILE * pFile;
    pFile = fopen("F://C/1.txt", "ab+");
    num = 0;
    ppcStringArry = (char**)malloc(STR_LEN_MAX * sizeof(char*));
    while(NULL != fgets(acDate, STR_LEN_MAX, pFile))
    {
        ppcStringArry[num] = (char*)malloc(STR_LEN_MAX);
        memset(ppcStringArry[num], 0, STR_LEN_MAX);

        memcpy(ppcStringArry[num], acDate, strlen(acDate)+1);
        memset(acDate, 0, sizeof(acDate));
        num++;

    }
    SortOutPut();
}
/* 使用fscanf函数*/
void OutPutTest2()
{
    //char acDate[STR_LEN_MAX] = {0};
    FILE * pFile;
    pFile = fopen("F://C/1.txt", "ab+");
    int ret = 0;
    num = 0;
    ppcStringArry = (char**)malloc(STR_LEN_MAX * sizeof(char*));
    ppcStringArry[num] = (char*)malloc(STR_LEN_MAX);
    memset(ppcStringArry[num], 0, STR_LEN_MAX);
    ret = fscanf(pFile, "%s", ppcStringArry[num]);

    while( EOF != ret)
    {
        num++;
        ppcStringArry[num] = (char*)malloc(STR_LEN_MAX);
        memset(ppcStringArry[num], 0, STR_LEN_MAX);
        ret = fscanf(pFile, "%s", ppcStringArry[num]);
    }
    SortOutPut();
}

 

C根据排序字符串

标签:tput   for   printf   dex   bsp   put   字符串数组   print   def   

原文地址:http://www.cnblogs.com/xiaodeyao/p/7859649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!