Personal Project--the primary student of calculator

Source: Internet
Author: User

I. Project time planning and practical usage

PSP2.1

Personal Software Process Stages

Estimated time/h

Actual Time/h

Planning

Plan

· Estimate

· Estimate how long this task will take

10

10

Development

Development

· Analysis

· Demand analysis (including learning new technologies)

2

2

· Design Spec

· Creating a design Document

0.5

0.5

· Design Review

· Design Review (and colleagues review design documents)

0.5

0.5

· Coding Standard

· Code specification (to develop appropriate specifications for current development)

0.2

0.2

· Design

· Specific design

1

2

· Coding

· Specific code

4

8

· Code Review

· Code review

3

1

· Test

· Test (self-test, modify code, commit changes)

2

5

Reporting

Report

· Test Report

· Test report

1

1.5

· Size Measurement

· Computational effort

0.2

0.2

· Postmortem & Process Improvement Plan

· Summarize afterwards and propose process improvement plan

1

1

Total

14.9

21.4

This writing program is a bit of a hole in the C + + syntax and its memory recovery mechanism is not deep, and due to the VS2012 compiler over-indulgence, such as an array of cross-border will not error, curly braces do not match the error and so on, resulting in the specific code to spend a lot of time in the search data and debugging. Fortunately, this time I write the code very carefully understand and analyze the project requirements, the requirements of every point taken into account, the establishment of the correct specific design ideas, so that in the process of writing code has not changed, has been in accordance with the original design to write, and finally finished after writing can also be very good to run. It also helps to optimize the code later and improve the performance.

  Two. Improve program performance

I took about 3 hours to improve the program, including design optimization ideas, code changes, program debugging. The proportion of the time that debugging takes the most.

The whole idea of the program: generate a unique infix expression by randomly generating a prefix expression, and evaluate the value of the expression. The operands of the expression, operators, and the number of operators are randomly generated, followed by all operators in front of the string, followed by all the operands to splice the resulting prefix expression, using the hash value of the expression to sift out the duplicate expression, in which, once a non-conforming expression will regenerate the new expression.

My optimization thinking mainly has the following points:

1. Improved stochastic algorithm: one of the most expensive features of random algorithm generation arithmetic is the generation of a non-conforming rule expression and then rebuilding. In fact, we can create a modification strategy for those expressions that do not conform to the rules, making it a legitimate expression and avoiding a waste of time to regenerate. For example: For the calculation process negative expression, you can change the minus sign to other operators, for the occurrence of the case except 0, randomly produce a positive number to replace it;

2. Optimization of the weighing operation: for the point of weighing, I started with the idea of writing the corresponding function to detect the repetition of the expression, but it was expensive in both space and time. In this, I think of a point, we directly generate no repetition of the expression, so as to avoid the cumbersome check and re-operation. My practice is to enforce that the operators of the generated expressions must be sorted in descending order, while using the hash value of the expression to remove the exact same expression, because the operands in the expression are ordered, so the program does not produce both 3+2+1,1+3+2 expressions, and only generates 3+2+ 1, this can completely exclude the case of duplicate expression.

While this allows some of the original correct expressions to be generated, in the test, the R size is 3 o'clock, it is possible to generate 10000 expressions, which is enough to meet the project requirements.

3. In the performance analysis process, when the value of R increases (>30), the value of the calculation expression is very time-consuming, mainly because the number of bits of the numerator denominator is too large, in which the function of finding the maximum common factor gcd () takes a lot of time, so the code structure of the GCD () function is emphatically optimized. When the value of n increases (>10000), I find that the most time-consuming aspect is the process of converting the resulting expression to a string type and outputting it. At first I was using StringStream to convert int to string, but it was time-consuming, so I rewrote the correlation function to convert int to a string array, although the number of lines of code increased, but the efficiency was improved.

It can be found that when the values of N and R are increasing, the real time-consuming function in the program is not the core code used to generate the random expression, but the related code of the type conversion, so we should not only consider the optimization of the core algorithm when optimizing the code, but also consider whether the data structure is reasonable and convenient for the operation output. This is a revelation of performance analysis to me.

Next performance Analysis Diagram (command line input is *.exe-n 10000-r 10)

the most expensive function in the program

  

Three. Bugs found

Here's a record of the tricky bugs I've found:

1.vs2012 in the C + + compilation process, for array out of bounds do not error problem:

This problem I did not realize in the early morning, when the whole program is finished debugging, the program runs normally but at the end of the run will always crash off. Later realized that the array is out of bounds, but the 500 line of code to find the array out of bounds is indeed quite troublesome.

Problem with 2.int integer overflow:

Since the presence of fractions may lead to a large numerator denominator in the operation, I calculate that an int integer overflow can occur when the value of R is 15 o'clock. To do this, I changed int to unsigned _int64, which can support the case of value 200 to R. If the value of R is larger, the program will call the police!!

four. Test Cases

1.-N 10-r 1

Program output error message, because the program specified R value range is (0,200]

2.-n 1000-r 1

Program output error message, because the value range of 1 can generate fewer than 1000 expressions

3.-n 20000-r 10

Program output 20000-channel expression, the operand range is [0,10]

4.-n 1000-r 100

The program outputs 1000 expressions and the operand range is [0,100]

5.-n 10000-r 100

Program output 10000-channel expression, operand range [0,100]

6.-n 1000-r 300

The range of R exceeds the upper bound of the program setting and the program output error message

7.-E exercises.txt-a Answers.txt

There are no wrong answers in the TXT test

8.-E exercises.txt-a Answers.txt

There are 10 wrong answers in the test txtx

9.-E exercises.txt-a Answers.txt

The corresponding TXT does not exist, the program output prompt message "Please input correct parameters!! ”

10.-E exercises.txt-a Answers.txt

Two txt length mismatch, the program will be a short TXT file to be judged by the wrong

Program Correctness Proof:

The main algorithm: generate the prefix sequence and convert it to the corresponding infix sequence expression. The resulting prefix expression follows such a rule that the prefix expression operator is preceded, the operands are preceded, and the resulting operands are sorted in descending order and the resulting operands cannot be exactly the same. This ensures that there is no exchange repetition, because the same expression is duplicated and the order of the operands is inevitably broken, which is not present in the program. In addition, the hash value of each expression is stored and the same expression is removed by judging the hash value. This ensures that the program does not have Exchange duplicates or generates duplicate expressions.

Five. Personal summary

1. For the first time, the various stages of software project development are practiced, although it is no different from the usual writing procedure, but the design requirement analysis stage is really very important, avoids the code repetition, the idea midway changes and so on.

2. Preliminary knowledge of C + + related grammar, including the use of common functions, such as Stringstream,ofstream,ifstream,map, and the C + + memory management and recovery mechanisms have a certain understanding. Programming on VS2012 for the first time, although there are lots of pits, it's good to be familiar with it, and its performance analysis tools are really helpful for improving program performance and program performance optimizations.

3. Correct their own optimization habits, for the program optimization I am accustomed to the core algorithm optimization, such as my main focus is to optimize the random generation prefix expression algorithm, but in later performance analysis I found that the core code takes less than 1%, the real speed is the number to the string conversion process. And this is mainly because of their own design score this data structure has a problem, so that the program output is also more conversion, the score of the data structures have been modified, the speed of the program to improve some. I think this corrects the error in the optimization, the optimization of the data structure is also very important.

Personal Project--the primary student of calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.