Program design
Before coding, design the program first.
This sounds obvious, which means I will probably ignore it and then suffer.
Simple process#
- Define the goal of the program.
The program should read two numbers.
- Define requirements, meaning what the program should do.
The user enters numbers.
The program calculates the result.
The program prints the result.
- Break a hard problem into smaller problems.
calculator
- get first number
- get operation
- get second number
- calculate result
- print result
- Turn those smaller parts into functions.
getUserInput();
getMathematicalOperation();
calculateResult();
printResult();
This fits the rule that a function should do one specific thing.
- Decide the order of actions.
input -> calculation -> output
- Start with
main()and commented function calls.
int main()
{
// getUserInput();
// getMathematicalOperation();
// calculateResult();
// printResult();
return 0;
}
- Implement functions one by one.
prototype -> body -> test
Do not write the whole program at once.
Start with a simple working version.
Do not improve the first version too early.
At the beginning, write for readability, not performance.