| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 678 人关注过本帖
标题:让代码飞会__不限长度、个数字符串输入/输出__第三版
只看楼主 加入收藏
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
结帖率:90.91%
收藏
已结贴  问题点数:20 回复次数:5 
让代码飞会__不限长度、个数字符串输入/输出__第三版
程序代码:
/***************************************************************************
*                                                                          *
* File    : strings.c                                                      *
*                                                                          *
* Purpose : Unlimited string's count and string's size input.              *
*                                                                          *
* Author  : Dennis          Date  :  9/4/2015                              *
*                                                                          *
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define _STDC_WANT_LIB_EXT1_  1
#define buffer_len 10
#define buffer_len_inc 5
typedef struct string string;
struct string
{
    char* start;
    char* end;
    int str_length;
    string* next;
};
//function prototype
string* sturct_memory(size_t size);
char* memory_init(size_t size);
string* get_string(int length, string* buf);
string* store_strings(string* buf);
string* add_buf_memory(int length, string* buff);
void output_release(string* cur, string* pre);
int main(void)
{
//variable declare    
    string* first = NULL;
    string* current = NULL;
    string* previous = NULL;
    int buf_len = buffer_len;
    int count= 0;
    printf("input something you want : \n");
    do{
        current = sturct_memory(sizeof(string));
        if(first == NULL)
            first = current;
        if(previous != NULL)
            previous->next = current;
        current->start = current->end = memory_init(buf_len * sizeof(char));
        current = get_string(buf_len, current);
        if(*(current->start) == '\n' && count == 0)
        {
            printf("you don't input anything\n");
            break;
        }
        *(current->end - 1) = '\0';
        current->str_length = strlen(current->start);
        store_strings(current);        
        count++;
        current->next = NULL;
        previous = current;
        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 and release memory
    printf("output strings: \n"); 
    current = first;
    output_release(current, previous);
    return 0;
}
/***************function declare*****************/
string* sturct_memory(size_t size)
{
    string* temp = NULL;
    temp = (string*)malloc(size);
    if(temp == NULL)
    {
        fprintf_s(stderr, "malloc() function error!!!\n");
        exit(1);
    }
    return temp;
}
//memory initialization for input buffer
char* memory_init(size_t size)
{
    char* p_buf = NULL; 
    p_buf = (char*)malloc(size);
       if(p_buf == NULL)
       {
           fprintf_s(stderr, "malloc() function error!!!\n");
           exit(1);
       }
    return p_buf;
}
//input strings  
string* get_string(int length, string* buf)
{
    int buff_len = length;
    fflush(stdin);
    do
    {
        *(buf->end) = getchar();
        (buf->end)++;
        if(buf->end - buf->start == length)       
        buf = add_buf_memory(buff_len, buf);      
    }
    while(*(buf->end - 1) != '\n');
    return buf;
}
//store strings 
string* store_strings(string* buf)
{
    char* store_temp = NULL;
    int test_int = 0;
    store_temp = (char*)malloc(buf->str_length);
    if(store_temp == NULL)
    {
        fprintf_s(stderr, "malloc() function error!!!\n");
        exit(1);
    }
    test_int = strcpy_s(store_temp, buf->str_length + 1, buf->start);
    if(test_int == !0)
    {
        fprintf_s(stderr, "strcpy_s() function error!!!\n");
        exit(1);
    }
    buf->start = store_temp;
    return buf;
}
//output and release function
void output_release(string* cur, string* pre)
{
do
    {
        printf_s("%s\n", cur->start);
        pre = cur;
        cur = cur->next;
        free(pre);
        pre = NULL;
    }
    while(cur != NULL);
}
//add buffer memory
string* add_buf_memory(int length, string* buff)
{ 
        char* buf_temp = NULL;  
        length += buffer_len_inc;
        buf_temp = (char*)realloc(buff->start, length * sizeof(char));
        if(buf_temp == NULL)
        {
            fprintf_s(stderr, "realloc() function error!!!\n");
            exit(1);
        }
        buff->end = buf_temp + (buff->end - buff->start);
        buff->start = buf_temp;
        buf_temp = NULL;
        return buff;    
}

http://bbs.bccn.net/thread-443501-1-1.html  第一版

http://bbs.bccn.net/thread-443617-1-1.html  第二版

第一版是原始版,main()函数又臭又长,逻辑复杂,思路不清晰。第二版将第一版的各个功能模块写成了子函数,使得main()函数清晰、整洁一点。
第三版摒弃了前两版使用的char** 类型,这是逻辑复杂的根源。采用了单向链表,这样程序逻辑没有前两版难理解,同时也精简了一定的代码,减少了大量的变量声明。
还希望各位能提出宝贵意见,能让代码清晰、易理解。
搜索更多相关主题的帖子: count 字符串 
2015-04-09 10:40
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
收藏
得分:0 

还一个问题忘了写了,就是关于可选的安全函数,为什么编译器会有Warning?
#define _STDC_WANT_LIB_EXT1_  1

这条语句没用吗?
2015-04-09 10:45
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:20 
你最好使用安全函数,而不是屏蔽它。

授人以渔,不授人以鱼。
2015-04-09 10:50
_Dennis_h
Rank: 2
等 级:论坛游民
帖 子:37
专家分:15
注 册:2015-3-6
收藏
得分:0 
以下是引用TonyDeng在2015-4-9 10:50:05的发言:

你最好使用安全函数,而不是屏蔽它。

书上说,对C11的支持需要检查是否定义了 _STDC_LIB_EXT1_ 符号,如果没有定义,就需要
#define _STDC_WANT_LIB_EXT1_  1
这条语句,才能使用可选函数,禁用可选函数用这条语句
#define _STDC_WANT_LIB_EXT1_  0
2015-04-09 11:16
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
安全型函数跟C11没有关系

授人以渔,不授人以鱼。
2015-04-09 11:18
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
动态字符串?复杂了一点,还有改造空间。另外,注意测试程序的正确性,尤其是在发生realloc()情形的时候。

授人以渔,不授人以鱼。
2015-04-09 14:00
快速回复:让代码飞会__不限长度、个数字符串输入/输出__第三版
数据加载中...
 
   



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

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