标题:代码写得优美、易懂__第二版
只看楼主
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
结帖率:90.91%
已结贴  问题点数:20 回复次数:2 
代码写得优美、易懂__第二版
程序代码:
/***************************************************************************
*                                                                          *
* File    : strings.c                                                      *
*                                                                          *
* Purpose : Unlimit string's count and string's size input.                *
*                                                                          *
* Author  : Dennis          Date  :  00/00/0000                            *
*                                                                          *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _STDC_WANT_LIB_EXT1_ 1
#define buffer_len 10
#define buffer_len_inc 5
#define string_cont 5
#define string_cont_inc 2
typedef struct point point;
struct point
{
    char* point_start;
    char* point_end;
}buffer = {NULL, NULL};

//function prototype
char** memory_init_1(int size);
char* memory_init_2(int size);
point get_string(point buf, int length);
//char* add_buf_memory(char* start, char* end);
void store_strings(char** str, int cont,int cur_buf_len, point buf);
char** add_str_memory(char** str_st ,int count, int str_count);
void output_strings(char** str_st, int count);

int main(void)
{
//variable declare    
    int buf_len = buffer_len;
    int current_buf_len = 0;

    char** str_dre_dre = NULL;
    char** str_start = NULL;
    int count = 0;
    int str_cont = string_cont;
//初始化分配内存
    str_start = str_dre_dre = memory_init_1(str_cont * sizeof(char*));
    buffer.point_start = buffer.point_end =  memory_init_2(buf_len * sizeof(char));
//
    printf("input something you want : \n");
    do{
        if(count > 0) 
            buffer.point_end = buffer.point_start;
        buffer = get_string(buffer, buf_len);
        if(*(buffer.point_start) == '\n' && count == 0)
        {
            printf("you don't input anything\n");
            break;
        }
        *(buffer.point_end - 1) = '\0';
        current_buf_len = strlen(buffer.point_start);
        store_strings(str_dre_dre, count, current_buf_len, buffer);        
        if(count == str_cont -1)
            str_dre_dre = str_start =add_str_memory(str_start, count, str_cont);
        count++;
        printf("you are already input %d string%s.\n",count, (count == 1) ? "" : "s");
        printf("continue?(Y/N)");
        fflush(stdin);      
      }
    while(tolower((char)getchar()) == 'y');
//output strings
    printf("output strings: \n"); 
    output_strings(str_start, count);
//release memory
    for(int i =0; i < count; ++i)
    {
        free(*(str_start + i));
        *(str_start + i) = NULL;
    }
    free(buffer.point_start);
    buffer.point_end = buffer.point_start = NULL;
    str_start = str_dre_dre = NULL;
    return 0;
}
/*************function declare******************/
//memory initialization for string's address
char** memory_init_1(int size)
{   
    char** str = NULL;
    str = (char**)malloc(size);
        if(str == NULL)
        {
             printf("malloc() function error!!!\n");
             exit(1);
        }
    return str;
}
//memory initialization for input buffer 
char* memory_init_2(int size)
{
    char* p_buf = NULL; 
    p_buf = (char*)malloc(size);
       if(p_buf == NULL)
       {
           printf("malloc() function error!!!\n");
           exit(1);
       }
    return p_buf;
}
//input string and add input buffer memory 
point get_string(point buf, int length)
{
    fflush(stdin);
    char* buf_temp = NULL;
    do
    {
        *(buf.point_end) = getchar();
        buf.point_end++;
        if(buf.point_end - buf.point_start == length)   
        {
             length += buffer_len_inc;
             buf_temp = (char*)realloc(buf.point_start, length * sizeof(char));
                 if(buf_temp == NULL)
                 {
                     printf("realloc() function error!!!\n"); 
                     exit(1);
                 }
             buf.point_end = buf_temp + (buf.point_end - buf.point_start);
             buf.point_start = buf_temp;
             buf_temp = NULL;
        }    
    }
    while(*(buf.point_end - 1) != '\n');
    return buf;
}
//store string 
void store_strings(char** str, int cont, int cur_buf_len, point buf)
{
    int test_int = 0;
    *(str + cont) = (char*)malloc(cur_buf_len);
    if(*(str + cont) == NULL)
    {
        printf("malloc() function error");
        exit(1);
    }
    test_int = strcpy_s(*(str + cont), cur_buf_len + 1, buf.point_start);
    if(test_int == !0)
    {
        printf("strcpy_s() function error!!!\n");
        exit(1);
    }
}
//add memory to store string
char** add_str_memory(char** str_st ,int count, int str_count)
{
    char** str_dre_temp = NULL;
    str_count += string_cont_inc;
    str_dre_temp = (char**)realloc(str_st, str_count * sizeof(char*));
    if(str_dre_temp == NULL)
    {
        printf("realloc() function error!!!\n");
        exit(1);
    }
    return str_dre_temp;
}
//output stings function
void output_strings(char** str_st, int count)
{
    for(int i = 0; i < count; ++i)
    {
        printf("%s\n", *(str_st + i));
    }
}

上一版程序:
http://bbs.bccn.net/thread-443501-1-1.html

