20184313 Experiment 2 "Python Programming" Experiment Report

20184313 2019-2020-2 "Python Programming" Experiment 2 Report

Course: "Python Programming"
Class: 1843
Name: Sun Weili
Student ID: 20184313
Experimental Teacher: Wang Zhiqiang
Experiment Date: April 12, 2020
Required / Elective: Public Elective

1. Experimental content

  • Design and complete a complete application program, complete operations such as addition, subtraction, multiplication and division, and more functions.
  • Examine the knowledge points of basic grammar, judgment statements, loop statements, logical operations, etc.

2. Experimental process and results

  • Program framework
    flow chart

  • ZhuanHuan function

    This function is mainly used to convert mid-order expressions to post-order expressions, referring to the practice in the data structure course of the last semester:

    • When the operand is read, it is directly placed in the output; (considering the floating point number, the string is spliced ​​first, and then converted into a floating point variable after the reading is completed)
    • If the read operator is "(" or the stack is empty, it is directly pushed onto the stack;
    • When the operator is read, it is not directly placed in the output, it must be stored somewhere (in fact, the stack), and then the priority is compared: if the priority of the operator is higher than the top of the stack, Then it is put on the stack; if the priority of the operator is not higher than the top of the stack, then the operator on the top of the stack is out of the stack, and then the priority of the current stack top operator is compared;
    • If the read operator is ")", then pop the operator from the stack and write the output until it encounters "(", "(" and ")" are not written to the output;
    • When the input is empty, all the operators in the stack are popped and written to the output
  • JiSuanHouXu function

    The main function of this function is to find the corresponding operands and operators from the post-order sequence, first take the last bit as the operator, then determine the number of operands according to the type of operator, take the last operand, and call JiSuan Function for operation

  • JiSuan function

    The purpose of this function is to perform operations based on the input operands and operators. It is relatively simple, and is mainly implemented with some if statements

  • operation result

    Test case:

    1-2!*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568%14))-(-4^3)/(16-3*2))
    

    operation result

    Code Cloud Link

3. The problems encountered during the experiment and the resolution process

  • Question 1: When deleting used operands and operators in the JiSuanHouXu (HouXu) function, one less operand is deleted when the operation result is the same as operand 2
  • Problem 1 Solution: Before, the variable a was used to save the operand, and the while loop was used to compare whether the last bit of the post-order expression was a to delete it. 30) Therefore, one less operand is deleted, which affects the result of the operation, so the operators with different numbers of operands are operated separately to avoid the problem
  • Question 2: When dealing with negative numbers, the beginning is to add 0.0 to the post-order expression and put the-sign on the stack, but if the symbol is in the first item of the mid-order expression (that is, without brackets) and the operator precedence after If it is higher than the minus sign, the result will be wrong. -2% 3 will be calculated as -2
  • Solution to Problem 2: Since the decimal point will not be pushed onto the stack as an operator, replace the '-' with '.' And set its priority to the highest, if the element thrown is'. 'Yes then convert to'-'
  • Question 3: The priority of the operator should be the same level of '+', '-', the same level of '*', '/', but for the convenience of calculation, the priority in my program is addition <subtraction <multiplication <division
  • Solution to Problem 3: There is nothing wrong with the result of this, because both addition and multiplication have a combination law, but if purely considering the conversion of middle-order expressions to later-order expressions, the results are inconsistent. Such as 3 + 2-1, it is customary to calculate 3 + 2 first, then 5-1, but because only one sequence is used, my program calculates 2-1 + 3, but due to the law of addition combination, the result is correct

Others (sentiment, thinking, etc.)

Through this experiment, I reviewed the knowledge of the data structure of the last semester, but also let me have a deeper understanding of the advantages of Python over C. In C language, the simplest way to implement the stack is linear Storage structure, which requires that the data type of the elements stored in the stack must be clarified in advance, and the maximum capacity must also be considered, and methods cannot be defined in the structure, and Python sequences can ignore the length and data types, and can Define the method of stacking and stacking in the class to facilitate porting.

In short, this experiment allowed me to revisit some of the programming ideas I learned before, but also gave me a better understanding of Python and a deeper understanding of some advantages of Python over C.

Reference c

- "Data Structure (2nd Edition)"

- "Zero Basic Python (Full Color Edition)"

Guess you like

Origin www.cnblogs.com/swldeblog/p/12737103.html