• 科普

SCIENCE

英语

ENGLISH

科技

TECHNOLOGY

MOVIE

FOOD

励志

INSPIRATIONS

社会

SOCIETY

TRAVEL

动物

ANIMALS

KIDS

卡通

CARTOON

计算机

COMPUTER

心理

PSYCHOLOGY

教育

EDUCATION

手工

HANDCRAFTS

趣闻

MYSTERIES

CAREER

GEEKS

时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

《卷轴教程之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,

and how do we go about writing those comments in our code,

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.

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,

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,

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,

and then we’ll implement the actual C++ code.

So let’s get started.

Ok, so let’s go ahead and get started with ‘Program3’.

So we’ll come here to ‘File’, ‘New’,

and then come across here to ‘C++ Project’,

left click,

and we’ll name this project ‘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,

and then click on ‘Finish’.

Ok, so we’ve got our ‘Program3’ project folder,

we can right click on that,

then go here to ‘New’,

and then left click on, ‘Source File’.

So I want to create a C++ source file,

and we’ll just name it ‘Program3.cpp’,

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.

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.

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.

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.

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,

one from metacritic’s, the Metascore,

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.

And then we also need to get the Metascore.

So I’ll say, ‘Get the Metascore’.

And then we need to get the other score,

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,

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,

so let’s include ‘string’,

and let’s also do the ‘using’,
‘namespace’
‘namespace’,
‘std’ 这样就省去了一些代码
‘standard’, save us a few keystrokes.

And now we’re ready to start typing in the main function,

so ‘int main()’,

And then I’ll put an ‘{‘ here,

and then come down below,

and put a ‘}’.

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.

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,

a variable of type string,

and we’ll call it ‘movie’,

and we’ll set it to the empty string.

Ok, and then,

we’ll display to the user, using our ‘cout’,

‘What is the name of the movie?’
‘What is the name of the movie?’.

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,

is just use ‘cin’ and then the ‘>>’,

and then specify the name of the variable,

where we want to store that input.

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,

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.

So we can just say ‘cin’,

and then where do we want to store it to,

so that’s the second argument.

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,

and get the Metascore,

so the Metascore is going to be an integer value.

So,

we’ll just declare an integer variable, ‘int’,

and we’ll just say,

‘metascore’ is the new…the variable.

And we’ll initialise that to zero.

And then we’ll have some output to the user.

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,

use our variable called ‘movie’,

and then, maybe ‘?\n’ for a newline character.

Ok, so this is just the prompt that we’re going to have,

then the input, the…the rating from Metascore.

And then once I’ve done that, we can then read in that value.

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,

so we just type in ‘metascore’, here.

You ok?

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.

So we’ll say ‘int’,
‘tomatometer’
‘tomatometer’,

and we’ll assign that to zero.

And then we’ll do ‘cout’,
‘<<'
use the ‘<<',

and we’ll say,
‘What is the tomato’
‘What is the tomato’,

let’s capitalise that ‘Tomatometer’,
‘rating for’
‘rating for’,

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,

so ‘cin >>’,

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.

positiveme

HX