學習 C++

A. 概述

A1. David Aksnes 是誰?

這是 的第一篇 CSDN 的博客,是用 MarkDown 格式寫的,我也順便在這裏練習如何用 MarkDown 格式寫博客。
請先讓我自我介紹一下:
David Aksnes 是一個 計算機科學本科學位 還沒有畢業就退學了的學生。對計算機科學的認識也只能從 這個視頻 裏認識。 在脫離了嚴格的大學體系下去學習計算機科學,我就只能按照我的理解來學習了,那就是計算機,編程和網絡。其中,編程能力我認爲是最重要的技能。

A2. 這篇博客是在記錄什麼?

這是一個記錄學習 CPP 過程的博客,同時也會 表達我對這個過程中遇到的技術的看法 。這篇博客主要記錄編程能力其中一種語言 C++ 的學習。 這個博客其他的文章也會記錄下學習計算機本身,和網絡的文章。今天既然挖了這麼多坑,之後也有理由去填,去學了。同時,我也會在這裏把看書做的練習記錄下來,方便和我一起看 同一本書 的人對對答案。我也在 github 上找到這本書的答案

當然也有另外有一篇博客文章會專門記錄工作中遇到的問題。畢竟不記錄下來什麼,之後就什麼都忘記了。這個博客之後再開設吧。

這個文章每天都會更新。如果覺得對你有幫助的話,可以關注我,和寫下你的評論。1

希望 CSDN 不會倒閉吧。我還是定期在本地備份一下好了。

B. 目的

現在學習 CPP 的目的是熟悉這種語言的基本知識。之後會按照 Project Base Learning (PBL) 的方法學習。
依照我的查找的資料,我會按照接下來這個表格來學習:

B1. 項目記錄表

項目目的工具
C++ Primer掌握 C++ 語言的基本知識
Qt掌握 GUI 界面Qt Creator
  • 如果你有更好的推介,請告訴我一下,謝謝你。

C. 進度

Fri 12 12:00 Sat 13 12:00 Apr 14 12:00 Mon 15 12:00 Tue 16 12:00 Wed 17 開始 1.1 到 1.6 開始 -> 2.1 Preface Chapter One Chapter Two C++ Primer

D. 筆記

2019年4月

12日

  • 00:48 am
    開始第一天的工作,完成藍圖之後的第一步就是先睡覺。甘特圖還要多從 這裏 學習,不然寫個博客都不知道怎麼寫了。
  • 08:21 am
    現在可以趁着醒了,但是還沒上班,花半小時把 Preface 看完。
Preface:

auto => for type interface.

  • 之後需要學習 data structuresalgorithms
  • Library 好重要,之後可以自己寫嗎?
  • Generic Programming 是什麼?
  • LATEX 真好用,之後也要會。

花15分鍾就看完了,甘特圖似乎可以精確到分鍾和小時,有機會一定要知道怎樣用。

  • 11:30 pm 開始看第一章
Chapter 1:

1.1 Writing a Simple C++ Program

CPP文件基本結構:

int main(): // parameter list & int: built-in type
{
	return 0; // 0 -> status indicator
}

Program files are normally referred to as a source files.

compile cpp 文件 (From a.cpp to a)

g++ -o a a.cpp # compile a.cpp (source file) to a (executable file)
./a # run the cpp executable file
Exercise 1.1:

以下 file naming convention 適用於 Ubuntu 16.04.6

g++ -o a a.cpp -Wall
Exercise 1.2:

Recompile 以下 CPP source file 什麼結果也沒有 echo 出來

int main()
{
	return -1;
}

1.2 A First Look at Input/Output

iostream library 是常用庫:

iostream library
istream
ostream

A stream is a sequence of characters read from orwritten to an IO device.

iostream library
cin // input
cout // output
cerror // output
clog // output

例子:

#include <iostream> // header
int main()
{
	std::cout << "Enter two numbers: " << std::endl; // expression & string literal
	int v1, v2;
	std::cin >> v1 >> v2;
	std::cout << "The sum of " << v1 << " and " << v2 <<
			  << " is " << v1 + v2 << std::endl;
	return 0; 
}

String literal: a sequence of characters enclosed in double quotation marks.
end: manipulator for flushing the buffer.
std: namespace for 避免命名衝突

Exercise 1.3
#incldue <iostream>
int main()
{
	std::cout << "Hello, World" << std::endl;
	return 0;
}

Output:

