C++第二講

 
Part 1
Working with strings使用字符串
 
Review 上節課內容回顧
       在第0部份通過解析小程序,我門學習了C++的基本概念:
              註釋、標準頭文件、作用域、表達式、語句、字符串直接量、輸出。
 
註釋
v      “//”單行註釋符,多用於較短註釋,爲程序員優先採用
v      “/*”開始,“*/”結束,連續多行註釋或處理不想編譯的程序代碼
v      c(console)out.
 頭文件
v      header 頭文件:兩種
v      standard header 標準頭文件,如已學的iostream.h、string.h,其格式爲:
       #include <文件名.擴展名>
v      用戶自定義(編寫)的頭文件,文件名用用雙引號括起,其格式爲:
       #include “文件名.擴展名”
 
       在這一部分,繼續學習基本概念,通過使用字符串編寫簡單程序,學習聲明、初始化變量,進一步瞭解輸入和string庫。
本部分內容安排
1. Input 輸入
2. Framing a name 爲姓名裝框
3. Details 小結
 
1. Input   輸入
請閱讀以下修改版的 “Hello”程序
       // ask for a person's name, and greet the person
       #include <iostream>
       #include <string>
       int main()
       {
       // ask for the person‘s name 請輸入你的名字
       std::cout << "Please enter your first name: ";
       // read the name 讀名字
       std::string name;         // define name
       std::cin >> name;         // read into
       // write a greeting 寫賀語
       std::cout << "Hello, " << name << "!" << std::endl;
       return 0;
       }
此程序運行時
屏幕顯示:
               Please enter your first name:
輸入以下名字作爲響應:
               Kitty
程序將會輸出:
               Hello, Kitty!
程序解析 1
 
       // ask for a person's name, and greet the person
       #include <iostream>
       #include <string>
       int main()
       {
       // ask for the person‘s name 請輸入你的名字
       std::cout << "Please enter your first name: ";
       // read the name 讀名字
       std::string name        // define name
       std::cin >> name;         // read into
       // write a greeting 寫賀語
       std::cout << "Hello, " << name << "!" << std::endl;
       return 0;
       }
解析:name 變量variable,是具有名稱的對象object
爲讀入輸入,找個存放地方
 
std::string name
std”表示標準庫;string表示類型;name表示變量
Name:變量
Ø          變量:一個具有名稱的對象
Ø          對象:計算機中一段具有類型的內存空間
Ø          有些對象沒有名稱
Object, Name & Variable
An Object     不一定有     Name
Variable        一定有   Name
A Variable     是個有   Name的Object
v            變量name類型是std::string,使用字符串
  string是標準庫一部分,
  std::string 相關頭文件是<string>
v            定義在一個函數體內(在一對花括號內),是局部變量
v            局部變量僅存活在花括號括起的程序運行期內
v            程序運行至 },變量被銷燬、變量所佔內存釋放
v            局部變量有限的生存期是區分變量和對象的一個重要依據
 
定義變量方法
定義一個名爲name的string變量(具名對象)
後,能夠對變量name做庫允許的所有操作
 
格式:
              數據類型 變量名;
              數據類型 變量名,變量名,…,變量名;
 
如:       char C;       //定義字符變量 C
              int i,j;     //定義整型變量i, j
              float x,y,z,     //定義實型變量
A bit extra
       整數類型int的值是指在
              -32 768
到 32 767
       範圍之內的整數值。
 
程序解析 2
       // ask for a person's name, and greet the person
       #include <iostream>
       #include <string>
       int main()
       {
       // ask for the person‘s name 請輸入你的名字
       std::cout << "Please enter your first name: ";
       // read the name 讀名字
       std::string name;         // define name 定義變量
       std::cin >> name;         // read into 讀取到變量name、、、“
       // write a greeting 寫賀語
       std::cout << "Hello, " << name << "!" << std::endl;
       return 0;
       }
 
 
初始化 Initialize
              std::cin >> name; //把名字讀進name
v            標準庫要求每一個string對象都要有一個初始化值
v            在定義一個變量時,對它進行隱式初始化
v            在創建一個字符串時,
 可對它賦明確值
 若未賦值,則爲空字符串(不包含任何字符)
v            一個字符串還可以通過直接給它一串字符或另一個字符串變量來初始化
              std::string othername = " Kitty ";
              std::string name = othername;
語句分析-輸入
std::cin >> name; //把名字讀進name
庫使用 >>運算符std::cin 進行輸入
std::cin中輸入的值讀入變量name
 
v          用庫讀字符串:
             首先略去輸入開始時碰到的whitespace空白字符space空白, tab, backspace, or the end of the line
             然後連續讀字符name變量中
             直至whitespace空白字符或文件結束標記
v          表達式執行結果:讀輸入詞(字符串),再存儲到變量name
 
 
程序解析 3
 
       // ask for a person's name, and greet the person
       #include <iostream>
       #include <string>
       int main()
       {
       // ask for the person‘s name 請輸入你的名字
       std::cout << "Please enter your first name: ";
       // read the name 讀名字
       std::string name;         // define name 定義變量
       std::cin >> name;         // read into
       // write a greeting 寫賀語
       std::cout << "Hello, " << name << "!" << std::endl;
       return 0;
       }
