#include <stdio.h> #include <stdlib.h> #include <assert.h>
typedef struct is_char { char ch; int num; struct is_char* next; }IS_CHAR; typedef struct is_china { char china[3]; int num; struct is_china* next; }IS_CHINA;
IS_CHAR* init_charhead() { IS_CHAR* head = (IS_CHAR*)malloc(sizeof(IS_CHAR)); if(head == NULL) { printf("char head malloc error\n"); return NULL; } head->next = NULL; return head; }
IS_CHINA* init_chinahead() { IS_CHINA* head = (IS_CHINA*)malloc(sizeof(IS_CHINA)); if(head == NULL) { printf("china head malloc error\n"); return NULL; } head->next = NULL; return head; }
int is_in_char(IS_CHAR* char_head, char ch, int flag) { assert(char_head != NULL); IS_CHAR* p = char_head->next; while(p != NULL) { if( p->ch==ch ) { if(flag == 0) p->num++; else { if(p->num > 0) p->num--; } return 1; } p = p->next; } return 0; }
int is_in_china(IS_CHINA* china_head, char* chinese, int flag) { assert(china_head != NULL); IS_CHINA* p = china_head->next; while(p != NULL) { if(strcmp(p->china,chinese) == 0) { if(flag == 0) p->num++; else { if(p->num > 0) p->num--; } return 1; } p = p->next; } return 0; }
void my_print_char(IS_CHAR* char_head) { printf("\n char \n"); IS_CHAR* p = char_head->next; while(p != NULL) { printf("char %c num %d\n",p->ch,p->num); p = p->next; } }
void my_print_china(IS_CHINA* china_head) { printf("\n chinese \n"); IS_CHINA* p = china_head->next; while(p != NULL) { printf("str %s num %d\n",p->china,p->num); p = p->next; } }
int is_include(const char *a, const char *b) { char* t = (char*)a; char* s = (char*)b; printf("t is %s\n s is %s\n",t,s); char chinese[3]; int res = 1; IS_CHAR* char_head = init_charhead(); IS_CHINA* china_head = init_chinahead(); while(*s != '\0') { if(*s&0x80) { chinese[0] = *s; s++; chinese[1] = *s; s++; chinese[2] = '\0'; if(is_in_china(china_head, chinese, 0) == 0) { printf("now add %s\n",chinese); IS_CHINA* china_p = (IS_CHINA*)malloc(sizeof(IS_CHINA)); china_p->num = 1; sprintf(china_p->china,"%s",chinese); china_p->next = china_head->next; china_head->next = china_p; } } else { if(is_in_char(char_head, *s, 0) == 0) { printf("now add %c\n",*s); IS_CHAR* char_p = (IS_CHAR*)malloc(sizeof(IS_CHAR)); char_p->num = 1; char_p->ch = *s; char_p->next = char_head->next; char_head->next = char_p; } s++; } } printf("s is over\n"); my_print_char(char_head); my_print_china(china_head); while(*t != '\0') { if(*t & 0x80) { chinese[0] = *t; t++; chinese[1] = *t; t++; chinese[2] = '\0'; is_in_china(china_head, chinese, 1); } else { is_in_char(char_head, *t, 1); t++; } } printf("t is over\n"); my_print_char(char_head); my_print_china(china_head); IS_CHAR* char_tmp1 = char_head->next; IS_CHAR* char_tmp2 = char_tmp1; while( char_tmp1!=NULL ) { if(char_tmp1->num != 0) res = 0; char_tmp2 = char_tmp1->next; free(char_tmp1); char_tmp1 = char_tmp2; } free(char_head); char_head = NULL; IS_CHINA* china_tmp1 = china_head->next; IS_CHINA* china_tmp2 = china_tmp1; while( china_tmp1!=NULL ) { if(china_tmp1->num != 0) res = 0; china_tmp2 = china_tmp1->next; free(china_tmp1); china_tmp1 = china_tmp2; } free(china_head); china_head = NULL; return res; }
int main(int argc, char *argv[]) { char* a = "acac我是我我是123123zjzxxtxlzjzxxtxl"; char* b = "ac我我是123zjzxxtxl"; printf("str:%s\nand str:%s\n",a,b); is_include(a, b) ? printf("included\n") : printf("not included\n"); system("PAUSE"); return 0; }
|