Data Structure Course Design - Project 3 - Arithmetic Expression Solving (Experiment Preparation)


1. Problem description

Design a simple arithmetic expression calculator.

2. Basic requirements

Realize the evaluation of four arithmetic expressions of standard integer types (including parentheses, which can be embedded in multiple layers).

3. Problem analysis

【Functional Analysis】

To translate an expression into a correctly evaluated sequence of machine instructions, or to evaluate an expression directly, the expression must first be interpreted correctly.
Expressions should be evaluated according to the following four arithmetic operations:
(1) multiplication and division first, then addition and subtraction
(2) calculation from left to right
(3) inside the brackets first, then outside the brackets

【Basic idea】

Since the arithmetic expression input by the user is an infix expression, it is not conducive to the automatic solution of the machine. And we know that in applications such as syntax scanning analysis, we generally use suffix expressions (reverse Polish) to do operations. To evaluate the reverse Polish expression, it only needs to scan from left to right, and when an operator is encountered, the two operands on the left will be calculated until the end of the whole expression scan.

[Selection of data structure]

We divide the characters in arithmetic expressions into operands and operators. Operators include arithmetic operators {'+', '-', '*', '/'} and arithmetic delimiters {left bracket '(', Right bracket ')', delimiter '=', '#'}. We can create two stack structures, the operand stack s and the operator stack chs, the operand stack s is used to store operands, and the operator stack chs is used to store operators.

4. Logic Design

【Module division】

1. Two operand calculation module
2. In-stack operator priority module
3. Out-of-stack operator priority module
4. Infix expression reverse Polish form module
5. Calculate reverse Polish form module

【Abstract data structure ADT】

ADT	算数表达式求解
	Data
	  操作数栈s
	  操作符栈chs
	Operation
	  操作数栈顶的两个元素的运算
	    输入:运算符oper
	    功能:将操作数栈顶的两个元素取出做oper运算并重新压入操作数栈中
	    输出:无
	   栈内运算符优先级赋值
	     输入:栈外运算符
	     功能:为栈外运算符优先级赋值
	     输出:该运算符的优先级
	   栈内运算符优先级赋值
	     输入:栈内运算符
	     功能:为栈内运算符优先级赋值
	     输出:该运算符的优先级
	  中缀表达式转后缀表达式
	    输入:中缀表达书
	    功能,将中缀表达式转换为逆波兰式并将操作符相应地压入操作符栈
	    输出:后缀表达式
	  计算后缀表达式
	    输入:后缀表达式
	    功能:通过后缀表达式计算出算数表达式的结果
	    输出:算数表达式的求解结果
endADT 

5. Physical Design

【Storage structure】

1. Open up two stack structures to store operands and operators respectively
2. Use a txt file to store infix expressions and postfix expressions for easy writing and reading

【Function Design】

1. Operation design of the two operands on the top of the stack

伪代码:
1.弹出操作数栈的栈顶两个元素
2.将第一个弹出的元素作为右操作数,第二个弹出的元素作为左操作数
3.根据参数给定的运算符对两个操作数进行运算并压入操作数栈中

2. In-stack operator priority assignment design

//求操作符栈内栈顶元素的优先级
int Calculator::inp(char ch) {
    
    
    switch (ch) {
    
    
        case'(': return 1;
        case'*': return 3;
        case'/': return 3;
        case'+': return 2;
        case'-': return 2;
        case'#': return 0;
        default: cout << "有不可识别的运算符 !" << endl;
            exit(0);
    }      
}

3. Off-stack operator priority assignment design

//求栈外(当前扫描到的操作符)的优先级
int Calculator::outp(char ch) {
    
    
    switch (ch) {
    
    
        case'(': return 5; break;
        case'*': return 3; break;
        case'/': return 3; break;
        case'+': return 2; break;
        case'-': return 2; break;
        case'#': return 0; break;
        default: cout << "有不可识别的运算符!" << endl;
            exit(0);
    }
}

4. Infix expressions reverse Polish design

伪代码:
1.通过键盘一个字符一个字符地读取输入的中缀表达式并以等号结束输入
2.若读到数值,将该数值放回,重新用double变量读取整体数值并写入文件(数值写入时候用"|"分隔)
3.若遇右括号,则将运算符栈中左括号后压入的运算符弹出并写入文件
4.若遇左括号,判断括号内是否为负数
5.对于其余运算符,若当前扫面的运算符优先级大于栈顶运算符优先级,则将压入栈顶。
  若小于栈顶运算符优先级,则将大于栈外运算符优先级的栈内元素全部弹出写入文件。
6.读取完毕后,将操作符栈中的运算符全部弹出写入文件

5. Arithmetic expression design for solving reverse Polish equation

伪代码:
1.从文件中读取逆波兰式(以#为结束符)
2.当遇到分隔符'|'时,读取一个doule,并将其压入操作数栈中
3.当遇到加减乘除的运算符,则调用栈顶两个操作数运算函数求解

【Function Algorithm Framework】

Function call diagram

6. Test data

(30+2* 70)/3-12* 3=
5+(9*(62-37)+15)*6=
It is required to design illegal expressions and test the program to ensure the stable operation of the program.

Guess you like

Origin blog.csdn.net/m0_59056822/article/details/124864352