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

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

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

Devising an Algorithm C++ Program - Part 2

在我一会儿就要编写的三两行代码中
So in these next couple of lines of code that I’m going to write,
我会故意安排一些错误
I’ll maybe make some mistakes on purpose,
然后我们过一会儿再回过头来看错误处
and then we’ll come back and look at it.
通常情况下 在学习某些知识方面
But often times, in terms of learning something,
如果先展示出一个问题
if you exhibit the problem first,
然后再回来更正它 这种方法会让你
and then come back and correct it,
对知识会有更加深刻的印象 理解的也更加彻底
it’s more impactful in terms of getting things right later.
废话不多说 回到正题 我们该怎样计算平均数呢
Ok, so how do we go about calculating the average?
平均数就是
So the average is just going to be,
若干个数值的总和
the summation of some values,
除以数值个数的结果
and then dividing by the number of values that we have.
这是一个非常基础的计算方法
So a pretty basic calculation.
首先我们需要一个用来存储计算结果的变量
We’ll need to have a variable to actually store the result of our calculation.
所以
So,
我们现在考虑一个基本的语句
we can think about just the basic expression,
我们想要你做些什么呢
what are we wanting you to do?
我们想要你取出Metascore
We’re wanting you to take the, the Metascore,
和Tomatometer的值
and the Tomatometer score,
并把它们加起来
add those up,
然后除以2
and then divide by 2,
由于我们已经声明过两个score了
since there’s 2 scores there.
所以我们想 或许应该是这样的一条语句
So we can think of, maybe an expression like this,
metascore + tomato
‘metascore + tomato’,
tomatometer
‘tomatometer’,
然后我们想要把它除以2
and then we want to divide by 2.
现在我们一定要小心这里的除法运算
Now we have to be careful here with this division here,
我们应该牢牢记住运算顺序
we have to remember our order of operations.
我们知道应该在这里放置一个圆括号
So we know that we know that we do anything in parentheses first,
我们做一个说明 先是乘法或者除法
then we do exponents, then multiplication/division,
然后是加法和减法
and then addition and subtraction,
由左至右运算
working from left to right.
所以
So,
就是这样了 这里有详细说明
they ways it’s, it’s specified here,
实际上在加法运算之前会先做除法运算
would actually do this division operation before we did the addition.
我们实际上想要计算出这些数值的和
We actually want to sum these value first,
所以这里应该放置一个圆括号
so we should probably put parentheses here.
为了先做括号内的运算
So that we construct parentheses around some particular operation,
所以我们用圆括号环绕着这个特别的运算
that we want to do before some other operation,
仅仅是为了推翻之前说过的算符优先
to override the operator precedence of multiplication before,
抱歉 口误 我想说的是除法优先于加法运算
or excuse me, division before addition here.
所以在这里先敲入一个圆括号
So we do what’s in parentheses first.
然后我们要做的就是
And what we need to do,
把这里的计算结果赋值给一个变量
is assign this particular calculation here, to a variable.
把这个值赋给目标变量average
So I’m going to just assign it to,
就叫它avg好了
an end variable called average, avg.
在声明末尾处千万不要忘记写一个分号
And make sure that our statement ends with a semicolon.
好的 现在我们就有一个合法的
So we have a valid statement here in C++,
用来计算平均数的声明了
to calculate the average.
好的
Ok.
我想指出的另外一件事就是
The other thing that I’m going to point out here is that,
Metascore和Tomatometer
Metascore and Tomatometer,
这两个变量的范围都是100
they both offer values that are on 100 point range,
而不是10
as oppose to a 10 point range.
所以在这里要重新调节我们刚提出的平均数
So this is where we need to rescale the average that we come up with,
把它的范围调成10
from 100 point range to a 10 point range.
所以概括的说就是 无论计算出什么结果
So basically we’re taking whatever value we calculated here,
都得把它除以10
and we’re going to divide it by 10.
现在看下面的这个Rescale average
So, down here under ‘Rescale average’,
声明一个新的变量
I’m going to declare a new variable,
就命名为rescale吧
and we’ll call it ‘rescale’,
或许应该是rescaledScore
maybe ‘rescaledScore’,
或许这才是个好的变量名
maybe that’s a good variable name.
现在我们将要获得这个预先计算好的平均数
And we’ll take the average that we previously calculated,
为了把它的范围重新调节成10
and divide it by 10,
把它除以十
so that rescales it onto a 10 point scale.
然后显示出结果
Then for displaying the result,
这仅需要一个cout
it’s just going to be a ‘cout’,
然后打上
and then we’ll say,
Our calculated
‘Our calculated’,
呃 打calculated
Oops, just going to say, ‘calculated’,
average
‘average’,
on a 10 point scale
‘on a 10 point scale’,
for 在这里要输出movie的名字
‘for’, and we’ll put in the name of the movie here,
然后
and,
is
we’ll say ‘is’,
空格 我看一下
space, let’s see,
然后输入变量rescaledScore
and then put in the variable ‘rescaledScore’.
好的 然后可以在末尾处输入一个句号
alright, and then we can put a period here at the end.
如果你想 你可以在这里加几行
If you prefer, you can put this on multiple lines.
所以无论何时你看这里的插入符
So anytime you see this insertion operator here,
在它前面或者后面敲击一个回车
you can hit ‘return’ before or after the insertion operator.
把它增加到下一行 这样一行就不会太长了
And I’ll just put it on the next line, so that way our line is not too long.
现在还有最后一段代码
That’s our last bit of code that we need to have.
应该要做的一件事
One thing else that we probably should do,
就是return 0
is have this ‘return 0’.
如果这里没有放置一个return 0
If we don’t put that ‘return 0’ in there,
那么默认的主函数也是可以的
it will be ok by default main,
如果是在C++中 如果这里你没有一个返回声明
if you in C++, if you don’t have a return statement in there.
那么就会有一个默认的return 0声明
It will default to a ‘return 0’ statement.
这样也许会在某些开发环境 某些编辑器中
You may end up with some sort of warning from whatever editor,
产生一些警告
or whatever integrated development environment that you’re using.
OK 看起来一切就绪了 可以保存并建造项目了
Ok, so it pretty much looks like we’re ready to, to save and build our programme.
不过在这之前
But before we do that,
我们先计算出scaled average的结果
let’s go ahead and calculate what the scaled average would be,
转换成10分制的结果
on a 10 point scale,
用来作为一个电影的详细说明
for a particular movie,
如果我们打算使用Metascore和Tomatometer评分
if we were to use the Metascore and the Tomatometer score.
那么我们需要想出一个特殊的测试用例
So we want to be able to come up with a particular test case,
看看会得到什么结果
see what the value would be,
然后 当程序运行时
and then when we run our programme,
我们就能通过对比实际数值和期望数值来测试程序是否正确
we can test our, our programme against the value that we expect to receive.
所以 我在这里放置了一个movie
So, I’ve already pulled up a movie here,
《超人特工队》
The Incredibles,
在烂番茄网和Metacritic中
on both Rotton Tomatoes and metacritic,
哪一个有Metascore
which has the Metascore.
比如Metascore是90分
And so Metascore has a score of 90,
在烂番茄影视网上
and then on Rotton Tomatoes,
Tomatometer是97
it has a Tomatometer score of 97.
在这里我们先打开计算器算一下
So let’s just pull up our calculator here,
这里是90+97
and say ’90 + 97′,
接下来程序将给我们一个总和
and then that would give us the summation that we’re going to do,
然后这个总和将除以2
and then we’ll divide that by 2,
结果是93.5
which is ‘93.5’,
再然后 把这个结果除以10 得到十分制的数
and then we’ll divide that by 10 to get our scaled score.
所以说 如果结果是9.35
So we would expect to get a result of ‘9.35’ from our programme,
那么这个程序工作正常 没有问题
if it’s working correctly.
现在继续 回到Eclipse
So let’s go ahead and, and go back in here to Eclipse,
然后运行程序
and run this programme.
首先我们需要保存下
So first we need to save it,
然后建立工程
and then we will build it.
在建工程期间
So during the build phase,
编译器做的工作是编译和链接
we’re doing the compiling and the linking operations.
看起来没什么问题
Doesn’t look like we have any problems,
OK 现在继续 运行
so let’s go ahead and run this.
OK 现在向下看 看这里的控制台
Ok, so if we look down here at the console,
我们看到这样的提示What is the name of movie:
we’ll see the prompt of ‘What is the name of the movie: ‘,
电影名字是The Incredibles
so the name of our movie is ‘The Incredibles’.
输入它
so type that in.
然后敲击回车
Hit ‘Enter’.
然后呢 它提示
And then say, it says,
What is the metascore rating for the Incredibbles?
‘What is the metascore rating for The Incredibles?’,
metascore评分是90
the metascore rating was 90,
所以我们敲入 然后敲击回车
so type that in and hit ‘Enter’.
然后我们看到 What…Incredibles?
And then we have ‘What is the Tomatometer rating for The Incredibles?’.
97 然后我们敲击回车
So that was 97, and then we’ll hit ‘Enter’.
可能 程序输出这样的结果
So, looks like, for our programme it says,
超人特工队的平均得分 调整为十分制后是9
‘Our calculated average on a 10 point scale for The Incredibles is 9’.
然后你看 这里有一个点
And then it has a dot there,
实际上它只不过是个句号
and that’s actually the period,
它并不代表小数点
it’s not even representative of a decimal point.
它只不过是这句话末尾处的句号而已
It’s just the period that ended our, our sentence here.
其实这个结果与预期相比 并不是很精确
So, it’s not exactly what we were expecting.
预期数值是9.35
We were expecting to get 9.35.
现在回到刚才那个地方 重新审查程序
So let’s go back and, and look at our programme,
实际上就是计算并重新调整平均数那里
where we were actually calculating the average and rescaling the average.
现在你注意下这里 如果看的足够仔细
So what you’ll notice here, if we look at this closely,
你就会看到这里的数据类型有问题
we have a datatype over here,
在这个赋值运算的左边
on the left hand side of the assignment operation,
是一个int类型
of an ‘int’.
所以说这个平均数是一个int类型的数值
So we’re saying that the average is an ‘int’ datatype,
知道什么是int类型吗
and what do we know about ‘int’ datatypes?
好的 这就是典型的int类型
Well, those are representative of integers,
这个类型不会存储小数部分
so they do not store fractional parts.
所以如果在这里计算得到平均数
So if we calculate an average here,
我们知道这个平均数可能会有小数部分
and we know that the average can have a fractional part,
如果我们取一些数
if we were to take some set of numbers,
用它们除以另一个数
and divide them by some other number,
就有可能出现小数部分
we can end up with a fractional part.
我们弄丢它了 你懂的 小数部分被删掉或者丢失了
We’re losing that, that’s being, you know, truncated or lost,
所以说 在这里存储变量
whenever we store it to this variable over here,
将无法存储
cause it’s not capable of storing,
小数这一部分
a, a fractional part.
所以 我们能做的就是
So what we can do,
修改这一类型
is change this particular datatype,
把它替换成一个能保存小数部分的数据类型
to being a datatype that can preserve or save a fractional part.
我们想一下 double就是其中一个比较适合的类型
So one of the types that we looked at that can do that is the double,
所以现在把它改成double
so let’s change this to ‘double’.
这里的Rescale average也应该做同样的处理
And let’s also do the same thing here for ‘Rescale average’.
这个变量也需要保存小数
So we need to be able to preserve it there,
因为这里变量虽然保存了小数 然后在下一行除以10
cause if you preserve it here, and then you go and divide by 10 on this next line,
之后又把它存回到整型数值
and then store it back into an integer value,
这样无论怎样你都会再一次丢失小数部分
you’re going to lose whatever fractional part you had, again.
好吧 我们继续 保存
Alright, so lets go ahead and save this.
然后再次建立工程
And we’ll build it again,
再一次运行
and then we’ll run it again.
好的 现在重新来过
Alright, so let’s go back through,
我们将输入名字 The Incredibles
and we’ll type in the name, being ‘The Incredibles’,
再一次给metascore打分 是90
the metascore rating again, was 90,
Incredibles的Tomatometer评分是97
and the Tomatometer for The Incredibles was 97.
所以这次程序输出
So this time it says,
超人特工队的平均得分 调整为十分制后是9.3
‘Our calculated average on a 10 point scale for The Incredibles is 9.3’.
这仍然不是我们预期的9.35
So still not the 9.35 that we were expecting,
所以程序还是发生了一些错误
so something else is going on.
那么是什么错误呢 如果我们看这行代码
And what going on, is if we look at this line of code here,
我们看到(metascore+tomatometer)/2
we have ‘(metascore + tomatometer) / 2’,
看起来没问题
we’ll that looks like, you know,
它应该返回给我们一个带有小数部分的平均数
it should give us an average that has a fractional part.
但是在后面的场景中发生了一些微妙的事情
But something subtle is going on behind the scenes,
实际上如果我们理解了 就不会觉得它微妙了
or maybe not so subtle if we actually understand it.
metascore是一个整型数值
So, ‘metascore’ is an integer value,
就是我们刚刚输入的90
cause we typed in ’90’;
tomatometer也是一个整型数值
‘tomatometer’ was also an integer value,
我们刚才详细说明了两个整型数值
it’s just we specify that both of those were integer values.
tomatometer是97
It was 97,
无论何时把它们加起来
and whenever we add those together,
结果一定是一个整型数值
it’s also going to be resulting in an integer value,
这里的2仍然是一个整型数字
and this ‘2’ here is an integer literal.
所以做除法计算时
So whenever we preform this division here,
由于除数和被除数都是整型数值
what’s going on is it’s performing what we call, integer division.
所以依据我们所要求的计算方法 结果就是整型除法
since we have an integer both in the dividend and the divisor.
所以这个结果一定也是一个整型数值
So the result is also going to be an integer,
就是这样了
that’s just what it is.
所以这样看来 我们还需要去改变这件事
So the way that we need to change this,
就是能够使被除数是一个double类型数值
is that we can make either the dividend a, a double,
或者说除数是一个double类型数值
or the divisor a double,
无论是哪一个都没关系
it really doesn’t matter which one.
因为如果有一个是double类型数值
Because if one’s a double,
那么实际上这个运算会保存小数部分
then the operation that it will do will actually preserve the fractional part.
所以我们能做的就是
So what we can do,
就是对这里的数值进行类型转换
is actually cast, maybe the, the value here,
将(metascore+tomatometer)转成double类型数值
‘(metascore + tomatometer)’ to be a double value.
类型转换的方法是
And the way we can go about casting,
前面加上一个(double)
is just doing an ‘(double)’.
那么这里运算的
So what this effectively is saying is,
具体含义是
for this particular operation,
将这里的数据
make this stuff here,
转换成double类型
to be of type double.
那么只要有一个double类型数值存在
So whenever we have a double here,
double 噢 不好意思 是int
and then double, or excuse me, int here,
它会自动的做保存小数部分
it will automatically do a division operation,
的除法运算
so that it preserves the fractional part.
那么可以看到 结果对了
So we can see that, that works.
保存下
I’ll save this,
点击Build按钮
click on the ‘Build’ button,
然后再一次运行程序
and then we’ll run our programme again.
这次再一次输入电影名字
And this time the name of the movie, once again,
它是 The Incredibles
is going to be ‘The Incredibles’,
然后我们输入metascore 是90
and then we have the metascore rating, which was 90,
然后Tomatometer评分是97
and the Tomatometer rating, which was 97.
超人特工队的平均得分 调整为十分制后是9.35
So ‘Our calculated average in a 10 point scale for the Incredibles is 9.35.’.
这次得到的数值与预期结果一样了
So we get the expected result this time.
现在我强调一下 我们本来可以把2变成double类型
Now I did say that we could have also made this ‘2’ value a double.
这也是最容易的一种办法
And one of the easiest ways to make this ‘2’ value a double,
加个.0 变成2.0
is to just do,
就是这样了
‘.0’.
小数点 零
So the decimal point, zero.
这就是一个double类型文字
So this makes this a double literal.
任何时候在程序中 有一个数值
Anytime in our programme, where we have a value,
它有小数点
and it has decimal point,
编译器会自动的把它当成double数值
it’s automatically seen as a double value.
所以如果你想 你可以不必声明变量为double
So if you wanted to, you could get rid of this,
无论那是什么
it doesn’t matter that it’s there.
但是我们先不去管它
But we could get rid of that,
所以这里有一个double
and so you have a double here,
这里有一个int
and you have an int here.
既然有了int和double
And since you have and int and a double,
编译器会自动的提升它变成double
it’s going to automatically promote this to being a double,
执行double类型的除法
and perform double division,
因此这里就有小数部分了
so that we have a fractional part.
现在继续 保存 编译
So let’s go ahead and save that, build it,
然后运行
and then run it.
好吧 再一次 名字是The Incredibles
Alright, so again, the name is ‘The Incredibles’,
metascore评分是90
the metascore rating is 90,
Tomatometer得分是97
and the Tomatometer score is 97.
再一次 我们得到了相同的精确结果
And so again, we get the same exact result.
就是9.35 没错 这就是我们要的预期结果
We get 9.35, so, that was our expected result.
好的 这个程序里已经包含了很多知识点
Alright, so we’ve covered a lot of things in this programme here.
我们着手设计了一个算法 编写了伪代码
We’ve looked at devising an algorithm, writing pseudocode,
伪代码就是注解后面那里
the idea behind comments,
类型转换 整型除法计算
type conversion, also integer division
将一个特殊数值转换类型到另一个数值
casting, so being able to cast a particular value to another value.
在另一个程序中我们可能会讲更多运算
And we’ll probably talk more about casting in another programme.
嗯 我们已经看过一些了
So we’ve, we’ve seen a lot.
在下个程序中我们会做更多的演示
And we’ll do more in our next programme.

发表评论

译制信息
视频概述

double int double you

听录译者

positiveme

翻译译者

[B]Azrael

审核员

知易行难

视频来源

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

相关推荐