#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 300
void str_replace(char * str1, char * str2, char * str3){
int i, j, k, done, count = 0, gap = 0;
char temp[MAXSIZE];
for(i = 0; i < strlen(str1); i += gap){
if(str1[i] == str2[0]){
done = 0;
for(j = i, k = 0; k < strlen(str2); j++, k++){
if(str1[j] != str2[k]){
done = 1;
gap = k;
break;
}
}
if(done == 0){
for(j = i + strlen(str2), k = 0; j < strlen(str1); j++, k++){
temp[k] = str1[j];
}
temp[k] = '\0';
for(j = i, k = 0; k < strlen(str3); j++, k++){
str1[j] = str3[k];
count++;
}
for(k = 0; k < strlen(temp); j++, k++){
str1[j] = temp[k];
}
str1[j] = '\0';
gap = strlen(str2);
}
}else{
gap = 1;
}
}
if(count == 0){
printf("Can't find the replaced string!\n");
}
return;
}
unsigned int Str_Num(char SS,unsigned char *s)
{
char *begin;
unsigned char i,len,base=1,j=0,k=0;
unsigned int dat=0;
len=strlen(s);
for(i = 0 ; i < len; i++)
{
if((*s >= '0')&&(*s <= '9'))
{
if(j==0 && base==SS)
{
begin=s;
k++;
}
else if(j==0)
{
base++;
k++;
}
if(k==SS)
{
dat = dat*10 + *begin - '0';
begin++;
}
j++;
}
else j=0;
s++;
}
return dat;
}
char *insert(char *s1, char *s2, int n)
{
int len1 = 0, len2 = 0, i, j = 0, k = 0;
char s3[50];
if (s1 == NULL)
return NULL;
if (s2 == NULL)
return s1;
len1 = strlen(s1);
if(n > len1)
return NULL;
len2 = strlen(s2);
for ( i = 0; i < n; i++)
j++;
for ( i = 0; i < len1; i++)
s3[k++] = s1[i];
for ( i = 0; i < len2; i++)
s1[j++] = s2[i];
for( i = n; i < len1; i++)
s1[j++] = s3[i];
s1[j] = '\0';
return s1;
}
char *str_del(char *s1, int n)
{
int len1 = 0;
if (s1 == NULL)
return NULL;
len1 = strlen(s1);
memmove(s1+n,s1+n+1,len1-n-1);
return s1;
}
int str_get_longer(int long1, int long2)
{
printf("\r\n long1:%d long2:%d ",long1,long2);
if(long1>=long2)
{
return long1;
}
else
{
return long2;
}
}
#define FRACTION_MARK_S '{'
#define FRACTION_MARK_M '@'
#define FRACTION_MARK_E '}'
int str_get_fraction_len(char *str , int *pall_len , int *pfraction_len )
{
int fraction_cnt = 0;
int fraction_len = 0;
int all_len = 0;
char *p_s = NULL;
char *p_e = NULL;
char *p_m = NULL;
if(str == NULL)
{
return 0;
}
p_e = str;
while(1)
{
p_s = strchr(p_e,FRACTION_MARK_S);
if(p_s == NULL)
{
*pall_len = all_len;
*pfraction_len = fraction_len;
return fraction_cnt;
}
fraction_cnt++;
p_m = strchr(p_s,FRACTION_MARK_M);
p_e = strchr(p_s,FRACTION_MARK_E);
printf("\r\n s:%08x m:%08x e:%08x",(unsigned int)p_s,(unsigned int)p_m,(unsigned int)p_e);
fraction_len = fraction_len + str_get_longer((int)(p_m-p_s+1-2),(int)(p_e-p_m+1-2));
all_len = all_len+p_e-p_s+1;
}
return fraction_cnt;
}
#define LINE_MAX_CHAR_NUM 16
typedef struct
{
char fz[LINE_MAX_CHAR_NUM];
char fm[LINE_MAX_CHAR_NUM];
char num[LINE_MAX_CHAR_NUM];
char *p_mark;
}fraction_str_t;
fraction_str_t* str_get_next_fraction(char*str)
{
fraction_str_t *res = NULL;
int t;
if(str == NULL)
{
return NULL;
}
char *p_s = strchr(str,FRACTION_MARK_S);
if(p_s == NULL)
{
return NULL;
}
res = malloc(sizeof(fraction_str_t));
res->p_mark = strchr(str,FRACTION_MARK_E)+1;
if(p_s == str)
{
t = sscanf(str,"{%[^@]@%[^}]",res->fz,res->fm);
res->num[0] = 0;
}
else if(p_s+1 == strchr(str,FRACTION_MARK_M))
{
t = sscanf(str,"{%*[^@]@%[^}]",res->fm);
res->num[0] = 0;
res->fz[0] = 0;
}
else
{
t = sscanf(str,"%[^{]{%[^@]@%[^}]",res->num,res->fz,res->fm);
}
return res;
}
int main()
{
char str_test[100] = {"123+{12@1+2}+138+{(1+2)*3@1+2}"};
char str_test2[100] = {"12+{1@1234}"};
char str_output1[50] = {0};
char str_output2[50]= {0};
char str_output3[50] = {0};
char str_output4[50]= {0};
insert(str_test, "", 4);
str_del(str_test, 5);
str_replace(str_test, "{", "((");
str_replace(str_test, "@", ")/(");
str_replace(str_test, "}", "))");
int t = sscanf("{123+123@11}64{4@3456}4","%*[^{]{%[^@]@%[^}]",str_output1,str_output2);
printf("\r\n%s",str_test);
printf("\r\nt %d \r\nS1:%s",t,str_output1);
printf("\r\nS2:%s",str_output2);
int all_len ;
int fraction_len;
int cnt;
cnt = str_get_fraction_len(str_test , &all_len , &fraction_len );
printf("\r\nstr_get_fraction_len %d %d %d",all_len,fraction_len,cnt);
fraction_str_t * fraction = str_get_next_fraction(str_test2);
printf("\r\nnum:%s ,fz:%s,fm:%s [%s]",fraction->num,fraction->fz,fraction->fm,fraction->p_mark);
if(fraction->p_mark != NULL)
{
fraction = str_get_next_fraction(fraction->p_mark);
if(fraction)
{
printf("\r\n===2====\r\nnum:%s ,fz:%s,fm:%s",fraction->num,fraction->fz,fraction->fm);
}
else
{
}
}
printf("\r\nt strlen %d \r\nS1:%s",strlen(""));
return 0;
}