Learn cpp with me(based on C++ Primer Plus)

0.Introduction

  I need cpp, so I am now learning cpp.
  I found that <C++ Primer Plus> is a good book since it not only just teach me how to code but also tells me about the theory of how computer works, which can help me understand other computer and electrical systems such as ROS which I am learning in the meantime.
  I will write this blog in English so that I can get closer to the original meanings and the essentials of some terms.
  Concepts have been marked in the book, I would just paste some practicing source codes and take note of my own conclusion.
  No more nonsense, let’s start our learning journey.

1.Getting started with C++

2.Setting out to C++

Firstly, use cpp grammar to print hello world:

//display a two-line message
#include <iostream>
using namespace std;

int main()
{
    
    
    cout << "Hello World!" << endl << "Welcome to cpp space." << endl;
}

Do it in a different way:

//display a two-line message
#include <iostream>

int main()
{
    
    
    std::cout << "Hello World!" << std::endl;
    std::cout << "Welcome to cpp space." << std::endl;
}

Add an input and deal with a value

//add two carrots
#include <iostream>
using namespace std;

int main()
{
    
    
   int carrots;
   cout << "How many carrots do you have?" << endl;
   cin >> carrots;
   cout << "You will be given two more." << endl;
   carrots = carrots + 2;
   cout << "So now you have " << carrots << " carrots" << endl;
}

Define a function

//define a function
#include <iostream>
using namespace std;

void say(int);

int main()
{
    
    
    int number;
    cin >> number;
    say(number);
}

void say(int n)
{
    
    
    cout << "I want to read " << n << " books in the holiday!" << endl;
}

Define a function with a return value
(some lines are not necessary but can make the codes more readable)

//convert stone to pounds
#include <iostream>
using namespace std;

int convert(int);

int main()
{
    
    
    int stone,pounds;
    cout << "Type in the number of stones" << endl;
    cin >> stone;
    pounds = convert(stone);
    cout << pounds << endl;
}

int convert(int stone)
{
    
    
    int pounds;
    pounds = stone * 14;
    return pounds;
}

Exercise 3

//define two functions, just to print
#include <iostream>
using namespace std;

void sent1();
void sent2();

int main()
{
    
    
    sent1();
    sent1();
    sent2();
    sent2();
}

void sent1()
{
    
    
    cout << "Three blind mice" << endl;
}

void sent2()
{
    
    
    cout << "See how they run" << endl;
}

Exercise 5

//temperature conversion
#include <iostream>
using namespace std;

double convert(double);

int main()
{
    
    
    double CelsTemp,FahrTemp;
    cout << "Enter the Celsius temperature" << endl;
    cin >> CelsTemp;
    FahrTemp = convert(CelsTemp);
    cout << "The Fahrenheit value is " << FahrTemp << endl;
}

double convert(double C)
{
    
    
    double F;
    F = 1.8*C+32.0;
    return F;
}

Exercise 7

//time display
#include <iostream>
using namespace std;

void prttime(int,int);

int main()
{
    
    
    int hour,minute;
    cout << "Enter the number of hours: ";
    cin >> hour;
    cout << "Enter the number of minutes: ";
    cin >> minute;
    prttime(hour,minute);
}

void prttime(int hour,int minute)
{
    
    
    cout << "Time is " << hour << ":" << minute << endl;
}

3.Dealing with data

Using sizeof to check the space a variable type takes

//size of data
#include <iostream>
#include <climits>

int main()
{
    
    
    using namespace std;

    int n_int;
    long n_long;

    cout << "size of int is " << sizeof(int) << " bytes" << endl;
    cout << "size of long is " << sizeof n_long << " bytes" << endl;
}
size of int is 4 bytes
size of long is 8 bytes

Check the maximum number a variable can contain

//size of data
#include <iostream>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    
    
  using namespace std;

  cout << SHRT_MAX << endl;
	cout << INT_MAX << endl;
	cout << LONG_MAX << endl;
	cout << LLONG_MAX << endl;
  cout << DBL_MAX << endl;
  cout << FLT_MAX << endl;
  cout << LDBL_MAX << endl;
}
2147483647
9223372036854775807
9223372036854775807
1.79769e+308
3.40282e+38
1.18973e+4932

Play with integer literals:

//practicing decimal,octal and hex literals
#include <iostream>

int main()
{
    
    
    using namespace std;

    int dec = 81;
    int oct = 0121;
    int hex = 0x51;

    cout << dec << endl;
    cout << oct << endl;
    cout << hex << endl;
}
81
81
81

Reversion:

//practicing decimal,octal and hex literals
#include <iostream>

