Microsoft PowerPoint - Class5.pptx

Similar documents
c_cpp

Microsoft PowerPoint - Class4.pptx

untitled

untitled

C/C++基礎程式設計班

ebook39-5

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

C/C++基礎程式設計班

迅速在两个含有大量数据的文件中寻找相同的数据

新版 明解C++入門編

提问袁小兵:

Strings

C++ 程式設計

ebook39-6

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

全国计算机技术与软件专业技术资格(水平)考试

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

untitled

Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien

踏出C++的第一步

<4D F736F F D205BCAE9B0FCCDF85DC8FDC9FAC8FDCAC0CAAEC0EFCCD2BBA82E646F6378>

<4D F736F F F696E74202D FB5F8B3A5A142B8EAAEC6B6C7BBBCA142BB50C0C9AED7BEDEA7402E >

ebook55-13

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

Microsoft PowerPoint - CH07 Arrays and Vectors [相容模式]

第七讲 继承与多态

運算子多載 Operator Overloading

Microsoft PowerPoint - STU_C_Lang_CH13.ppt

資料結構之C語言重點複習

( 总 第 1073 期 ) 浙 江 省 人 民 政 府 主 办 2015 年 3 月 17 日 出 版 省 政 府 令 省 政 府 文 件 目 录 浙 江 省 大 型 群 众 性 活 动 安 全 管 理 办 法 ( 浙 江 省 人 民 政 府 令 第 333 号 ) (3) 浙 江 省 人 民 政

2015 年 度 收 入 支 出 决 算 总 表 单 位 名 称 : 北 京 市 朝 阳 区 卫 生 局 单 位 : 万 元 收 入 支 出 项 目 决 算 数 项 目 ( 按 功 能 分 类 ) 决 算 数 一 财 政 拨 款 一 一 般 公 共 服 务 支 出 二

目 录 第 一 部 分 档 案 局 概 况 一 主 要 职 责 二 部 门 决 算 单 位 构 成 第 二 部 分 档 案 局 2016 年 度 部 门 预 算 表 一 2016 年 度 市 级 部 门 收 支 预 算 总 表 二 2016 年 度 市 级 部 门 支 出 预 算 表 三 2016

Strings


書本介紹

Microsoft PowerPoint - L17_Inheritance_v4.pptx


Microsoft PowerPoint - Class2.pptx

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

FY.DOC

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

第 一 节 认 识 自 我 的 意 义 一 个 人 只 有 认 识 自 我, 才 能 够 正 确 地 认 识 到 自 己 的 优 劣 势, 找 出 自 己 的 职 业 亮 点, 为 自 己 的 顺 利 求 职 推 波 助 澜 ; 一 个 人 只 有 认 识 自 我, 才 能 在 求 职 中 保 持

1.5招募说明书(草案)

untitled

untitled

Microsoft PowerPoint - 04-array_pointer.ppt

1

Microsoft PowerPoint - string_kruse [兼容模式]

Microsoft PowerPoint - 13_指標、資料傳遞2.pptx

Microsoft PowerPoint - 10 模板 Template.pptx

untitled

投影片 1

untitled

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

v

CC213

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

2

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

untitled

The golden pins of the PCI card can be oxidized after months or years

Microsoft Word - 01.DOC

Oracle 4

Open topic Bellman-Ford算法与负环

105A 資管一程式設計實驗 06 函式定義謝明哲老師 2 程式設計實驗 6.3: 自行定義一個可以接受兩個整數並傳回其最大公因數的函式, 接著利用該函式自 行定義一個可以接受兩個整數並傳回其最小公倍數函式 // gcd_fcn.cpp int gcd(int m,

EJB-Programming-4-cn.doc

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2

C/C++语言 - C/C++数据

Microsoft Word cppFinalSolution.doc

第3章.doc

Chapter12 Derived Classes

C语言的应用.PDF

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

技 巧 5: 避 免 除 以 0 的 運 算 在 做 除 的 運 算 時, 先 檢 查 除 數 的 數 值, 避 免 有 除 以 0 的 情 況 若 運 算 中 除 數 為 0,SAS 會 在 LOG 中 註 記 提 醒 並 將 運 算 結 果 設 定 為 遺 漏 值, 減 慢 程 式 的 執 行

陣列與鏈結串列 Array and Linked List

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

Microsoft Word - data_mid1611_and_sol.docx

概述

51 C 51 isp 10 C PCB C C C C KEIL

運算子多載 Operator Overloading

Microsoft Word - part doc

chp6.ppt

Outline USB Application Requirements Variable Definition Communications Code for VB Code for Keil C Practice

f2.eps

2/14 Buffer I12, /* x=2, buffer = I 1 2 */ Buffer I243, /* x=34, buffer = I 2 43 */ x=56, buffer = I243 Buffer I243I265 code_int(int x, char *buffer)

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

PowerPoint Presentation

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

PowerPoint Presentation

ebook70-5

untitled

Microsoft Word 管理學

v

epub83-1

epub 61-2

Microsoft Word - CPE會議紀錄151022

