编写一个程序实现求解任意算术表达式的值

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/15 19:31:06
编写一个程序实现求解任意算术表达式的值
xXoEW#ŷBTxn(:_ƾQ)R*^T<xhԿ&g{s"۝LN_~٫ggXv/:;{?y닋7o'/cf{ߧHɵ'(р!>͈7?Ǯ7'fnzAbS9a}`ZnK|ya0T !ǿ0)& L٬O$]^·(G!=_ &ؔkVj1jSro l̘Ohr2wP #,<> RrȖxG}2(N9/ sjTT.d&ᔑs>#[r |y< WAj U80j@$OxBI-kc (M2ʁvī"fLۈ5hA܄G)ClF$aPjJXb@p!BϏr:䘲@'gF㘸63 h2J,jxFR{T`F< :h0)W6% 2 cآ2bXKhy 8=8mϺ[qp#Dk9fs땸S=SZmbg.;>Ϩw!z-#^TPJńqf3OF!5rK#ؠk!VEm@Qj}~ .gz!I aNBhCk^}݇Vw~42 !k_mVOzAz-f{Tn%6IS͌ q'E0|G0|?a Qٯ2QjoAjEaTKvu+@X1JJ0lk }Ԧ%pKƎQfp|lpV;55X\JJ>2'

编写一个程序实现求解任意算术表达式的值
编写一个程序实现求解任意算术表达式的值

编写一个程序实现求解任意算术表达式的值
#include
#include
#include
#include
#include
#include
int Check_bracket(char *p_str);
int Get_SubString(char *p_substr, const char *p_str, int *p_bpos, int *p_length);
int Calc_SubExpr(char *p_result, const char *p_expr);
void Insert_SubExpr(char *p_str, const char *p_result, int p_bpos, int p_length);
int Change_StrToFloat(float *p_fdata, char *p_operator, const char *p_result, int *p_num);
int main( int argc, char *argv[])
{
int i;
int j;
int i_sts;
int i_sts1;
int i_sts2;
int i_pos;
int i_length;
char i_expr[256];
char i_subexpr[128];
char i_rusult[128];



while (1){
i = 0;
j = 0;
i_sts = 0;
i_sts1 = 0;
i_sts2 = 0;
i_pos = 0;
i_length = 0;
memset(i_expr, 0x00, sizeof(i_expr));
memset(i_subexpr, 0x00, sizeof(i_subexpr));
memset(i_rusult, 0x00, sizeof(i_rusult));
/* */

printf("please intput the expression > ");
gets( i_expr );

if(memcmp(i_expr,"exit",sizeof("exit"))==0) break;
/* */
while ( i_sts == 0 )
{
memset(i_subexpr, 0x00, sizeof(i_subexpr));
memset(i_rusult, 0x00, sizeof(i_rusult));
i_sts = Check_bracket( i_expr );
if( i_sts == -1 )
{
printf("This is not a expression\n");
break;
}
else if (i_sts == 2)
{
i_sts2 = Calc_SubExpr(i_rusult, i_expr);
if (i_sts2 != 0)
{
printf("This is not a expression\n");
break;
}
break;
}
else if (i_sts == 1)
{
printf("This is not a expression\n");
break;
}
i_sts1 = Get_SubString(i_subexpr, i_expr, &i_pos, &i_length);
if (i_sts1 == 0)
{
i_sts2 = Calc_SubExpr(i_rusult, i_subexpr);
if (i_sts2 != 0)
{
printf("This is not a expression\n");
break;
}
Insert_SubExpr(i_expr, i_rusult, i_pos, i_length);
}

}


puts( i_rusult );
}
return 0;
}
//
int Change_StrToFloat(float *p_fdata, char *p_operator, const char *p_result, int *p_num)
{
float i_temp;
int i;
int j;
int k;
int m;
int sts;
char data[128];

i = 0;
j = 0;
k = 0;
m = 0;
i_temp = 0;
sts = 0;
memset(data, 0x00, sizeof(data));

for(i=0; p_result[i] != '\0'; i++)
{
if ((p_result[0] == '.') || (p_result[0] == '+')
|| (p_result[0] == '*') || (p_result[0] == '/'))
{
return -1;
}


switch( p_result[i] ) {
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
case '0' :
data[j++] = p_result[i];
break;
case '.' :
if( ( (p_result[i+1] == '+') || (p_result[i+1] == '-')
|| (p_result[i+1] == '*') || (p_result[i+1] == '/')
|| (p_result[i+1] == '.') || (p_result[i+1] == (char)0))
&& (sts = 1 ))
{
return -1;
}
data[j++] = p_result[i];
sts = 1;
break;
case '+' :
case '-' :
case '*' :
case '/' :
if( (p_result[i+1] == '+') || ( (p_result[i+1] == '-') && ( (p_result[i+2] == '+') || (p_result[i+2] == '-' )
|| (p_result[i+2] == '*') || (p_result[i+2] == '/' )
|| (p_result[i+2] == '.') || (p_result[i+2] == (char)0)))
|| (p_result[i+1] == '*') || (p_result[i+1] == '/')
|| (p_result[i+1] == '.') || (p_result[i+1] == (char)0))
{
return -1;
}
i_temp = (float)atof(data);
p_fdata[k++] = i_temp;
p_operator[m++] = p_result[i];
j = 0;
memset(data, 0x00, sizeof(data));
sts = 0;
break;
default:
break;
}
}
i_temp = (float)atof(data);
p_fdata[k] = i_temp;
*p_num = m;
return 0;
}