int main()
{
    
    
    using namespace std;

    int aNum = 42;

    cout << aNum << endl;
    cout << oct;
    cout << aNum << endl;
    cout << hex;
    cout << aNum << endl;
}
42
52
2a

Get to know char:

//tasting char
#include <iostream>

int main()
{
    
    
    using namespace std;

    char ch;
    cin >> ch;
    cout << "the character you typed in is " << ch << endl;
}

To know about the char and ASCII code:

//tasting char and dealing with ASCII code
#include <iostream>

int main()
{
    
    
    using namespace std;

    char ch = 'M';
    int code = ch;
    cout << "The ASCII code of M is " << code << endl;

    cout << "Now modify the ASCII code by adding 1\n";
    ch = ch + 1;
    code = ch;
    cout << "The character becomes " << ch << " now\n";
    cout << "And the ASCII code for " << ch << " is " << code << endl;
}
The ASCII code of M is 77
Now modify the ASCII code by adding 1
The character becomes N now
And the ASCII code for N is 78

Exercise1

//height conversion
#include <iostream>
using namespace std;

void convert(int);

int main()
{
    
    
    cout << "Please enter you heigh in inch:___\b\b\b";

    int height_ver1;
    cin >> height_ver1;

    convert(height_ver1);   
}

void convert(int height_ver1)
{
    
    
    const int CONVERSION_FACTOR = 12;
    int inch;
    int feet;
    feet = height_ver1/CONVERSION_FACTOR;
    inch = height_ver1%CONVERSION_FACTOR;
    
    cout << "Your height is: " << feet << " feet " << inch << " inches\n";
}

在这里插入图片描述
Take note of a technique: press Alt to chose several discontinuous texts
在这里插入图片描述
Exercise 4

//time conversion
#include <iostream>
using namespace std;

int get_quotient(int,int);
int get_remainder(int,int);

int main()
{
    
    
    cout << "Please enter the number of seconds:________\b\b\b\b\b\b\b\b";
    long long seconds;
    cin >> seconds;
    
    long days;
    days = get_quotient(seconds,24*60*60);
    seconds = get_remainder(seconds,24*60*60);
    
    int hours;
    hours = get_quotient(seconds,60*60);
    seconds = get_remainder(seconds,60*60);
    
    int minutes;
    minutes = get_quotient(seconds,60);
    seconds = get_remainder(seconds,60);

    cout << "The time is " << days << "days, ";
    cout << hours << "hours, ";
    cout << minutes << "minutes, ";
    cout << seconds << "seconds, " << endl;
}

int get_quotient(int divisor,int dividend)
{
    
    
    int qutient;
    qutient = divisor/dividend;
    return qutient;
}

int get_remainder(int divisor,int dividend)
{
    
    
    int remainder;
    remainder = divisor%dividend;
    return remainder;
}

在这里插入图片描述
To unify my naming style,
when the name is a noun phase, I will use camel-case like myName, usually in a variable name;
when the name is a verb phase, I will use underscore-case like get_remainder, usually in a function name.,.

4. Compound types

Get to know about array:

//get to know about array
#include <iostream>
using namespace std;

int main()
{
    
    
    int yam[4] = {
    
    8,1,10,11};

    int total;
    total = yam[0]+yam[1]+yam[2]+yam[3];
    cout << "The total number is " << total << endl;

    cout << "The size of the array is " << sizeof yam << endl;
    cout << "The size of the array is " << sizeof yam[0] << endl;
}

About character,there is something to notice:
here is a comparison:

#include <iostream>
using namespace std;

int main()
{
    
    
    char ch = 'M';
    ch = ch + 1;
    cout << ch;
}

在这里插入图片描述

#include <iostream>
using namespace std;

int main()
{
    
    
    char ch = 'M';
    cout << ch + 1;
}

在这里插入图片描述
The cout function has its own ideas
Next, take a look at string:

//the number of letters, the size, the first several letters of a string
//storing strings in an array
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    
    
    const int SIZE = 10;
    char name1[SIZE] = "jame";
    char name2[SIZE];

    cout << "my name is " << name1 << endl;
    cout << "what is your name?" << endl;

    cin >> name2;

    cout << "your name has " << strlen(name2) << " letters\n";
    cout << "it is an array of " << sizeof(name2) << " bytes\n";
    cout << "the first letter of your name is " << name2[0] << endl;
    name2[2] = '\0';
    cout << "the first two letters of your name are " << name2 << endl;
}

在这里插入图片描述
Take a close look at cin:
If I do it in an in an intuitive way:

//play with cin
#include <iostream>
using namespace std;

