#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 300
#define FRACTION_MARK_S '{'
#define FRACTION_MARK_M '@'
#define FRACTION_MARK_E '}'
#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));
memset(res,0,sizeof(fraction_str_t));
res->p_mark = strchr(str,FRACTION_MARK_E)+1;
if(p_s != str)
{
t = sscanf(str,"%[^{]",res->num);
}
{
t = sscanf(p_s,"{%[^@]",res->fz);
}
{
t = sscanf(strchr(str,FRACTION_MARK_M),"@%[^}]",res->fm);
}
return res;
}
int test1()
{
fraction_str_t * fraction = NULL;
char str_test2[100] = {"11+{1*23@123+4}"};
fraction = str_get_next_fraction(str_test2);
if(fraction!= NULL)
{
printf("\r\n1,num:%s ,fz:%s,fm:%s",fraction->num,fraction->fz,fraction->fm);
free(fraction);
}
char str_test3[100] = {"12+{1+23@}"};
fraction = str_get_next_fraction(str_test3);
if(fraction!= NULL)
{
printf("\r\n2,num:%s ,fz:%s,fm:%s",fraction->num,fraction->fz,fraction->fm);
free(fraction);
}
char str_test4[100] = {"13+{@}"};
fraction = str_get_next_fraction(str_test4);
if(fraction!= NULL)
{
printf("\r\n3,num:%s ,fz:%s,fm:%s",fraction->num,fraction->fz,fraction->fm);
free(fraction);
}
char str_test5[100] = {"14"};
fraction = str_get_next_fraction(str_test5);
if(fraction!= NULL)
{
printf("\r\n4,num:%s ,fz:%s,fm:%s",fraction->num,fraction->fz,fraction->fm);
free(fraction);
}
return 0;
}
int is_num(char c)
{
if('0'<= c && c <= '9' || c=='.')
{
return 1;
}
return 0;
}
char* is_dai_fenshu(char *str)
{
int index = 0;
int num_cnt = 0;
int s_len = 0;
if(str == NULL)
{
return NULL;
}
if(strchr(str, '{') == NULL)
{
return NULL;
}
s_len = strlen(str);
for(index = 0; index < s_len ; index++)
{
if(is_num(str[index]))
{
num_cnt++;
}
else
{
if(str[index] == '{' && num_cnt != 0)
{
return &str[index-num_cnt];
}
num_cnt = 0;
}
}
return NULL;
}
void main()
{
const char* test_str[] ={
"1234+12{33@44}+33{66@55}",
"2222{33@44}+33{66@55}",
"+{33@44}+33{66@55}",
"0.355{33@44}+33{66@55}",
"333{33+44@44}+33{66@55}",
"333{(33+44)*55@44}+33{66@55}"
"88{1@2}",
"{1@2}",
"{1@2}+{1@2}+5{1@2}",
"{1@2}+2{1@2}+5{1@2}",
};
for(int i = 0; i < sizeof(test_str)/sizeof(const char*) ; i++)
{
char * p = is_dai_fenshu((char *)test_str[i]);
if(p)
{
printf("\r\n[%d]%s",i,p);
}
else
{
printf("\r\n[%d]%s",i,"NULL");
}
}
}