Hello, world
Exercise 1.4
#include <iostream>
int main()
{
	std::cout << "Enter two numbers: " << std::endl;
	int v1, v2;
	std::cin >> v1 >> v2;
	std::cout << "The product of " << v1 << " and " << v2
	          << " is " << v1 * v2 << std::endl;
	return 0;
}

Output:

Enter two numbers:
>> 3 5
The product of 3 and 5 is 12
Exercise 1.5
#include <iostream>
int main()
{
	std::cout << "Enter two numbers: " << std::endl;
	int v1, v2;
	std::cin >> v1 >> v2;
	std::cout << "The product of " << std::endl;
	std::cout << v1 << std::endl;
	std::cout << " and " << std:endl;
	std::cout << v2 << std::endl;
	std::cout << " is " << std::end;
	std::cout << v1 * v2 << std::endl;
	return 0;
}

其實我也不知道這樣子算不算 “separate statement to print each operand”。之後有機會找到答案就對一下吧。

Exercise 1.6
std::cout << "The sum of " << v1;
          << " and " << v2;
          << " is " << v1 + v2 << std::endl;

這個 program 是 illegal 的,因爲第二行的 operand 已經被第一行的 semicolon 隔開。沒有了 std::cout,除了第一行的內容都不能打印出來了。

Fixed:

std::cout << "The sum of " << v1
          << " and " << v2
          << " is " << v1 + v2 << std::endl;

1.3 A Word about Comments

// 單行注釋
/*
 * 多行注釋
 */

當想 comment out 的代碼裏有 /* / 的話,就應該用 // 在 / 之前,把它注釋掉。

/ /*
Exercise 1.7

會出現一下錯誤:

error: expected primary-expression before ‘/’ token
      */
       ^
Exercise 1.8

我的答案:
第一,二,四行沒有問題,第三行出現問題,因爲沒有被注釋掉的 " 沒有與之對應的 "。
Compiled 結果:

warning: missing terminating " character
     std::cout << /* "*/" */;
                        ^

13日

  • 01:13 am 睡覺了,明天早上繼續吧。進度很慢。要趕快了。
  • 10:20 am 繼續看。

1.4. Flow of Control

A while statement repeatedly executes a section of code so long as a given condition
is true.

while (condition)
	statement

A condition is an expression that yields a result that is either true or false.
A block is a sequence of zero or more statements enclosed by curly braces.

Exercise 1.9
int main()
{
    int sum = 0, val = 50;
    while (val <= 100) {
        sum += val;
        ++val;
    }
    std::cout << "Sum of 50 to 100 inclusive is "
              << sum << std::endl;
}
# Output:
Sum of 50 to 100 inclusive is 3825
Exercise 1.10
#include <iostream>
int main()
{
    int num = 10;
    while (num >= 0) {
        std::cout << num << std::endl;
        num--;
    }
}
# Output:
10
9
8
7
6
5
4
3
2
1
0
Exercise 1.11
#include <iostream>
int main()
{
    std::cout << "Enter two integers: " << std::endl;
    int num1, num2;
    std::cin >> num1 >> num2;

    // dividing num1 is greater thatn num2 and vice versa
    if (num1 <= num2) {
        while (num1 <= num2) {
            std::cout << num1 << std::endl;
            num1++;
        }
    } else {
        while (num2 <= num1) {
            std::cout << num2 << std::endl;
            num2++;
        }
    }

    return 0;
}
# Output:
# 1.
Enter two integers:
1 3         
1                             
2                                          
3  
# 2.
Enter two integers:
5 1
1            
2
3
4
5

val++++val 有什麼不同?
回答: 一樣

Exercise 1.12

這段代碼打印出從 -100 加到 100 的和。

Exercise 1.13
#include <iostream>
int main()
{
    int sum = 0;
    for (int num = 50; num <= 100; ++num) {
        sum += num;
    }
    std::cout << "Sum of 50 to 100 inclusive is "
              << sum << std::endl;
    return 0;
}
#include <iostream>
int main()
{
    int sum = 0;
    for (int num = 10; num >= 0; --num) {
        std::cout << num << std::endl;
    }
    return 0;
}
#include <iostream>
int main()
{
    std::cout << "Enter two integers: " << std::endl;
    int num1, num2;
    std::cin >> num1 >> num2;

    if (num1 <= num2) {
        for (; num1 <= num2; ++num1) {
            std::cout << num1 << std::endl;
        }
    } else {
        for (; num2 <= num1; ++num2) {
            std::cout << num2 << std::endl;
        }
    }

    return 0;
}
Exercise 1.14

