算术表达式求值 实训(3)
来源:互联网 发布:育知同创教育 编辑:程序博客网 时间:2024/06/03 18:12
/*一个算术表达式是由操作数(operand)、运算符(operator)和界限符(delimiter)组成的。
假设操作数是正整数,运算符只含+、-、*、/等四种运算符,界限符有左右圆括号和表达式结束符"#",
如:(7+15)*(23-28/4)#。编程按照算术运算规则,求算术表达式的值*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct stack_1
{
doubledata;
structstack_1 *next;
}Operand, *pOperand; //操作数栈,栈1;
typedef struct stack_2
{
charch;
structstack_2 *next;
}Deliniter, *pDeliniter; //运算符与界限符栈,栈2;
pOperand push_1(pOperand datastack, doubledata) //操作数进栈;
{
pOperandmiddle;
middle= (pOperand)malloc(sizeof(Operand));
middle->data= data;
middle->next= datastack;
datastack= middle;
returndatastack;
}
double operate(double oper1, char theta,double oper2) //二元运算;
{
doubleresult;
switch(theta)
{
case'+':
{
result= oper1 + oper2;
}
break;
case'-':
{
result= oper1 - oper2;
}
break;
case'*':
{
result= oper1 * oper2;
}
break;
case'/':
{
result= oper1 / oper2;
}
break;
}
returnresult;
}
char expression(char str[], double*data) //表达式处理函数;
{
staticint i;
if('0'<=str[i] && '9'>=str[i])
{
intnum;
num= str[i] - '0';
for(++i; i<49; ++i)
{
if('0'<=str[i] && '9'>=str[i])
{
num= num*10 + (str[i]- '0');
}
else
{
*data = (double)num;
returnNULL;
}
}
}
else
{
charch;
ch= str[i];
++i;
returnch;
}
}
pDeliniter push_2(pDeliniter charstack,char ch) //运算符与界限符进栈;
{
pDelinitermiddle;
middle= (pDeliniter)malloc(sizeof(Deliniter));
middle->ch= ch;
middle->next= charstack;
charstack= middle;
returncharstack;
}
double pop_1(pOperand datastack) //操作数出栈;
{ //c语言中没有引用传递,指针的实质也只是在同一内存单元操作,当改变栈顶指针时,调用函数中不改变;
doubledata;
data= datastack->data;
pOperandmiddle;
pOperandp;
middle= datastack;
p= datastack;
while(NULL!=middle->next)
{
middle->data= middle->next->data;
p= middle;
middle= middle->next;
}
p->next= NULL;
free(middle);
returndata;
}
char pop_2(pDeliniter charstack) //运算符与界限符出栈;
{
charch;
ch= charstack->ch;
pDelinitermiddle;
pDeliniterp;
middle= charstack;
p= charstack;
while(NULL!=middle->next)
{
middle->ch= middle->next->ch;
p= middle;
middle= middle->next;
}
p->next= NULL;
free(middle);
returnch;
}
char precede(char ch1, char ch2) //比较运算符的优先级;
{
if(('+'==ch1 || '-'==ch1) && ('+'==ch2 || '-'==ch2 || ')'==ch2 ||'#'==ch2))
{
return'>';
}
elseif (('+'==ch1 || '-'==ch1) && ('*'==ch2 || '/'==ch2 || '('==ch2))
{
return'<';
}
elseif ('*'==ch1 || '/'==ch1 || ')'==ch1)
{
if(')'==ch1 && '('==ch2)
{
returnNULL;
}
if(('*'==ch1 || '/'==ch1) && '('==ch2)
{
return'<';
}
return'>';
}
else
{
if(('('==ch1 && '#'==ch2) || ('#'==ch1 && ')'==ch2))
{
returnNULL;
}
if(('('==ch1 && ')'==ch2) || ('#'==ch1 && '#'==ch2))
{
return'=';
}
return'<';
}
}
int main(void)
{
pOperanddatastack;
datastack= (pOperand)malloc(sizeof(Operand));
datastack->next= NULL; //新建操作数栈1;
pDelinitercharstack;
charstack= (pDeliniter)malloc(sizeof(Deliniter));
charstack->ch= '#';
charstack->next= NULL; //新建运算符与界限符栈2;
charstr[50]; //表达式最高为49位;
chartheta; //运算符;
doubleoper1,oper2;
doubleresult; //运算结果;
double data;
double*pdata = &data;
charch;
charch1;
printf("请输入运算表达式,以#键结束!\n");
scanf("%s",str);
ch = expression(str, pdata);
while('#'!= ch || '#'!=charstack->ch)
{
if('+'!=ch && '-'!=ch && '*'!=ch && '/'!=ch &&'('!=ch && ')'!=ch && '#'!=ch)
{
datastack= push_1(datastack, data);
ch= expression(str, pdata);
}
else
{
ch1 = precede(charstack->ch, ch); //比较charstack的栈顶与ch的优先权;
switch(ch1)
{
case'<':
{
charstack= push_2(charstack, ch);
ch= expression(str, pdata);
}
break;
case'>':
{
theta= pop_2(charstack); //字符出栈;
oper2= pop_1(datastack); //操作数出栈;
oper1= pop_1(datastack);
result= operate(oper1, theta, oper2);
datastack= push_1(datastack, result); //oper1与oper2的运算结果进栈;
}
break;
case'=':
{
pop_2(charstack); //左括弧出栈,不接收;
ch= expression(str, pdata);
}
break;
}
}
}
printf("表达式的值为:%.2lf\n",datastack->data );
return0;
}
- 算术表达式求值 实训(3)
- 算术表达式求值
- C#算术表达式求值
- 算术表达式求值
- 字符串算术表达式求值
- 算术表达式求值
- 算术表达式求值
- 图解--算术表达式求值
- 算术表达式求值(Dijkstra)
- 简单算术表达式求值
- 算术表达式求值
- 算术表达式求值
- 算术表达式求值
- 简单算术表达式求值
- 算术表达式求值过程
- 简单算术表达式求值
- 【练习】算术表达式求值
- 算术表达式求值
- linux 下查看分区情况
- iphone的自动旋转和自动调节大小
- 转载:提高firefox扩展开发效率
- 碎碎
- 猴子选大王 实训(2)
- 算术表达式求值 实训(3)
- 真机缺少com.google.android.maps.jar
- 员工工资管理(综合训练) (实训4)
- OGNL表达式struts2标签“%,#,$”
- 不动点迭代求解方程数值解
- 难死人不偿命!苹果8大笔试题及答案
- c语言暂替控制台返回,查看结果
- 课本的T_SQL语言
- IBM面试记