Strings

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

WinMDI 28

Transcription:

C++ 程式初探 V 2015 暑期 ver. 1.0.1

C++ 程式語言 大綱 1. 大量檔案讀取 & 計算 2. 指標 3. 動態記憶體 & 動態陣列 4. 標準函式庫 (STL) vector, algorithm 5. 結構與類別 2

大量檔案讀取 & 計算 若目前有一個程式將讀取純文字文件 (.txt) 中的整數, 並將該文件中的整數有小到大排序後, 儲存到另外一個新的純文字件中 假設有 20 個純文字文件, 每個文件格式包含兩項資訊為 : 1. 整數數量大小 (size, 已知最大數量為 300) 2. 欲排序之整數 (data) 3

大量檔案讀取 & 計算 給定最大陣列 #include<iostream> #include<fstream> #include<algorithm> const int maxsize = 300; int size; int data[maxsize]; size maxsize 1 2 3 4 5 6 0 0 0 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[298] a[299] ifstream sinf("sdata\\sdata00000.txt"); sinf>>size; //dynamic size is determined by reading file for(int i=0; i<size; i++) sinf>>data[i]; //sorting sort(data, data+size); //output on the screen for(int i=0; i<size; i++) cout<<data[i]<<" "; 4

大量檔案讀取 & 計算 產生動態的檔名 #include<iostream> #include<fstream> #include<sstream> #include<iomanip> #include<algorithm> const int maxsize = 300; int size; int data[maxsize]; int filenum = 20; for(int n=0; n<filenum; n++) // set the name rule for reading file stringstream ifname; //give the path as "sdata\\sdata00000.txt" ifname<<"sdata\\sdata"<<setw(5)<<setfill('0')<<n<<".txt"; 引入標頭檔 #include<sstream> sstream 物件 stringstream sstream 物件使用 sstr; sstr<<"str"<<36<<var; //read file ifstream sinf(ifname.str().c_str()); sstream 物件轉換為 c style 字串 sstr.str().c_str(); 5

大量檔案讀取 & 計算 產生動態的檔名 #include<iostream> #include<fstream> #include<sstream> #include<iomanip> #include<algorithm> #include<direct.h> const int maxsize = 300; int size; int data[maxsize]; int filenum = 20; 引入標頭檔 VC++ #include<direct.h> mkdir 函式 mkdir("folder_name"); mkdir("result"); //create new folder for(int n=0; n<filenum; n++) // set the name rule for reading file stringstream ifname; //give the path as "sdata\\sdata00000.txt" ifname<<"sdata\\sdata"<<setw(5)<<setfill('0')<<n<<".txt"; //read file ifstream sinf(ifname.str().c_str()); // 接續下一頁... 6

大量檔案讀取 & 計算 產生動態的檔名 // 接續上一頁... sinf>>size; //dynamic size is determined by reading file for(int i=0; i<size; i++) sinf>>data[i]; sort(data, data+size); // give the name for output file stringstream ofname; ofname<<"result\\rsdata"<<setw(5)<<setfill('0')<<n<<".txt"; //output file for sorted data ofstream routf(ofname.str().c_str()); for(int i=0; i<size; i++) routf<<setw(5)<<data[i]; sinf.close(); routf.close(); 7

指標 (pointer) int var = 4; int* ptr = &var; cout<<&var<<endl; //12FF88 cout<<ptr<<endl; //12FF88 Cout<<*ptr<<endl; //4 name Cout<< var<<endl; //4 address value int* ptr2; name ptr2 = &var; address value 指標 int* ptr; int* ptr = &var; var 取址運算子 0012FF88 4 ptr 0012FF80 0012FF88 & 8

指標 int var = 4; int* ptr = &var; cout<<var<<endl; //4 *ptr = 5; cout<<var<<endl; //5 Var = 6; cout<<*ptr<<endl; //6 name address value name address value 指標 int* ptr; int* ptr = &var; var 取址運算子 0012FF88 4 ptr 0012FF80 0012FF88 & 取值運算子 * 9

問題 1 //*(&var) 等於 *(ptr) cout<<*&var<<endl; //4 cout<<&*var<<endl; //invalid cout<<*&ptr<<endl; //0012FF88 //&(*ptr) 等於 &(var) cout<<&*ptr<<endl; //0012FF88 name address value name address value var 0012FF88 4 ptr 0012FF80 0012FF88 10

指標 指標做為函式引數 (pointer as argument) void swap_ref(int& a, int& b) int temp = a; a = b; b= temp; void swap_ptr(int* a, int* b) int temp = *a; *a = *b; *b= temp; 11

指標 指標做為函式引數 #include<iostream> int a =3, b=5; cout<<a<<" "<<b<<endl; swap_ref(a,b); cout<<a<<" "<<b<<endl; swap_ptr(&a,&b); cout<<a<<" "<<b<<endl; 12