使用 for loop 可以控制範圍, 但是 condition 部分就收到限制;
使用 while loop 就簡單暴力,可是當 condition 部分簡單的時候,就有點多餘了。

Exercise 1.15

Often a single error canhave a cascading effect and cause a compiler to report more errors than
actually are present. It is also a good idea to recompile the code after each fix—or after making at most a small number of obvious fixes. This cycle is known as edit-compile-debug.

#include <iostream>
int main()
{
    int num      // syntax error 缺少 ; 語法錯誤
    int "hello"; //type error 類型不匹配
    val = 1;     // declaration error 使用 val 前沒有 declarate
    
    return 0;
}
  • 11:45 am 休息一下
  • 繼續。
Exercise 1.16
#include <iostream>
int main()
{
    std::cout << "Enter the integers:" << std::endl;
    std::cout << "Enter Ctrl + D for showing the result." << std::endl;
    int sum = 0,
        val = 0;
    while (std::cin >> val) {
        sum += val;
    }
    std::cout << "Sum is " << sum << std::endl;
    return 0;
}

用來說明 if statement 的例子有些奇怪,要手動 Ctrl + D 才能結束。
這也是一個錯誤的數數字出現次數的程序。

if (std::cin >> currVal){ // 不明白這個意思
	...
} 

解釋

Exercise 1.17 & 1.18

因爲本節程序是錯的,問題不回答了。

Exercise 1.19

看不明題目。。。

Skip 1.5. Introducing Classes & 1.6. The Bookstore Program

Skip: Exercise 1.20 to 1.25

没有他的网上版本,看不了 Sales_item.h。在另外的地方學 class ,或者這只是開始作一個總的介紹,之後會詳細介紹 class。本章剩下的部分會看,可是練習現在就不做了。

Chapter 2. Variables and Basic Types

2.1. Primitive Built-in Types

Primitive types
arithmetic types
void
integral types
characters
integers
boolean values
floating-point numbers
  • The type determines how many bits are used and how to interpret those bits.
  • A signed type represents negative or positive numbers (including zero);
  • An unsigned type represents only values greater than or equal to zero.
  • 知道不會是負數的時候用 unsigned。
  • char 和 bool 都是用來存儲 True or False 的
  • 浮點數就用 double
Exercise 2.1

int 佔用的 bit 只有 16,long 是 32 bits,long long 是 64 bits, short 是 16 bits;Signed 的數字範圍可以是正數和負數,Unsigned 只有非負數;float 是有 6 個有效數字,而 double 則有 10 個。

Exercise 2.2

當要計算 按揭付款 時:

  • rate 用 double,因爲 rate 的範圍主要是 1 到 0;
  • principal 和 payment 用 double,因爲數字較大,而且含有小數部分。
Exercise 2.3
32
4562534574
32
-32
0
0
#include <iostream>
int main()
{
    unsigned u = 10, u2 = 42;
    std::cout << u - u2 << std::endl;
    std::cout << u2 - u << std::endl;

    int i = 10, i2 = 42;
    std::cout << i - i2 << std::endl;
    std::cout << i2 - i << std::endl;

    std::cout << u - i << std::endl;
    std::cout << i - u << std::endl;
    return 0;
}
# Output
4294967264
32
-32
32
0
0
  • Floating-point number: 3.14159E0
  • A character enclosed within single quotes is a literal of type char
  • Zero or morecharacters enclosed in double quotation marks is a string literal
  • The type of a string literal is array of constant chars with the null character
Exercise 2.5

答案

(a1): ‘a’: one char
(a2): L’a’: wide character literal, type is wchar_t
(a3): “a”: one string
(a4): L"a": long string? 正確答案:wchar_t[2]
(b1): 10: integer
(b2): 10u: unsigned integer
(b3): 10L: long integer
(b4): 10uL: unsigned long integer
(b5): 012: 八進制數字
(b6) 0xC: 十六進制數字
(c1): 3.14: float point number
(c2): 3.14f: float point number
(c3): 3.14L: long float point number
(d1): 10: integer
(d2): 10u: unsigned integer
(d3): 10.: float point number
(d4): 10e-2: float point number

Exercise 2.6

第一行是十進制數字
第二行是八進制數字

Exercise 2.7
  • 11:02 pm 休息了。

  1. 謝謝你的關注和評論。 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值