快速接帖--计算数学表达式的值,马上接帖 给出链接的也给分int ComputeExpression(CString& str);做一个类似这样的函数 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <C++程序设计语言>(贝尔实验室,Bjarne Stroustrup著)第六章一开头就有个用C++实现中缀表达式的例子程序,运算符没实现几个,结构却是完善,扩充它应该不是难事。 http://www.relisoft.com/book/lang/project/index.htm 程序比较小,我就贴这里好了,希望对你有用./* ======================================== *//* 程式实例: 函数计算处理.cpp *//* 计算中序形式的任意函数表达式的值 *//* ======================================== */#include <stdlib.h>#include <string.h>#include <math.h>#include <ctype.h>#include <stdio.h>#define M 21#define N 6struct prioprities { //创建优先级表结构 char op[6]; int prioprity1;};//运算符与优先级的对应表struct prioprities operators[M]={"+",1,"-",1,"*",2,"/",2,"%",2, //5 "(",0,")",0,"pow",3,"sin",3,"cos",3,"cosh",3,"acos",3, //7 "asin",3,"sinh",3,"exp",3,"log",3,"ln",3,"tan",3,"atan",3,//7 "fabs",3,",",0};struct stack_node /* 栈的结构宣告 */{ double data; /* 栈资料 */ struct stack_node *next; /* 指向下一节点 */};typedef struct stack_node stack_list; /* 串列新型态 */typedef stack_list *link; /* 串列指标新型态 */link operands = NULL; /* 运算元栈指标 *//* ---------------------------------------- *//* 栈资料的存入 *//* ---------------------------------------- */link push(link stack,double value){ link new_node; /* 新节点指标 */ /* 配置节点记忆体 */ new_node = ( link ) malloc(sizeof(stack_list)); if ( !new_node ) { printf("记忆体配置失败! \n"); return NULL; /* 存入失败 */ } new_node->data = value; /* 建立节点内容 */ new_node->next = stack; /* 新节点指向原开始 */ stack = new_node; /* 新节点成为栈开始 */ return stack;}/* ---------------------------------------- *//* 栈资料的取出 *//* ---------------------------------------- */link pop(link stack,double *value){ link top; /* 指向栈顶端 */ if ( stack != NULL ) { top = stack; /* 指向栈顶端 */ stack = stack->next; /* 栈指标指向下节点 */ *value = top->data; /* 取出资料 */ free(top); /* 释回节点记忆体 */ } else { printf("数据栈为空,操作失败!\n"); exit(1); *value = -1; } return stack; /* 传回栈指标 */ }/* ---------------------------------------- *//* 检查栈是否是空的 *//* ---------------------------------------- */int empty(link stack){ int flag=0; if ( stack == NULL ) /* 是否是空 */ flag=1; /* 空的 */ return flag; /* 不是空的 */}/* ---------------------------------------- *//* 是否是运算子 *//* ---------------------------------------- */int isoperator(char *op1){ int flag=0; for(int i=0; i<M; i++) if(strcmp(operators[i].op, op1)==0) { flag=1; break; } return flag;}/* ---------------------------------------- *//* 运算子的优先权 *//* ---------------------------------------- */int priority(char *op1){ int i; for(i=0; i<M; i++) if(strcmp(operators[i].op, op1)==0) return operators[i].prioprity1;}struct stack_operator /* 栈的结构宣告 */{ char operator1[N]; /* 栈资料 */ struct stack_operator *next; /* 指向下一节点 */};//运算符栈的与操作typedef struct stack_operator stack_op; /* 串列新型态 */typedef stack_op *oplink; /* 串列指标新型态 */oplink operator_stack = NULL;/* ---------------------------------------- *//* 栈资料的存入 *//* ---------------------------------------- */oplink push_op(oplink stack,char *op){ oplink new_node; /* 新节点指标 */ /* 配置节点记忆体 */ new_node = ( oplink ) malloc(sizeof(stack_op)); if ( !new_node ) { printf("记忆体配置失败! \n"); return NULL; /* 存入失败 */ } strcpy(new_node->operator1,op); /* 建立节点内容 */ new_node->next = stack; /* 新节点指向原开始 */ stack = new_node; /* 新节点成为栈开始 */ return stack;}/* ---------------------------------------- *//* 栈资料的取出 *//* ---------------------------------------- */oplink pop_op(oplink stack,char *op){ oplink top; /* 指向栈顶端 */ if ( stack != NULL ) { top = stack; /* 指向栈顶端 */ stack = stack->next; /* 栈指标指向下节点 */ strcpy(op, top->operator1); /* 取出资料 */ free(top); /* 释回节点记忆体 */ } return stack; /* 传回栈指标 */}/* ---------------------------------------- *//* 检查栈是否是空的 *//* ---------------------------------------- */int empty_op(oplink stack){ int flag = 0; if ( stack == NULL ) /* 是否是空 */ flag = 1; /* 空的 */ return flag; }//求运算符op2在优先级表中的下标int subscript(char *op2){ for(int i=0; i<M; i++) { if(strcmp(operators[i].op, op2)==0) { return i; break; } }}//将字符串颠倒处理void reverse(char *num){ int len=strlen(num); char *ptr1,*ptr2,temp; for(ptr1=num, ptr2=ptr1+len-1; ptr1<ptr2; ptr1++, ptr2--) { temp=*ptr1; *ptr1=*ptr2; *ptr2=temp; }} double compute_postfix(char *op1);//将一个中序表达式转换为后序表达式char *infix_postfix(char *expr,double x){ char result[100],op1[10], op2[10],num[10]; result[0]=op1[0]=op2[0]=num[0]='\0'; unsigned int i=0,j=0; int sign=1; double y; while(i < strlen(expr)) { num[0]=op1[0]='\0'; if(expr[i]==' ') { i++; continue; } if(expr[i]=='-')//处理负号 { i++; sign=-1; } if(isdigit(expr[i])) { j=0; while(isdigit(expr[i]) || expr[i]=='.')//读取字符串数字串 num[j++]=expr[i++]; num[j]='\0'; } else if(expr[i]=='x' || expr[i]=='X') //处理变量 x { j=0; num[j++]=expr[i++]; num[j]='\0'; } if(isalpha(expr[i])) { j=0; while(isalpha(expr[i])) // 多个字符组成的运算符处理 op1[j++]=expr[i++]; op1[j]='\0'; } else if(!isdigit(expr[i]) || !isalpha(expr[i])) //处理单个字符构成的运算符 { j=0; op1[j++]=expr[i++]; op1[j]='\0'; } if(strlen(num)>0) //建立一个操作数栈operands { strcat(result,num); /*将操作数的数字中存入后序结果表达式result中 */ if(strcmp(num,"x")==0 || strcmp(num,"X")==0) { operands = push(operands, sign*x); } else operands = push(operands, sign*atof(num)); } if ( isoperator(op1) ) /* 是不是运算符 */ { if( empty_op(operator_stack) || strcmp(op1,"(")==0 ) //处理空栈和左括号 /* 存入运算子至栈 */ operator_stack = push_op(operator_stack,op1); else { if ( strcmp(op1 ,")")==0 || strcmp(op1 ,",")==0) /* 处理右括号或逗号 */ { while ( strcmp(operator_stack->operator1 ,"(") != 0) { /* 取出运算子直到是'(' */ operator_stack = pop_op(operator_stack,op2); y = compute_postfix(op2); strcat(result, op2); /* 将op2存入后序结果表达式 */ } /* 取出运算子'(' */ if(strcmp(op1 ,")")==0)//若是右括号,则取出左括号 operator_stack = pop_op(operator_stack , op2); } else { /* 比较优先权 */ while ( !empty_op(operator_stack) && priority(op1) <= priority(operator_stack->operator1)) { /* 取出优先级高的运算符 */ operator_stack = pop_op(operator_stack,op2); y = compute_postfix(op2); strcat(result, op2) ; /* 存入结果表达式 */ } /* 存入运算子至栈 */ operator_stack = push_op(operator_stack,op1); } }//( isoperator(op1) ) } }//while while ( !empty_op(operator_stack) ) /* 取出剩下的运算子 */ { operator_stack = pop_op(operator_stack, op2); /* 取出运算子 */ y = compute_postfix(op2); strcat(result, op2); /* 存入结果表达式 */ } result[i] = '\0'; /* 设定字符串结束 */ return result; // expr2=result;// return expr2;}/* 计算后序表达式后, 计算其值.*/double compute_postfix(char *op1){ int number; double operand1; //前运算元变数 double operand2; //后运算元变数 if ( isoperator(op1) ) /* 若是运算符 */ { number=subscript(op1);//求运算符op1在优先级表中的下标 switch(number) { case 0: operands = pop(operands,&operand1); //+ operands = pop(operands,&operand2); operands = push(operands, operand1+operand2); break; case 1: operands = pop(operands,&operand1); //- operands = pop(operands,&operand2); operands = push(operands, operand2-operand1); break; case 2: operands = pop(operands,&operand1); //* operands = pop(operands,&operand2); printf("%lf,%lf\n", operand1, operand2); operands = push(operands, operand1*operand2); break; case 3: operands = pop(operands,&operand1); /// operands = pop(operands,&operand2); if(operand1!=0) operands = push(operands, operand2/operand1); break; case 4: operands = pop(operands,&operand1); //% operands = pop(operands,&operand2); operands = push(operands, (int)(operand2)%(int)operand1); break; /*case 5: if( operator_stack ) //( operator_stack=pop_op(operator_stack, op2); //operator_stack = push_op(operator_stack,op1); break; case 6: //operator_stack=pop_op(operator_stack, op2); //) //if(strcmp(op2,"(")==0) // operator_stack=pop_op(operator_stack, op2); break;*/ case 7: operands = pop(operands,&operand1); //pow operands = pop(operands,&operand2); if(operand1>0) operands = push(operands, pow(operand2,operand1)); break; // case 8: operands = pop(operands,&operand1); //sin operands = push(operands, sin(operand1)); break; case 9: operands = pop(operands,&operand1); //cos operands = push(operands, cos(operand1)); break; case 10: operands = pop(operands,&operand1); //cosh push(operands, cosh(operand1)); break; case 11: operands = pop(operands,&operand1); //acos operands = push(operands, acos(operand1)); break; case 12: operands = pop(operands,&operand1); //asin operands = push(operands, asin(operand1)); break; case 13: operands = pop(operands,&operand1); //sinh operands = push(operands, sinh(operand1)); break; case 14: operands = pop(operands,&operand1); //exp push(operands, exp(operand1)); break; case 15: operands = pop(operands,&operand1); //log if(operand1>0) operands = push(operands, log10(operand1)); break; case 16: operands = pop(operands,&operand1); //ln push(operands, log(operand1)); break; case 17: operands = pop(operands,&operand1); //tan operands = push(operands, tan(operand1)); break; case 18: operands = pop(operands,&operand1); //atan operands = push(operands, atan(operand1)); break; case 19: operands = pop(operands,&operand1); //fabs operands = push(operands, fabs(operand1)); break; }//switch } /* 是运算子, 存入运算子栈 */ //operands = pop(operands,&operand1);//运算的最终结果 //printf("表达式[%lf %s %lf]的结果是 %lf\n",operand1, op1 , operand2 , operands->data); return operands->data;}/* ---------------------------------------- *//* 主程式: 输入中序表达式后, 计算其值. *//* ---------------------------------------- */void main(){ char exp[100]; /* 表达式字符串变数 */ char post_fix[100]; double result = 0; /* 计算结果变数 */ printf("请输入中序表达式,幂函数用pow函数表示,如x*x,表示成pow(x,2) ==>\n "); gets(exp); /* 读取中序表达式 */ puts(exp); double x=4; strcpy(post_fix, infix_postfix(exp,x));// infix_postfix(exp,post_fix,x); printf("%s\n",post_fix); operands=pop(operands, &result); /* 取出结果 */ printf("表达式[%s]的结果是 %lf\n",exp,result);} 见鬼了,奇怪的键盘钩子问题!!!!!!!!! 这就是传说中的CSDN? 关于加密算法,用MD5函数加密。 请问ODBC+ACCESS的问题 高分相送:如何显式调用DLL导出类 问个简单的问题关于CREATE对话框的问题 在WH_JOURNALRECORD钩子中能得到汉字和鼠标的坐标吗? 怎样点击按钮后打开一个html 关于用CRecordset,为什么添加不了记录? 高分请教2个问题 几个简单的问题?请说的详细些要不我看不懂. VC++ 一年工作到底是啥水平
程序比较小,我就贴这里好了,希望对你有用.
/* ======================================== */
/* 程式实例: 函数计算处理.cpp */
/* 计算中序形式的任意函数表达式的值 */
/* ======================================== */
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#define M 21
#define N 6
struct prioprities { //创建优先级表结构
char op[6];
int prioprity1;
};
//运算符与优先级的对应表
struct prioprities operators[M]={"+",1,"-",1,"*",2,"/",2,"%",2, //5
"(",0,")",0,"pow",3,"sin",3,"cos",3,"cosh",3,"acos",3, //7
"asin",3,"sinh",3,"exp",3,"log",3,"ln",3,"tan",3,"atan",3,//7
"fabs",3,",",0};
struct stack_node /* 栈的结构宣告 */
{
double data; /* 栈资料 */
struct stack_node *next; /* 指向下一节点 */
};
typedef struct stack_node stack_list; /* 串列新型态 */
typedef stack_list *link; /* 串列指标新型态 */link operands = NULL; /* 运算元栈指标 */
/* ---------------------------------------- */
/* 栈资料的存入 */
/* ---------------------------------------- */
link push(link stack,double value)
{
link new_node; /* 新节点指标 */ /* 配置节点记忆体 */
new_node = ( link ) malloc(sizeof(stack_list));
if ( !new_node )
{
printf("记忆体配置失败! \n");
return NULL; /* 存入失败 */
}
new_node->data = value; /* 建立节点内容 */
new_node->next = stack; /* 新节点指向原开始 */
stack = new_node; /* 新节点成为栈开始 */
return stack;
}/* ---------------------------------------- */
/* 栈资料的取出 */
/* ---------------------------------------- */
link pop(link stack,double *value)
{
link top; /* 指向栈顶端 */ if ( stack != NULL )
{
top = stack; /* 指向栈顶端 */
stack = stack->next; /* 栈指标指向下节点 */
*value = top->data; /* 取出资料 */
free(top); /* 释回节点记忆体 */
}
else
{
printf("数据栈为空,操作失败!\n");
exit(1);
*value = -1;
}
return stack; /* 传回栈指标 */
}/* ---------------------------------------- */
/* 检查栈是否是空的 */
/* ---------------------------------------- */
int empty(link stack)
{
int flag=0;
if ( stack == NULL ) /* 是否是空 */
flag=1; /* 空的 */
return flag; /* 不是空的 */
}/* ---------------------------------------- */
/* 是否是运算子 */
/* ---------------------------------------- */
int isoperator(char *op1)
{ int flag=0;
for(int i=0; i<M; i++)
if(strcmp(operators[i].op, op1)==0)
{
flag=1;
break;
}
return flag;
}
/* ---------------------------------------- */
/* 运算子的优先权 */
/* ---------------------------------------- */
int priority(char *op1)
{
int i;
for(i=0; i<M; i++)
if(strcmp(operators[i].op, op1)==0)
return operators[i].prioprity1;
}
struct stack_operator /* 栈的结构宣告 */
{
char operator1[N]; /* 栈资料 */
struct stack_operator *next; /* 指向下一节点 */
};
//运算符栈的与操作
typedef struct stack_operator stack_op; /* 串列新型态 */
typedef stack_op *oplink; /* 串列指标新型态 */
oplink operator_stack = NULL;
/* ---------------------------------------- */
/* 栈资料的存入 */
/* ---------------------------------------- */
oplink push_op(oplink stack,char *op)
{
oplink new_node; /* 新节点指标 */ /* 配置节点记忆体 */
new_node = ( oplink ) malloc(sizeof(stack_op));
if ( !new_node )
{
printf("记忆体配置失败! \n");
return NULL; /* 存入失败 */
}
strcpy(new_node->operator1,op); /* 建立节点内容 */
new_node->next = stack; /* 新节点指向原开始 */
stack = new_node; /* 新节点成为栈开始 */
return stack;
}/* ---------------------------------------- */
/* 栈资料的取出 */
/* ---------------------------------------- */
oplink pop_op(oplink stack,char *op)
{
oplink top; /* 指向栈顶端 */ if ( stack != NULL )
{
top = stack; /* 指向栈顶端 */
stack = stack->next; /* 栈指标指向下节点 */
strcpy(op, top->operator1); /* 取出资料 */
free(top); /* 释回节点记忆体 */
}
return stack; /* 传回栈指标 */
}/* ---------------------------------------- */
/* 检查栈是否是空的 */
/* ---------------------------------------- */
int empty_op(oplink stack)
{
int flag = 0;
if ( stack == NULL ) /* 是否是空 */
flag = 1; /* 空的 */
return flag;
}
//求运算符op2在优先级表中的下标
int subscript(char *op2)
{
for(int i=0; i<M; i++)
{
if(strcmp(operators[i].op, op2)==0)
{
return i;
break;
}
}
}
//将字符串颠倒处理
void reverse(char *num)
{
int len=strlen(num);
char *ptr1,*ptr2,temp;
for(ptr1=num, ptr2=ptr1+len-1; ptr1<ptr2; ptr1++, ptr2--)
{
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
}
}
double compute_postfix(char *op1);//将一个中序表达式转换为后序表达式
char *infix_postfix(char *expr,double x)
{
char result[100],op1[10], op2[10],num[10];
result[0]=op1[0]=op2[0]=num[0]='\0';
unsigned int i=0,j=0;
int sign=1;
double y;
while(i < strlen(expr))
{
num[0]=op1[0]='\0';
if(expr[i]==' ')
{
i++;
continue;
}
if(expr[i]=='-')//处理负号
{
i++;
sign=-1;
}
if(isdigit(expr[i]))
{
j=0;
while(isdigit(expr[i]) || expr[i]=='.')//读取字符串数字串
num[j++]=expr[i++];
num[j]='\0';
}
else if(expr[i]=='x' || expr[i]=='X') //处理变量 x
{
j=0;
num[j++]=expr[i++];
num[j]='\0';
}
if(isalpha(expr[i]))
{
j=0;
while(isalpha(expr[i])) // 多个字符组成的运算符处理
op1[j++]=expr[i++];
op1[j]='\0';
}
else if(!isdigit(expr[i]) || !isalpha(expr[i])) //处理单个字符构成的运算符
{
j=0;
op1[j++]=expr[i++];
op1[j]='\0';
}
if(strlen(num)>0) //建立一个操作数栈operands
{
strcat(result,num); /*将操作数的数字中存入后序结果表达式result中 */
if(strcmp(num,"x")==0 || strcmp(num,"X")==0)
{
operands = push(operands, sign*x);
}
else
operands = push(operands, sign*atof(num));
}
if ( isoperator(op1) ) /* 是不是运算符 */
{
if( empty_op(operator_stack) || strcmp(op1,"(")==0 ) //处理空栈和左括号
/* 存入运算子至栈 */
operator_stack = push_op(operator_stack,op1);
else
{
if ( strcmp(op1 ,")")==0 || strcmp(op1 ,",")==0) /* 处理右括号或逗号 */
{
while ( strcmp(operator_stack->operator1 ,"(") != 0)
{
/* 取出运算子直到是'(' */
operator_stack = pop_op(operator_stack,op2);
y = compute_postfix(op2); strcat(result, op2); /* 将op2存入后序结果表达式 */
}
/* 取出运算子'(' */
if(strcmp(op1 ,")")==0)//若是右括号,则取出左括号
operator_stack = pop_op(operator_stack , op2);
}
else
{
/* 比较优先权 */
while ( !empty_op(operator_stack) &&
priority(op1) <= priority(operator_stack->operator1))
{
/* 取出优先级高的运算符 */
operator_stack = pop_op(operator_stack,op2);
y = compute_postfix(op2);
strcat(result, op2) ; /* 存入结果表达式 */
}
/* 存入运算子至栈 */
operator_stack = push_op(operator_stack,op1);
} }//( isoperator(op1) )
}
}//while
while ( !empty_op(operator_stack) ) /* 取出剩下的运算子 */
{
operator_stack = pop_op(operator_stack, op2); /* 取出运算子 */
y = compute_postfix(op2);
strcat(result, op2); /* 存入结果表达式 */
}
result[i] = '\0'; /* 设定字符串结束 */
return result;
// expr2=result;
// return expr2;
}/* 计算后序表达式后, 计算其值.*/
double compute_postfix(char *op1)
{
int number;
double operand1; //前运算元变数
double operand2; //后运算元变数
if ( isoperator(op1) ) /* 若是运算符 */
{
number=subscript(op1);//求运算符op1在优先级表中的下标
switch(number)
{
case 0: operands = pop(operands,&operand1); //+
operands = pop(operands,&operand2);
operands = push(operands, operand1+operand2);
break;
case 1: operands = pop(operands,&operand1); //-
operands = pop(operands,&operand2);
operands = push(operands, operand2-operand1);
break;
case 2: operands = pop(operands,&operand1); //*
operands = pop(operands,&operand2);
printf("%lf,%lf\n", operand1, operand2);
operands = push(operands, operand1*operand2);
break;
case 3: operands = pop(operands,&operand1); ///
operands = pop(operands,&operand2);
if(operand1!=0)
operands = push(operands, operand2/operand1);
break;
case 4: operands = pop(operands,&operand1); //%
operands = pop(operands,&operand2);
operands = push(operands, (int)(operand2)%(int)operand1);
break;
/*case 5: if( operator_stack ) //(
operator_stack=pop_op(operator_stack, op2);
//operator_stack = push_op(operator_stack,op1);
break;
case 6: //operator_stack=pop_op(operator_stack, op2); //)
//if(strcmp(op2,"(")==0)
// operator_stack=pop_op(operator_stack, op2);
break;*/
case 7: operands = pop(operands,&operand1); //pow
operands = pop(operands,&operand2);
if(operand1>0)
operands = push(operands, pow(operand2,operand1));
break; //
case 8: operands = pop(operands,&operand1); //sin
operands = push(operands, sin(operand1));
break;
case 9: operands = pop(operands,&operand1); //cos
operands = push(operands, cos(operand1));
break;
case 10: operands = pop(operands,&operand1); //cosh
push(operands, cosh(operand1));
break;
case 11: operands = pop(operands,&operand1); //acos
operands = push(operands, acos(operand1));
break;
case 12: operands = pop(operands,&operand1); //asin
operands = push(operands, asin(operand1));
break;
case 13: operands = pop(operands,&operand1); //sinh
operands = push(operands, sinh(operand1));
break;
case 14: operands = pop(operands,&operand1); //exp
push(operands, exp(operand1));
break;
case 15: operands = pop(operands,&operand1); //log
if(operand1>0)
operands = push(operands, log10(operand1));
break;
case 16: operands = pop(operands,&operand1); //ln
push(operands, log(operand1));
break;
case 17: operands = pop(operands,&operand1); //tan
operands = push(operands, tan(operand1));
break;
case 18: operands = pop(operands,&operand1); //atan
operands = push(operands, atan(operand1));
break;
case 19: operands = pop(operands,&operand1); //fabs
operands = push(operands, fabs(operand1));
break;
}//switch
} /* 是运算子, 存入运算子栈 */
//operands = pop(operands,&operand1);//运算的最终结果
//printf("表达式[%lf %s %lf]的结果是 %lf\n",operand1, op1 , operand2 , operands->data); return operands->data;
}/* ---------------------------------------- */
/* 主程式: 输入中序表达式后, 计算其值. */
/* ---------------------------------------- */
void main()
{
char exp[100]; /* 表达式字符串变数 */
char post_fix[100];
double result = 0; /* 计算结果变数 */
printf("请输入中序表达式,幂函数用pow函数表示,如x*x,表示成pow(x,2) ==>\n ");
gets(exp); /* 读取中序表达式 */
puts(exp);
double x=4;
strcpy(post_fix, infix_postfix(exp,x));
// infix_postfix(exp,post_fix,x);
printf("%s\n",post_fix);
operands=pop(operands, &result);
/* 取出结果 */
printf("表达式[%s]的结果是 %lf\n",exp,result);
}