当前位置:编程学习 > 网站相关 >>

初学者指南:如何像程序员一样思考

 

很多人通过Email问我,怎样开始写一个程序。也许最好的建议非常简单,把程序的步骤写下来:一旦你开始把想法或者代码写下来后,你就会对你的项目有一些感觉了。通常程序设计有两种通用的做法:从上到下的方式和从下到上的方式。

A lot of people email me asking the way to start some program or another. Perhaps the best advice is simply to start writing down a layout for the program: once you start writing down ideas or code you'll start to get a feel for your project. There are two standard methods of program design: the top-down approach and the bottom-up approach. Top-down programming involves writing code that calls functions you haven't defined and working through the general algorithm before writing the functions that do the processing. Top-down programming is, to a good degree, a very abstract way of writing code because it starts out by using functions you haven't designed, and that you perhaps do not know how to design. The bottom-up approach to programming is the opposite: the programmer writes the basic functions she realizes will be necessary at some point in the programming and then work up to the more complex parts of the program.

 

比较有意思的是,那两种方法都是关注程序中要执行的动作或方法,而不是关注程序要处理的数据。很多时候,写程序最好的方法是,写出你将要用到或处理的数据,然后再从上到下的想怎么样去处理这些数据,最后才能得到你需要的结果。首先定义数据,然后再写出那些要处理这些数据的相关的函数。,这样你才会得到你的程序应该怎样写的基本思路,

 

It's interesting that both of these approaches focus on the actions of the program rather than the objects the program manipulates - variables. Many times, the best way to write a program is to figure out the variables you need to work with and then progress with a top-down approach to the program that manipulates those variables. By defining variables first and then working with functions that work on them, you will always maintain a basic foundation of what your program should be doing. Once you have an idea of what variables you will be using, then you can write functions to perform the operations you need to perform on the variables while maintaining sight of the goal. Finally you can write the code for each individual function.

 

Another value to defining variables before writing code is that many times you can accomplish an entire program without many functions; this fact is especially true when you are a beginner 易做图 易做图 programs. The variables give you the raw materials you need to begin working with the tools: loops, if statements, library functions, and perhaps user defined functions.

 

 

 

 

现在让我们来看一个关于怎样开始写一个完整程序的例子。假设你要写的程序是要模拟一个DVD商店的租售系统,这个系统需要计算出出租DVD的总收入。你的程序有可能要求,需要输入一个代码,告诉你这个DVD租售的价格是2元一天还是是3元一天,然后还需要它出租了多少天,最后如果这个输入的代码是0,整个程序就结束了。你应该要分别计算出租金为3元/天和2元/天的DVD的出租的总天数。拿这个程序来说,思考设计程序的最好的方式是,想象为了计算出租金的收入,你需要存储哪些信息:

 

 

 

你需要一个变量用来存储总收入,当程序结束时;

 

你需要一个临时变量用来存储代表DVD的租金的代号;

 

你需要一个临时变量用来存储某个DVD出租的天数;

你需要一个变量来存储租金为3元/天的所有DVD出租了多少天的总数;

 

最后,你还需要一个变量来存储租金为2元/天的所有DVD出租了多少天的总数;

 

 

Let's take a look at an example of how to go about thinking about a program. If you were to write a program to simulate a video store rental system that calculates the gross revenue from rentals, you might be asked to write a program that accepts a code telling you whether a certain video was rented at $2.00 (input as 2) a day or $3.00 (input as 3) a day and then asks for how many days it was rented out for; finally, if the code for the cost of rental is 0 the program should terminate. You should also count the number of days videos were rented at $3.00 per day and $2.00 per day. The best way to think about the design for a program such as this one is to imagine what information you need to store in order to calculate the revenue:

 

 

you need a variable to store the total dollar amount at the end of the program;

 

you need a temporary variable to store the code for the cost of a transaction;

 

you need a temporary variable to store the number of days a specific video was rented;

 

you need a variable to store the number of days each video was rented;

 

you need a variable to count the total number of days $3.00 videos were rented;

 

finally, you need a variable to count the total number of days $2.00 videos were rented.

一旦你认识到你需要这些数据,那么你就很容易想出如何处理这些数据:比如,你知道租金2元/天的DVD的总收入=所有租金为2元/天DVD的出租天数之和*2;类似的也可以计算出租金3元/天的DVD的总收入。你也会理解这个“代表DVD的租金的代号”,这个变量的用处是,当用户输入某个DVD出租的天数时,决定哪个变量会易做图作。在你的程序中你需要一个循环结构。

 

 

 

 

Once you realize you need these variables, you can easily imagine how to translate them in terms of each other: for example, you know the total amount of revenue is the number of days videos at $2.00 were rented times $2.00; in similar fashion, you know the relationship for $3.00 a day videos. You should understand that the transaction 'code' determines which variables are manipulated when the user inputs the number of days a specific video was rented (for example, whether to add to the count of days for $2.00 videos or $3.00 videos). You'll probably need a loop in your program (although you can't necessarily infer this from the variables).

 

 

 

程序的代码有可能会像下面那样:

 

 

The code might look as follows:

 

 1 The code might look as follows:

 2

 3 int main()

 4 {

 5   int total_dollars = 0;

 6   int total_days_at_3_dollars = 0;

 7   int total_days_at_2_dollars = 0;

 8   int transaction_code = 0;

 9   int days_for_one_video = 0;

10   do

11   {

12     if(transaction_code==2)

13       total_days_at_2_dollars+=days_for_one_video;

14     if(transaction_code==3)

15       total_days_at_3_dollars+=days_for_one_video;

16     cout<<"Please enter a transaction code and number of days a video was rented: ";

17     cin>>transaction_code>>days_for_one_video;

18   }while(transaction_code!=0)

19   return 0;

20 }

 

我希望,你现在已经有了一个基本的思路,在写代码之前,应该如何安排你的程序的结构。

 

 

Hopefully, you now have a basic idea of how to lay out your program structure in your mind before you begin to write code.

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,