程序功能还是输入/输出不限制长度,不限制个数的字符串。
在听了各位大神的意见后,我根据在国外网站上看到的程例,自己模仿写了一个第二版程序,代码相对于上一版main()函数里少了不少东西,都用子函数代替了原来又臭又长的main()函数里的内容。但是感觉逻辑还是比较难懂的,主要就是在char** 和 char* 这个难懂。
搜索更多相关主题的帖子: count 
2015-04-06 14:47
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:10 
有进步,还需继续努力。

授人以渔,不授人以鱼。
2015-04-06 21:06
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
得分:10 
程序代码:
/***************************************************************************
*                                                                          *
* File    : strings.c                                                      *
*                                                                          *
* Purpose : Unlimit string's count and string's size input.                *
*                                                                          *
* Author  : Dennis          Date  :  00/00/0000                            *
*                                                                          *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _STDC_WANT_LIB_EXT1_ 1
#define buffer_len 10
#define buffer_len_inc 5
#define string_cont 5
#define string_cont_inc 2
typedef struct point point;
struct point
{
    char* point_start;
    char* point_end;
} buffer = {NULL, NULL};

//function prototype
char** memory_init_1(int size);
char* memory_init_2(int size);
point get_string(point buf, int length);
//char* add_buf_memory(char* start, char* end);
void store_strings(char** str, int cont, int cur_buf_len, point buf);
char** add_str_memory(char** str_st , int count, int str_count);
void output_strings(char** str_st, int count);

int main(void)
{
//variable declare
    int buf_len = buffer_len;
    int current_buf_len = 0;

    char** str_dre_dre = NULL;
    char** str_start = NULL;
    int count = 0;
    int str_cont = string_cont;
//初始化分配内存
    str_start = str_dre_dre = memory_init_1(str_cont * sizeof(char*));
    buffer.point_start = buffer.point_end =  memory_init_2(buf_len * sizeof(char));
//
    printf("input something you want : \n");

    do
    {
        if (count > 0)
            buffer.point_end = buffer.point_start;

        buffer = get_string(buffer, buf_len);

        if (*(buffer.point_start) == '\n' && count == 0)
        {
            printf("you don't input anything\n");
            break;
        }

        *(buffer.point_end - 1) = '\0';
        current_buf_len = strlen(buffer.point_start);
        store_strings(str_dre_dre, count, current_buf_len, buffer);

        if (count == str_cont - 1)
            str_dre_dre = str_start = add_str_memory(str_start, count, str_cont);

        count++;
        printf("you are already input %d string%s.\n", count, (count == 1) ? "" : "s");
        printf("continue?(Y/N)");
        fflush(stdin);
    }
    while (tolower((char)getchar()) == 'y');

//output strings
    printf("output strings: \n");
    output_strings(str_start, count);

//release memory
    for (int i = 0; i < count; ++i)
    {
        free(*(str_start + i));
        *(str_start + i) = NULL;
    }

    free(buffer.point_start);
    buffer.point_end = buffer.point_start = NULL;
    str_start = str_dre_dre = NULL;
    return 0;
}
/*************function declare******************/
//memory initialization for string's address
char** memory_init_1(int size)
{
    char** str = NULL;
    str = (char**)malloc(size);

    if (str == NULL)
    {
        printf("malloc() function error!!!\n");
        exit(1);
    }

    return str;
}
//memory initialization for input buffer
char* memory_init_2(int size)
{
    char* p_buf = NULL;
    p_buf = (char*)malloc(size);

    if (p_buf == NULL)
    {
        printf("malloc() function error!!!\n");
        exit(1);
    }

    return p_buf;
}
//input string and add input buffer memory
point get_string(point buf, int length)
{
    fflush(stdin);
    char* buf_temp = NULL;

    do
    {
        *(buf.point_end) = getchar();
        buf.point_end++;

        if (buf.point_end - buf.point_start == length)
        {
            length += buffer_len_inc;
            buf_temp = (char*)realloc(buf.point_start, length * sizeof(char));

            if (buf_temp == NULL)
            {
                printf("realloc() function error!!!\n");
                exit(1);
            }

            buf.point_end = buf_temp + (buf.point_end - buf.point_start);
            buf.point_start = buf_temp;
            buf_temp = NULL;
        }
    }
    while (*(buf.point_end - 1) != '\n');

    return buf;
}
//store string
void store_strings(char** str, int cont, int cur_buf_len, point buf)
{
    int test_int = 0;
    *(str + cont) = (char*)malloc(cur_buf_len);

    if (*(str + cont) == NULL)
    {
        printf("malloc() function error");
        exit(1);
    }

    test_int = strcpy_s(*(str + cont), cur_buf_len + 1, buf.point_start);

    if (test_int == !0)
    {
        printf("strcpy_s() function error!!!\n");
        exit(1);
    }
}
//add memory to store string
char** add_str_memory(char** str_st , int count, int str_count)
{
    char** str_dre_temp = NULL;
    str_count += string_cont_inc;
    str_dre_temp = (char**)realloc(str_st, str_count * sizeof(char*));

    if (str_dre_temp == NULL)
    {
        printf("realloc() function error!!!\n");
        exit(1);
    }

    return str_dre_temp;
}
//output stings function
void output_strings(char** str_st, int count)
{
    for (int i = 0; i < count; ++i)
    {
        printf("%s\n", *(str_st + i));
    }
}


试试软件自动排版 献丑了
2015-04-07 12:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-443617-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.862401 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved