ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《卷轴教程之C++入门》#5 设计一个算法1 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

《卷轴教程之C++入门》#5 设计一个算法1

Devising an Algorithm C++ Program - Part 1

本期我们要学习的概念是
Alright, so in this video we want to look at the following concepts,
什么是算法
what is an algorithm,
什么是伪代码
what’s pseudocode,
什么是代码注释
what are code comments,
如何写注释
and how do we go about writing those comments in our code,
还有 C++ 的基本算术运算
basic arithmetic operations within C++,
我们也将学习
and also we’ll look at the concepts and ideas,
类型转换和整数除法
behind type conversion and integer division.
我们先来说说算法是什么
So let’s talk a little bit about what an algorithm is.
算法其实就是
An algorithm is just simply,
一系列有序的指令 能产生想要的结果
a set of ordered instructions that produces a desired result.
你也可以把算法看成
Eh, you could also think about an algorithm as, as just being,
类似食谱的东西
a recipe for doing something.
就比如
So we could think about,
做麦片的食谱是
a recipe for making cereal.
拿麦片 拿碗 拿牛奶
You know, getting the cereal, getting a bowl, getting the milk,
把麦片倒进碗里
pouring the cereal into the bowl,
把牛奶倒到麦片上
pouring the milk onto the cereal,
拿勺子
ah, getting a spoon, and then,
吃麦片
eating the cereal.
这是一个很简单的算法
That’s a very basic algorithm,
希望你能对算法有个概念
but hopefully you get the idea of what we’re looking at here.
事实上 在开发程序时
And it turns out, that in program development,
花费时间最多的事
this is where you’ll spend a lot of your time,
就是设计解决问题的算法
is devising the algorithm to solve some particular problem.
比如你要找到两点间的最短路径
You may be, trying to figure out the shortest path between two points,
或者要走出一个迷宫
or maybe how to navigate through a maze,
不仅要找到走法
eh, and get a proper solution there,
还要知道怎样找走法最高效
and the most efficient way to navigate through a maze.
刚开始学习编程时
So, early on whenever you’re learning how to program,
你可能会花大量时间在语法上
you are going to spend a lot of time trying to figure out the syntax,
也就是纠正自己代码中的
and maybe solving different errors that come…pop up from,
各种语法错误
the way you’ve coded something.
但随着你对一门语言逐渐熟悉
But later on, once you become proficient at a particular programming language,
你更多会花费时间在
where you’ll spend most of your time,
设计开发算法上面
is in designing and developing the algorithm,
然后才是用代码去实现算法
before you actually implement that algorithm in a specific language.
因此算法是与语言无关的
So algorithms are, you know, language independent,
用什么语言无所谓 只用一张纸我们就能设计算法
doesn’t really matter, we can just work it out on a sheet of paper,
然后再上机实现代码
before we actually go and start coding something up.
这就是算法的概念
So that’s the basic idea behind an algorithm,
能产生理想结果的一系列有序指令
a set of ordered instructions that produces a desired result.
或者简单点说 就是一张食谱
Or we can simply just say, a recipe.
好了 那么本期要介绍的其他概念还有
Ok, so for the other topics that we’re going to be looking at in this video,
注释 伪代码
comments, pseudocode,
基本算术运算 类型转换
basic arithmetic operations, type conversion,
以及整数除法
and integer division,
我们从一个具体问题出发来学习这些概念
we’ll look at them as we solve this particular problem here.
这个问题是
So this is the problem statement we have,
编写一个程序
says we want to create a program,
显示出某部电影
that displays the average scores,
在 Metascore 和 Tomatometer 上得到的
from metacritic’s Metascore, and Rotton Tomatoes’ Tomatometer,
平均评分
for a particular movie.
平均分采用十分制
The average score must be displayed on a 10 point scale.
电影的标题和评分由用户输入
The movie title and scores will be inputted by the user.
问题的描述
So this is our problem statement,
以及程序的要求就是这样
our basic requirements specifications for this program.
从这个问题出发
And based off of that,
我们要设计一个算法
We’re going to devise an algorithm,
并用 C++ 代码来实现它
and then we’ll implement the actual C++ code.
我们开始吧
So let’s get started.
先创建一个程序 ‘Program3’
Ok, so let’s go ahead and get started with ‘Program3’.
点击菜单 ‘File’ ‘New’
So we’ll come here to ‘File’, ‘New’,
鼠标移到 ‘C++ Project’
and then come across here to ‘C++ Project’,
单击
left click,
因为这是第三个项目了
and we’ll name this project ‘Program3’,
所以叫它 ‘Program3’
since this is our third program.
‘Project Type’选择’Empty Project’
Under ‘Project Type’ you can leave it as ‘Empty Project’,
‘Toolchains’ 选择 ‘MinGW GCC’
‘Toolchains’, make sure ‘MinGW GCC’ is selected,
点击 ‘Finish’
and then click on ‘Finish’.
好了 现在有了 ‘Program3’ 的目录
Ok, so we’ve got our ‘Program3’ project folder,
右键单击它
we can right click on that,
鼠标移到 ‘New’
then go here to ‘New’,
左键单击 ‘Source File’
and then left click on, ‘Source File’.
因为我们要创建的是 C++ 源文件
So I want to create a C++ source file,
所以就叫它 ‘Program3.cpp’
and we’ll just name it ‘Program3.cpp’,
然后点击 ‘Finish’
and then click on ‘Finish’.
好 现在创建好了源文件
Ok, so now we have our Program3 source file,
准备写代码了
and we’re ready to start typing in some things here.
我们首先
So the first thing that I’m going to do,
把问题题目粘贴进来
is paste in our problem statement.
你的电脑上没法粘贴
You won’t be able to do this,
不过你可以简单输入点问题描述
but maybe you can type in a brief summary,
或者随便你输点啥
or just a…a little bit of text here.
因为我只是要演示 Eclipse 里如何写注释
Cause I’m going to show you how to create a comment in Eclipse.
那么我就直接粘贴上
I’m going to go ahead and paste in,
问题的题目
our problem statement that we saw before.
然后全选
And then I’m going to select all of this text here.
按下键盘 ‘Ctrl’
And then hit ‘Control’.
按住不要松手
While holding down the ‘Control’ key,
然后按 ‘/’
hit the ‘/’.
这样就把选中的文字都变成注释了
And that creates a comment out of all this text here.
那么注释到底是什么呢
Ok, so what exactly is a comment?
注释就是源文件中的一些文字
A comment is just simply, text within our source code file,
这些文字
that we, as humans, developers,
是给开发程序的人看的
you know, other people that may be looking at the code can read.
编译器不会管这些文字
but this is going to be ignored by the compiler.
编译 ‘Program3.cpp’ 时
So when we go and compile ‘Program3.cpp’,
只要是 ‘//’ 开头的行
any line, that starts with this ‘//’,
都会被编译器忽略
is going to be ignored.
编译器不会试图把注释转换成机器代码
It’s not going to try and compile that into some machine code.
所以注释里一般写什么呢
So what do you traditionally find in comments?
一般会描述程序的功能
We’ll usually find maybe a description of the programme, what it does,
作者是谁
maybe the author,
啥时候写的
the date that it was created originally,
啥时候改的
maybe the last date that it was modified,
有时特定的一小段代码也会有注释
and also, you’ll find comment associated with certain portions of the code,
可能用来解释这里为什么要这么计算
maybe explaining why a particular calculation was used,
乘以某个常数是为了什么
or particular factor was used in a calculation,
总之就是解释不合常规的东西
that maybe is outside the norm.
但是 没有必要
But it’s not going to be a comment explaining,
为每一行代码都写注释
you know, ever single line of C++ code and what it does.
你写出来的代码应该清晰明了
The code itself should be self-documenting,
对于熟悉一门语言
so someone that understands how to write a particular programme,
会写程序的人来说
in a particular language,
程序的语法是不需要解释的
would need to have an explanation of the syntax.
但某个地方为何要这样算
But they may need an explanation why a particular calculation was used,
你的算法的原理则是需要解释的
or how the algorithm may work in general.
这就是注释的作用
So that’s the basic idea behind comments.
它就是源文件中的一些文字
It’s just some sort of text, within our programme source file,
用来给我们
that’s for us,
提供描述或者说解释的
providing maybe a description, or further explanation of something.
编译器不会管注释
And it’s going to be ignored by the compiler.
好了 那么我们回头看看问题
Ok, so let’s take a look back at the problem statement,
然后试试设计出一个算法
and try to devise a basic algorithm for our programme.
问题要求我们写一个显示某部电影
So we need to create a program that displays the average scores,
在两个众评网站上的
from metacritic’s Meatascore and Rotton Tomatoes’ Tomatometer,
平均分的程序
for a particular movie,
平均分满分十分
the average score must be displayed on a 10 point scale,
电影标题和评分由用户输入
and the movie title and scores will be inputted by the user.
习惯上 每次我们分析问题
So traditionally whenever we first start analysing a problem statement,
或者分析程序的
or some sort of requirement specifications,
设计需求时
for a programme,
我们都先找出要输入什么信息
is to figure out the inputs that we’re going to have.
在这个问题中
So looks like we have,
我们要输入三个信息
3 different pieces of information that need to be inputted.
首先是电影标题
One is the movie title,
然后是两个众评网站的评分
and the other two are the scores,
一个来自 Metacritics
one from metacritic’s, the Metascore,
一个来自 Rotten Tomatoes
and another one from Rotton Tomatoes’ Tomatomoon…meter.
有了这些信息
So once we have that information,
我们就能计算平均分了
then we can calculate the average score,
我们可能还要调整评分
and we may also have to condition that score,
确保算出来的分是十分制的(标准化)
so that it fits on a 10 point scale.
我们先获取众评网站的评分
We’ll see what Metascore and Tomatometer scores give to us,
然后再调整评分
and then from there, we can worry about our scaling.
好了 动手试试设计算法吧
Alright, so now let’s look at just trying to sketch out the basic algorithm,
先不要写出代码
without actually writing any code.
想一想 程序要做的第一件事是
So we’ll come here, and say, well, the first thing maybe we need to do, is,
获取电影的名字
is get the name of the movie.
然后获取 Metascore 的评分
And then we also need to get the Metascore.
所以输入“获取 Metascore”
So I’ll say, ‘Get the Metascore’.
接着获取另一个网站评分
And then we need to get the other score,
输入“获取 Tomatometer 评分”
so ‘Get the Tomatometer score’.
接着计算平均分
And then we’ll get the, the average, or we’ll ‘Calculate the average’.
也就是把两个网站的分平均一下
So we can get the average of those two scores.
再把评分标准化
And then we may need to do some rescaling,
输入“标准化平均分”
so I’ll say ‘Rescale average’.
标准化完成后
And then once we rescaled the average,
就可以把最终分数显示出来了
we ready to actually display this, the new average,
所以输入
or display it, so we’ll just say,
“显示结果”
‘Display the result’,
结果是指我们计算的结果
‘of our calculations’, maybe.
流程大概就是这样
And that’s basically it.
我们输入的这一堆
So all this stuff here,
就是在描述我们的算法
is a description of our algorithm,
还没有真正去写代码实现它
without actually writing any C++ code.
这样的描述就叫做伪代码
And what we call this is pseudocode, so,
程序开发刚开始的时候
often times whenever you’re in early code development,
我们往往需要先勾勒出程序的基本功能
you want to just sketch out what you want to do,
再实际去写代码
without writing any specific code.
这个过程就是思考问题的过程
So this just gets you thinking about the problem.
现在我们写好了伪代码
Ok, so now that we have our pseudocode in place,
那么就开始写真正的代码吧
so let’s go ahead and start actually writing some C++ code.
把光标移到问题下方
So I’m going to go here below the problem statement,
输入 ‘#include’
and put in our ‘#include’ statements, so ‘#include’,
‘iostream’ 先包含这个库
‘iostream’ is going to be the first one that we include,
这样我们就有了从键盘获取输入
so that we have support from…for getting input from the keyboard,
和输出到屏幕的功能
and also doing output to the screen.
我们还需要程序支持字符串类型
We want to have support for the string datatype,
所以也要包含 ‘string’
so let’s include ‘string’,
接着输入 ‘using’
and let’s also do the ‘using’,
‘namespace’
‘namespace’,
‘std’ 这样就省去了一些代码
‘standard’, save us a few keystrokes.
然后输入 main 函数
And now we’re ready to start typing in the main function,
输入 ‘int main()’
so ‘int main()’,
左花括号
And then I’ll put an ‘{‘ here,
光标移到下面
and then come down below,
输入右花括号
and put a ‘}’.
现在 伪代码位于 main 函数内部
So now, inside of this main function, we still have all of our pseudocode,
因此我们需要缩进它
and I’m going to just tab this pseudocode over.
凡是有花括号的地方
Basically, anytime we have an ‘{}’,
括起来的东西都需要缩进一级
anything inside of that should be coming over one level of indentation.
按下 ‘Tab’ 键就可以缩进
So I’m just using the ‘Tab’ key to tab all this over.
好 现在
Ok, what we’re going to do,
来做填空题
is start filling in this blanks here,
每行注释下方的空白处都要填上代码
filling in the section below each of the comments.
为了获取电影名
So in order to get the name of the, the movie,
我们要提示用户
we’re going to actually prompt the user of this programme,
输入电影名
to input the movie name.
那么第一步就是
So the first thing we need to do,
声明一个变量
is actually declare a variable,
用来存放电影名
that’s going to be able to hold the name of the movie.
这个变量
So I’m going to just introduce a datatype of sting,
是 string 类型
a variable of type string,
名叫 ‘movie’
and we’ll call it ‘movie’,
初值设为空字符串
and we’ll set it to the empty string.
然后
Ok, and then,
使用 ‘cout’
we’ll display to the user, using our ‘cout’,
询问用户
and ask them,
‘What is the name of the movie?’
‘What is the name of the movie?’.
然后加入 ‘: \n’
Ok, I’m going to put a ‘: \n’,
用来换行
for a newline character,
最后输入分号
and then a ‘;’ at the end here.
现在我们完成了
Ok, so now that we have the prompt,
提示用户输入电影名的部分
for the user to enter in the name of the movie,
接着要从键盘读取用户的输入
we need to be able to read in input from the keyboard.
我们之前的做法是
So usually what we’ve done,
用 ‘cin >>’
is just use ‘cin’ and then the ‘>>’,
加上变量名
and then specify the name of the variable,
指定要把输入存放到哪个变量
where we want to store that input.
问题是 ‘cin >>’
Unfortunately, this ‘cin’ and then the ‘>>’,
只能获取到电影名里的第一个单词
is only going to get the first word of the movie.
如果名字里有多个单词
So if it’s a multi-word movie title,
我们只能取到第一个
we’re only going to get the first word.
所以我们要换种方法
So we need something else besides what we have here,
那就是用一个函数
and we can make use of a function,
叫做 ‘getline’ 函数
call the ‘getline’ function.
它能获取一整行的输入
And now we’ll actually get an entire line.
那么
So,
这个函数需要提供两个参数
what we have to do is specify 2 arguments to this function.
第一个参数指定从哪里获取输入
So the first argument is going to be where we’re getting it from,
我们要从字符输入流 也就是键盘
so we’re going to be getting it from the character input stream,
获取输入
or the keyboard.
所以输入 ‘cin’ 就行了
So we can just say ‘cin’,
第二个参数指定
and then where do we want to store it to,
输入要存放在哪
so that’s the second argument.
所以输入 ‘movie’ 也就是我们之前
So the second argument is going to be ‘movie’, that’s the variable,
声明的那个变量
that we’ve declared previously.
我们需要把电影名存放到
And we want to store whatever the name of the movie is,
这个变量里
into that particular variable name.
这是很基础的东西
Alright, so that’s pretty basic.
这里我们第一次调用了一个函数
This is our first time that we’ve actually called a function.
其实
So,
如果你想调用一个函数 也就是使用它的功能
basically, in terms of calling a function, to make use of it,
只需指定函数名
you specify the name,
如果这个函数需要参数
and if they have any arguments that have to be specified,
还要在括号里提供参数
and those are specified within the parentheses here.

Ok.
获取电影名的代码
So we’ve got, basically all the code that we need,
我们写完了
to get the name of the movie.
来到下一步
And now we’ll come down here,
获取 Metascore 评分
and get the Metascore,
评分应该是个整数
so the Metascore is going to be an integer value.
所以
So,
声明一个整数变量 输入 ‘int’
we’ll just declare an integer variable, ‘int’,
然后
and we’ll just say,
变量名 ‘metascore’
‘metascore’ is the new…the variable.
赋初值为 0
And we’ll initialise that to zero.
向用户输出提示信息
And then we’ll have some output to the user.
输入 ‘cout <<'
So ‘cout’ and then the ‘<<',
然后
and we’ll say,
‘What is the metascore’
‘What is the metascore’,
‘rating for’
‘rating for’,
光标移到外面
and then we’ll come in here, and,
输入插入运算符
use our insertion operator,
再输入变量名 ‘movie’
use our variable called ‘movie’,
可能还要加个 ‘?\n’
and then, maybe ‘?\n’ for a newline character.
给用户的提示就是这样
Ok, so this is just the prompt that we’re going to have,
然后获取 Metascore 评分的输入
then the input, the…the rating from Metascore.
获取了评分后我们就能用来计算了
And then once I’ve done that, we can then read in that value.
这次可以用 ‘cin’ 了
We can go back to using our…our ‘cin’ in this case,
因为要获取的只有一个值
since it’s just a single value,
中间没有空格 没有多个单词
it’s not going to have any spaces or multiple words or anything like that,
所以输入 ‘metascore’
so we just type in ‘metascore’, here.
跟得上吗
You ok?
获取 Tomatometer 评分的代码完全类似
And it’s going to be very similar down here for the Tomatometer score,
这几行代码完全一样
so it’s almost the same exact lines,
只是要改改变量名
we’ll just change up the…the variable name.
和提示的文字
And a little bit on the string literal that we’re going to have there.
我们输入 ‘int’
So we’ll say ‘int’,
‘tomatometer’
‘tomatometer’,
赋值为 0
and we’ll assign that to zero.
输入 ‘cout’
And then we’ll do ‘cout’,
‘<<'
use the ‘<<',
输入提示
and we’ll say,
‘What is the tomato’
‘What is the tomato’,
这里要大写 ‘Tomatometer’
let’s capitalise that ‘Tomatometer’,
‘rating for’
‘rating for’,
然后输入 ‘movie’
and then we’ll use ‘movie’,
‘?\n’
and then we’ll put in a ‘?\n’,
即换行符 再加上分号
the newline character, and a ‘;’.
接着从键盘读取输入
And then we’ll actually read in a value from the keyboard,
所以用 ‘cin >>’
so ‘cin >>’,
变量名 ‘tomatometer’
and then we’ll use our variable, ‘tomatometer’,
差不多完成了
and that’s basically it.
我们已经获取了电影名
So at this point in time, we got the input for the name of the movie,
Metascore 评分
we’ve got the score from, or the rating from Metascore,
Tomatometer 评分
and the also have the rating from Tomatometer.
输入部分已经完成
So we’ve got our input,
接下来要计算平均分
and then now we can actually go in and calculate our average.

发表评论

译制信息
视频概述

卷轴 C++ 入门教程:设计一个简单算法

听录译者

positiveme

翻译译者

HX

审核员

知易行难

视频来源

https://www.youtube.com/watch?v=q11MdcomARw

相关推荐