#ifndef CHAR_STRUCT_H
#define CHAR_STRUCT_H
#define ERR 0
#define OK 1
#include <malloc.h>
typedef int Status;
typedef struct {
char *ch;/*字符串头指针*/
int lenght;/*字符串长度*/
} C_string;/*字符串结构 */
Status initstring( C_string *op, char *cha ) {/*初始化字符串*/
int ix;
char *c = cha;
if( !op->ch )
free( op->ch );
for( ix = 0; *c != '\0' ; ++ix, ++c );
if( ix == 0 )
{
op->ch = NULL;
op->lenght = 0;
}
else
{
op->ch = ( char* ) malloc ( ix * sizeof( char ) );
if( !op->ch )
return ERR;
for( ix = 0; cha[ ix ] != '\0'; ++ix )
op->ch[ ix ] = cha[ ix ];
op->ch[ ix ] = '\0';
op->lenght = ix;
}
return OK ;
}
int lenght( C_string *op ){ return op->lenght; }/*返回字符串长度*/
Status empty ( C_string *op ) { /*测试字符串是否为空*/
return op->lenght > 0 ? OK : ERR;
}
Status charcompare( C_string *op, C_string *pv ){/*比较字符串*/
int ix;
if( op->lenght == 0 || pv->lenght == 0 )
return ERR;
if( op->lenght != pv->lenght )
return ERR;
for( ix = 0; ix < op->lenght; ++ix )
if( op->ch[ ix ] != pv->ch[ ix ] )
return ERR;
return OK;
}
Status cls( C_string *op ){/*清空字符串*/
if( !op->ch )
{
free( op->ch );
op->ch = NULL;;
}
op->lenght = 0;
return OK;
}
Status char_print( C_string *op ) {/*打印*/
if( !op->ch )
return ERR;
printf( "[ %d ] ( %s )\n", op->lenght, op->ch );
return OK;
}
#endif