C++编程:算法设计、函数运用与实践
立即解锁
发布时间: 2025-08-20 01:48:52 阅读量: 1 订阅数: 3 


懒惰程序员的C++入门指南
### C++编程:算法设计、函数运用与实践
#### 1. 编程前的算法规划
在开始编写代码之前,制定一个计划是至关重要的。以绘制一系列同心圆为例,我们先思考程序运行时应该发生的事情:
1. 绘制第一个圆。
2. 绘制第二个圆。
3. 持续绘制,直到圆小到看不见(假设半径为 1)。
但这个算法还不够具体,因为我们不知道如何绘制这些圆,关键在于不知道它们的半径。我们可以任意选择最外层圆的半径,这里设为 200。对于后续的圆,我们希望其面积是前一个圆的一半。根据圆的面积公式 \(A = \pi r^2\),要使下一个圆的面积是前一个圆的一半,下一个圆的半径应为前一个圆半径除以 \(\sqrt{2}\)。
最初的算法可以写成:
- 绘制第一个半径为 200 的圆……
但这里没有说明绘制的位置,所以改进为:
- 在屏幕中心绘制半径为 200 的第一个圆。
- 在屏幕中心绘制半径为 \(200 / \sqrt{2}\) 的第二个圆。
- 在屏幕中心绘制半径为 \(200 / \sqrt{2} / \sqrt{2}\) 的第三个圆……
这样写过于复杂,我们可以使用变量来简化。将半径初始值设为 200,每次绘制后将半径除以 \(\sqrt{2}\)。由于不确定要绘制多少次,但至少会绘制一次,根据循环的黄金法则,这里使用 do - while 循环:
```cpp
start radius at 200
do
draw a circle at center of screen, with this radius
divide radius by √2
while radius > 1 (quit when circle's too small to see)
```
#### 2. 算法验证
在编写代码之前,我们需要验证算法是否可行。通过逐步跟踪代码执行过程来确认:
1. 初始时,半径设为 200,面积为 \(\pi \times 200^2\),绘制该半径的圆。
2. 半径变为 \(200 / \sqrt{2}\),面积为 \(\pi \times (200 / \sqrt{2})^2 = \pi \times 200^2 / 2\),是第一个圆面积的一半,绘制新圆。
3. 半径变为 \(200 / \sqrt{2} / \sqrt{2}\),面积为 \(\pi \times (200 / \sqrt{2} / \sqrt{2})^2 = \pi \times 200^2 / 4\),是第一个圆面积的四分之一,绘制新圆。
经过验证,算法看起来是可行的。随着程序复杂度的增加,这种算法验证过程会更加有用,避免在不确定算法是否正确的情况下花费时间编译程序。
#### 3. 代码实现与注释技巧
在创建程序时,我们通常在文件顶部说明要做的事情,然后将算法以注释的形式放入 main 函数中:
```cpp
//Program to draw 5 concentric circles
// Each circle is twice the area of the one inside it
// -- from _C++ for Lazy Programmers_
#include "SSDL.h"
int main (int argc, char** argv)
{
//start radius at 200
//do
// draw a circle at center of screen, with this radius
// divide radius by sqrt (2)
//while radius > 1 (quit when circle's too small to see)
}
```
将算法作为注释包含在程序中,这样已经完成了大部分注释工作。不同的编辑器有不同的快速将文本转换为注释的方法:
- **Emacs**:
1. 高亮显示要注释的区域。
2. 选择 C++ ➤ Comment out region 进行注释。
3. 按 tab 键进行缩进。如果是无图形界面的 Emacs,在区域一端按 Ctrl - space,移动光标到另一端,按 Ctrl - c Ctrl - c 进行注释,按 tab 键缩进。
- **Visual Studio**:点击 Comment out 按钮(界面右上角类似平行水平线的按钮)将高亮代码转换为注释。
接下来,我们先编写容易的部分,即半径的声明和循环:
```cpp
int main (int argc, char** argv)
{
double radius = 200.0; // start radius at 200
do
{
//draw a circle at center of screen, with this radius
//divide radius by √2
}
while (radius > 1.0); //quit when circle's too small to see
return 0;
}
```
然后将中间步骤用代码实现,同时保留算法注释:
```cpp
int main (int argc, char** argv)
{
double radius = 200.0; // start radius at 200
do
{
//draw a circle at center of screen, with this radius
SSDL_RenderDrawCircle (CENTER_X/2, CENTER_Y/2, int (radius));
radius /= sqrt (2); //divide radius by √2
}
while (radius > 1); //quit when circle's too small to see
return 0;
}
```
由于需要屏幕中心的坐标,我们添加获取窗口宽度和高度的代码:
```cpp
int main (int argc, char** argv)
{
const int CENTER_X = SSDL_GetWindowWidth();
const int CENTER_Y = SSDL_GetWindowHeight();
double radius = 200.0; // start radius at 200
do
{
//draw a circle at center of screen, with this radius
SSDL_RenderDrawCircle (CENTER_X/2, CENTER_Y/2, int (radius));
radius /= sqrt (2); //divide radius by √2
}
while (radius > 1); //quit when circle's too small to see
return 0;
}
```
最后,添加一些友好提示和常规收尾代码,得到完整的程序:
```cpp
//Program to draw concentric
```
0
0
复制全文
相关推荐