語句分析-輸出
std::cout << "Hello, " << name << "!" << std::endl;
 
       1. 輸出字符串直接量“Hello,”
       2. 輸出字符串變量name的值
       3. 對std::endl的值寫操作,結束輸出行
       4. 刷新緩衝區buffer
       5. 系統立即向輸出流寫入數據
 
刷新緩衝區flashes the buffer
              庫使用緩衝區buffer來積累待寫的字符,這樣,他就能把幾個輸出操作合併到一個單獨的寫操作中了。endl 有刷新緩衝區作用。
 
2. Framing a name 爲姓名裝框
輸出分析
程序有五行長度相同的輸出,各行組成:
v            1st & 5th line:框架開始,一串 * 字符組成;其長度跟3rd line:名字、問候(Hello)、兩端空格、兩端各一個 * 字符
v            2nd & 4th line:頭尾兩端各一個 * 字符、適量空格
v            3rd line:頭尾兩端各一個 * 字符、空格,中間greeting 信息
 
 
Part 1 Pragram
 
1.1    輸入
 
// ask for a person's name, and greet the person
       #include <iostream>
       #include <string>
 
       int main()
       {
       // ask for the person's name 請輸入你的名字
       std::cout << "Please enter your first name: ";
 
       // read the name 讀名字
       std::string name;         // define name
       std::cin >> name;         // read into
 
       // write a greeting 寫賀語
       std::cout << "Hello, " << name << "!" << std::endl;
       return 0;
       }
 
 
1.2 爲姓名裝框
 
// ask for a person's name, and generate a framed greeting
#include <iostream>
#include <string>
 
int main()
{
    std::cout << "Please enter your first name: ";
    std::string name;
    std::cin >> name;
 
    // 3行,輸出的問候語
    const std::string greeting = "Hello, " + name + "!"; A
 
    
    //2&4行,輸出的空白爲主
    const std::string spaces(greeting.size(), ' ');
    const std::string second = "* " + spaces + " *";B
 
               
    //1&5行,輸出一連串 *
    const std::string first(second.size(), '*'); C
 
               
    // write it all 輸出所有內容
    std::cout << std::endl;
    std::cout << first << std::endl;
    std::cout << second << std::endl;
    std::cout << "* " << greeting << " *" << std::endl; D
 
    std::cout << second << std::endl;
    std::cout << first << std::endl;
    
    return 0;
}
 
程序A部分解析
      // 3rd line, 輸出問候語
       const std::string greeting = "Hello, " + name + "!";
 
const 把變量定義成常量
 
用“=“符號給變量賦值
 
用“+“符號連接字符串直接變量
 
 
 
       注意:若變量是常量,需在定義時初始化。
                      在此用 “=” 初始化。
 
若輸入“Kitty”, 以上語句的輸出將爲:
Hello, Kitty!
程序B部分表達式1解析
 
const std::string spaces(greeting.size(), ' ');
greeting 爲對象size()爲成員 ' '
greeting.size()調用成員函數
v            greeting.size(): 對象greeting有一個成員size
       成員size是個函數;
v            A部分已定義greeting爲std::string類型;
v            greeting包含的字符個數,即對greeting.size()求值產生的整數
v            字符直接量表示一個字符,由單括號括起
理解 std::string spaces(greeting.size(), ' ');
v            如果有定義:std::string stars (10, ‘*’);
  stars.size()的結果是10,即字符串stars的長度
  stars 本身包括10個 * 字符,即**********
 
*********************
*                   *
*     Hello, Kitty!  *
*                   *
*********************
 
第3行,greeting(不包括兩端 ‘*’)
第2&4行, spaces(不包括兩端 ‘*’)
 
 
 
v               spaces = greeting 字符個數
              且字符都爲空白,即 ‘ ’
 
 const std::string second = "* " + spaces + " *";
 
       second : “*”、空白字符串、“*”
       這就是第2 & 4 行的輸出
 
3. Details小結
1) string類型
v            定義於標準頭文件<string>
v            一個string類型的object對象包含一連串(0或多個)字符
char & string 類型
v            char       類型表示一個字符,用單引號 ‘ ’括起;
v             string   類型表示一串字符,用雙引號 “ ”括起
string類型數據操作
假如變量及類型爲:n:整數,c:char類型
對string類型數據操作包括:
std::string s;   把s定義爲類型爲std::string、初始爲空的變量。
std::string t=s; 把t定義爲類型爲std::string的變量,其初始值包含字符同s; s可爲一個字符串或字符串直接量。
std::string z(n,c); 定義變量z爲std::string類型,其初始化爲包含n個字符c的字符串。c必須是一個字符,不能是字符串或字符串直接量。
s+t 此表達式結果爲std::string 類型,包含的字符串前半部爲s中所有字符,後半部爲t中所有字符。
s.size()   表示s包含的字符個數。
2) 程序中變量定義的3種形式
1. 用明確的初始化值來定義變量:
       std::string hello=“Hello”;
 
2. 根據類型和給定的表達式來構造一個變量:
       std::string stars(100, ‘*’);
 
3. 定義一個變量,定義是不明確制訂它的初始化值。此變量的初始化值取決於它的類型。
       std::string name;
3)輸入
              執行std::cin>>v 表達式會忽略任何在標準輸入流中的空白字符,然後從標準輸入把數據讀入到變量v中。
              這個表達式會返回類型爲istream的std::cin,這樣,我們就可以進行鏈式的輸入操作。
             
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章