指標 指標運算 var int var = 4; 0012FF88 int* ptr = &var; 4 cout<<ptr<<endl; //12FF88 ptr cout<<--ptr<<endl; //12FF84 0012FF80 //(-4 因為 int 大小為 4bytes) 0012FF88 cout<<++ptr<<endl; //12FF8c //(+4 因為 int 大小為 4bytes) cout<<ptr-5<<endl; //12FF74 (-4) cout<<ptr+5<<endl; //12FF9c (+20) 指標運算 ptr=ptr+5; ptr=ptr-5; ptr+=5; Ptr-=5; ptr++; Ptr--; 13

指標 陣列與指標間的關係 #include<iostream> int a[]=1,2,3,4,5,6,7,8,9,10; cout<<*a<<endl; // 1 cout<<*(a+1)<<endl; // 2 cout<<*(a+2)<<endl; // 3 cout<<*a+1<<endl; // for(int i=0, i<9; i++) cout<<*(a+i); // 1 2 3 4 5 6 7 8 9 10 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] 1 2 3 4 5 6 7 8 9 10 a a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9 14

動態記憶體 & 動態陣列 #include<iostream> // 動態記憶體 (new & delete) int *a = new int; // 配置記憶體 *a = 8; cout<<*a<<endl; // 8 delete a; // 釋放記憶體 address value name address value 0012FF88 8 a 0012FF80 0012FF88 15

動態記憶體 #include<iostream> void fun() int *a = new int; // 配置記憶體 *a = 8; cout<<*a<<endl; // 未釋放記憶體!!! fun(); address value name address value 0012FF88 8 a 0012FF80 0012FF88 16

動態陣列 一維陣列 #include<iostream> // 一維動態陣列 int N=10; // 不需為 const!!! int *a = new int [N]; // 配置記憶體 for(int i=0, i<9; i++) a[i]=i+1; 1 2 3 4 5 6 7 8 9 10 for(int i=0, i<9; i++) a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] cout<<a[i]<<endl; delete [] a; // 釋放記憶體 17

動態陣列 一維陣列 #include<iostream> // 一維動態陣列 int N; cin>>n; // 動態決定 int *a = new int [N]; // 配置記憶體 for(int i=0, i<n; i++) a[i]=i+1; for(int i=0, i<n; i++) cout<<a[i]<<endl; delete [] a; // 釋放記憶體 1 2 3 N-1 N a[0] a[1] a[2] a[n-2] a[n-1] 18

動態陣列 二維陣列 // 二維動態陣列 int N=5, M=3; int **a = new int* [N]; // 配置記憶體 for(int i=0, i<n; i++) a[i]= new int [M]; for(int i=0, i<n; i++) for(int i=0, i<m; i++) a[i][j] = i*m+j+1; for(int i=0, i<n; i++)// 釋放記憶體 delete [] a[i]; delete [] a; a[0] a[1] a[2] a[3] a[4] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 19

標準程式庫 (standard template library; STL) #include<iostream> #include<fstream> #include<string> STL 命名空間 cout<<"stl"<<endl; 20

標準程式庫 (STL) vector 動態陣列 #include<vector> vector<int> vct1(10); vector<int> vct2(10,2); vector<int> vct3; vct3.resize(10); vector 宣告 vector<type> var int size=10; vector<int> vct4(size); vct4.resize(12); 21

標準程式庫 (STL) vector 動態陣列 #include<vector> #include<iostream> vector<int> vct(10); int size = vct.size(); // 陣列大小 vct[0]=100; // 存入陣列 cout<<vct[0]<<endl; // 取得陣列數值 22

標準程式庫 (STL) vector 動態陣列 #include<vector> #include<iostream> vector<int> vct(3,2); vct.push_back(6); vct.insert(vct.begin()+3, 5); vct.erase(vct.begin()+4); 23

標準程式庫 (STL) 多維 vector 動態陣列 #include<vector> #include<iostream> // 宣告 3*4 的 vector 陣列 方法 1 vector<vector<int> > vec2d_1(3); for(int i=0; i<3; i++) vec2d_1[i] = vector<int>(4); // 宣告 3*4 的 vector 陣列 方法 2 vector<vector<int> > vec2d_2(3,vector<int>(4)); 24

標準程式庫 (STL) algorithm #include<algorithm> 方法 sort random_shuffle next_enumeration prev_enumeration find 說明排序亂數排序下一個列舉數上一個列舉數搜尋 25

結構 (structure) #include<iostream> struct customer double x; double y; int demand; ; void main(void) customer client; client.x = 10.5; client.y = 4.3; client.demand = 40; cout<<"the customer at ("<< client.x <<","<< client.y <<") " <<"has demand of "<< client.demand <<endl; 結構宣告 struct name... ; 26

結構 結構陣列 struct customer double x; double y; int demand; ; void main(void) customer client[5]; 27

結構與類別 (class) class customer public: double x; double y; int demand; ; // 兩者的宣告意義相同 struct customer double x; double y; int demand; ; 結構宣告 class name public:... protected:... private:... ; 28

練習 1 練習建構一個學生資料的類別 ( 結構 ), 包含 : 1. 學生學號 2. 學生姓名 3. 國文成績 4. 數學成績 29