python作业表达式求值_数据结构作业之表达式求值

#include#include#includeusingnamespacestd;#defineMax_Stack_Size 1000constcharIsFirst[7][7]={

{'>','>','','>'},

{'>','>','','>'},

{'>','>','>','>','','>'},

{'>','>','>','>','','>'},

{'

{'>','>','>','>','>','>','>'},

{'

} ;structStackT1{charStack[Max_Stack_Size];intTop;voidClearStack(){ Top=1; }charGetTop(){returnStack[Top-1]; }voidPush(charc){ Stack[Top++]=c; }voidPop(char&c){ c=Stack[--Top]; }

} OPTR;//运算符栈structStackT2{intStack[Max_Stack_Size];intTop;voidClearStack(){ Top=1; }intGetTop(){returnStack[Top-1]; }voidPush(intc){ Stack[Top++]=c; }voidPop(int&c){ c=Stack[--Top]; }

} OPND;//操作数栈boolIsNum(charc){//cout <='0'&&c<='9');

}intOperate(inta,charOP,intb){intc;//cout <

}returnc;

}intChange(charc){intret;switch(c) {case'+': ret=0;break;case'-': ret=1;break;case'*': ret=2;break;case'/': ret=3;break;case'(': ret=4;break;case')': ret=5;break;case'#': ret=6;break;

}returnret;

}charPrecede(chara,charb){//cout <

}voidEvaluateExpression(void){

OPTR.ClearStack(), OPND.ClearStack();

OPTR.Push('#');charChNow=getchar();while(ChNow!='#'||OPTR.GetTop()!='#'){if(IsNum(ChNow)) {intNowNum=ChNow-'0';

ChNow=getchar();while(IsNum(ChNow)){

NowNum=ChNow-'0'+NowNum*10;

ChNow=getchar();

}

OPND.Push(NowNum);/*for (int i = 0; i 

cout <

cout <

OPTR.Push(ChNow); ChNow=getchar();break;case'=':charx; OPTR.Pop(x); ChNow=getchar();break;case'>':chartheta; OPTR.Pop(theta);inta, b; OPND.Pop(b); OPND.Pop(a);

OPND.Push(Operate(a, theta, b));break;

}

}

}

cout<

}intmain(){

EvaluateExpression();return0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个算术表达式求值的演示代码,使用数据结构中的栈来实现: ```python class Stack: def __init__(self): self.items = [] def is_empty(self): return len(self.items) == 0 def push(self, item): self.items.append(item) def pop(self): if not self.is_empty(): return self.items.pop() def peek(self): if not self.is_empty(): return self.items[-1] def size(self): return len(self.items) def evaluate_expression(expression): operators = {'+': 1, '-': 1, '*': 2, '/': 2} operand_stack = Stack() operator_stack = Stack() for char in expression: if char.isdigit(): operand_stack.push(int(char)) elif char in operators: while (not operator_stack.is_empty() and operators[char] <= operators[operator_stack.peek()]): operand2 = operand_stack.pop() operand1 = operand_stack.pop() operator = operator_stack.pop() result = perform_operation(operand1, operand2, operator) operand_stack.push(result) operator_stack.push(char) while not operator_stack.is_empty(): operand2 = operand_stack.pop() operand1 = operand_stack.pop() operator = operator_stack.pop() result = perform_operation(operand1, operand2, operator) operand_stack.push(result) return operand_stack.pop() def perform_operation(operand1, operand2, operator): if operator == '+': return operand1 + operand2 elif operator == '-': return operand1 - operand2 elif operator == '*': return operand1 * operand2 elif operator == '/': return operand1 / operand2 expression = "3+4*2-6/3" result = evaluate_expression(expression) print("Result:", result) # 输出:7 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值