int main()
{
    
    
    int strSize = 10;
    char name[strSize];
    char dessert[strSize];
    
    cout << "Please enter your name\n";
    cin >> name;
    cout << "Please enter the dessert you want\n";
    cin >> dessert;
    
    cout << name << ", here is your " << dessert;
}
Please enter your name
jame lanny
Please enter the dessert you want
jame, here is your lanny[1] + Done 

Not so good (

The basic theory is explained in the book, to be brief, the empty space is regarded as the end of a single input.
So we need getline() or get() , which are line-oriented, and they will read input until a newline character.

//play with cin
#include <iostream>
using namespace std;

int main()
{
    
    
    int strSize = 30;
    char name[strSize];
    char dessert[strSize];
    
    cout << "Please enter your name\n";
    cin.getline(name,strSize);
    cout << "Please enter the dessert you want\n";
    cin.getline(dessert,strSize);
    
    cout << name << ", here is your " << dessert;
}

Getline is a member function of the istream class
Let’s take look at another member function get()

//learn to use get()
#include <iostream>
using namespace std;

int main()
{
    
    
    int strSize = 30;
    char name[strSize];
    char dessert[strSize];
    
    cout << "Please enter your name\n";
    cin.get(name,strSize).get();//concatenate member functions to read a new line
    cout << "Please enter the dessert you want\n";
    cin.get(dessert,strSize).get();
    
    cout << name << ", here is your " << dessert;
}

在这里插入图片描述
Mixing string and numeric input:

//read a numeric input and a string input
#include <iostream>
using namespace std;

int main()
{
    
    
    cout << "Please enter the built year of your house:\n";
    int year;
    (cin >> year).get();//remember this natation and what it does

    cout << "Please enter the address of your house:\n";
    char address[30];
    cin.get(address,30);

    cout << "balabala " << year <<" "<< address << endl;
}

Please enter the built year of your house:
1984
Please enter the address of your house:
Near the St.James' Park
balabala 1984 Near the St.James' Park

Take a look ant string type:

//play with string
//assigning, adding, and appending
#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    string s1 = "magic";
    string s2, s3;

    cout << "please make s2 something ";
    cin >> s2;

    s3 = s1;     //assign
    cout << s3 << endl;

    s3 = s1 + s2;//add
    cout << s3 << endl;

    s1 += s2;    //append
    cout << s1 << endl;
}
please make s2 something man
magic
magicman
magicman

More string class operations

strcpy(char1, char2) //copy
strcat(char1, char2) //catenate
mainly used in C-style strings

Here are several comparisons of C-style string and a string object

//explore the differences between C-style string and the string class
//use strcpy and strcat to copy and catenate
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    
    
    char ch1[20];
    char ch2[] = "magic";

    string str1;
    string str2 = "jackson";

    //assign
    strcpy(ch1,ch2);
    str1 = str2;
    cout << ch1 << str1 << endl;

    //append
    strcpy(ch1,"man");//here we cannot use "=" to assign the string
    str1 = "wang";
    strcat(ch2,ch1);
    str2 += str1;
    cout << ch2 << endl << str2 << endl;
}
magicjackson
magicman
jacksonwang

strlen str.size()

//measuring the length
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    
    
    string str = "magic";
    char ch[]= "man";

    int len1 = str.size();
    //int len2 = ch[].size();//INVALID
    //int len3 = strlen(str);//INVALID
    int len4 = strlen(ch);

    cout << len1 << endl
    //<< len2 << endl
    //<< len3 << endl
    << len4 << endl;
}

structure

//have fun with structure
#include <iostream>
using namespace std;

struct robot
{
    
    
    char name[10];
    int DOF;
};


int main()
{
    
    
    robot peter =
    {
    
    
        "Peter",
        40
    };                           //initialize one value per line
    
    robot parker = {
    
    "Parker",45};//we can also place them in one line

    cout << "full name is " << peter.name << parker.name << endl;
    cout << "total DOF is " << peter.DOF+parker.DOF << endl;
}
full name is PeterParker
total DOF is 85

Arrays of structures

//structures as elements of an array
#include <iostream>
using namespace std;

struct robot
{
    
    
    char name[10];
    int DOF;
};


int main()
{
    
    
    robot arr[2] = 
    {
    
    
        {
    
    "peter",40},
        {
    
    "parker",45}
    };

    cout << "full name is " << arr[0].name << arr[1].name << endl;
    cout << "total DOF is " << arr[0].DOF+arr[1].DOF << endl;
}

猜你喜欢

转载自blog.csdn.net/m0_73293161/article/details/128585170