让代码飞会__不限长度、个数字符串输入/输出__第三版
程序代码:
/***************************************************************************
* *
* 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** 类型,这是逻辑复杂的根源。采用了单向链表,这样程序逻辑没有前两版难理解,同时也精简了一定的代码,减少了大量的变量声明。
还希望各位能提出宝贵意见,能让代码清晰、易理解。









