第六章 字串與數值函數

57
第第第 第第第第第第第 第第第第 第第第第 :, Visual C++ 6 第第第第 第第第第第第第第第第

Upload: mercer

Post on 04-Jan-2016

80 views

Category:

Documents


0 download

DESCRIPTION

第六章 字串與數值函數. 參考書籍:古頤榛, Visual C++ 6 教學範本 , 碁峰資訊股份有限公司。. 前言. 就像使用變數必須先宣告一樣, C 語言的函數必須先引入對應的標題檔才能在程式敘述中出現。. 本章內容所引入的標題檔. http://yes.nctu.edu.tw/VC/Ref/include/include.htm. 範例程式. Vc601.cpp( 取得字串長度 ). 原始程式碼:. // Vc601.cpp // 取得字串長度練習 #include // 引入標準輸入/輸出函數標題檔 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第六章 字串與數值函數

第六章 字串與數值函數

參考書籍:古頤榛,Visual C++ 6教學範本 ,碁峰資訊股份有限公司。

Page 2: 第六章 字串與數值函數

前言 就像使用變數必須先宣告一樣, C 語言的函數必須先引入對應的標題檔才能在程式敘述中出現。

Page 3: 第六章 字串與數值函數

本章內容所引入的標題檔 http://yes.nctu.edu.tw/VC/Ref/include/in

clude.htm

Page 4: 第六章 字串與數值函數

範例程式

Page 5: 第六章 字串與數值函數

Vc601.cpp(取得字串長度 ) 原始程式碼:

// Vc601.cpp// 取得字串長度練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <string.h> // 引入字串函數標題檔

int main(){ char instr[80]; cout << " 請輸入字串: "; // 顯示訊息字串 cin.getline (instr, 80, '\n'); // 取得輸入字串列 cout << " 字串長度為: " << strlen(instr) // 顯示字串長度 << endl << endl; return 0;}

Page 6: 第六章 字串與數值函數

Vc601.cpp(取得字串長度 ) 解說 :

Page 7: 第六章 字串與數值函數

Vc601.cpp(取得字串長度 ) 輸出範例:

Page 8: 第六章 字串與數值函數

Vc602.cpp(複製字串 ) 原始程式碼:

// Vc602.cpp// 複製字串練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <string.h> // 引入字串函數標題檔

int main(){ char source[80], target[80]; cout << " 請輸入來源字串: "; // 顯示訊息字串 cin.getline (source, 80, '\n'); // 取得來源字串 strcpy(target, source); // 複製字串 cout << " 複製後目的字串: " << target // 顯示目的字串 << endl << endl; return 0;}

Page 9: 第六章 字串與數值函數

Vc602.cpp(複製字串 ) 解說 :

Page 10: 第六章 字串與數值函數

Vc602.cpp(複製字串 ) 輸出範例:

Page 11: 第六章 字串與數值函數

Vc603.cpp(比較字串檢查密碼 ) 原始程式碼:

// Vc603.cpp// 比較字串練習 ( 檢查密碼 )

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <string.h> // 引入字串函數標題檔

int main(){ char password[80] = "2000"; // 定義並啟始密碼 char instring[80]; cout << " 您有 3 次機會, ";

Page 12: 第六章 字串與數值函數

Vc603.cpp(比較字串檢查密碼 ) 原始程式碼:

for (int i = 1; i <=3; i++) // 輸入密碼迴圈 { cout << " 請輸入密碼: "; cin.getline (instring, 80, '\n'); // 取得來源字串 int flag = strcmp(password, instring); // 比較字串 if (flag == 0) { cout << " 恭喜您!密碼正確。 "; // 顯示目的字串 break; // 中斷迴圈 } else { if (i != 3) // 以計數值決定, cout << " 還有 " << 3-i << " 次機會, "; // 顯示的字串 else cout << " 對不起!沒機會了。 "; } }

Page 13: 第六章 字串與數值函數

Vc603.cpp(比較字串檢查密碼 ) 原始程式碼:

cout << endl << endl; return 0;}

Page 14: 第六章 字串與數值函數

Vc603.cpp(比較字串檢查密碼 ) 解說 :

Page 15: 第六章 字串與數值函數

Vc603.cpp(比較字串檢查密碼 ) 解說 :

Page 16: 第六章 字串與數值函數

Vc603.cpp(比較字串檢查密碼 ) 解說 :

Page 17: 第六章 字串與數值函數

Vc603.cpp(比較字串檢查密碼 ) 輸出範例:

Page 18: 第六章 字串與數值函數

Vc604.cpp(串接字串 ) 原始程式碼:

// Vc604.cpp// 附加字串練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <string.h> // 引入字串函數標題檔

int main(){ char first[80], last[80], full[160] = ""; cout << " 請輸入英文名字: "; // 顯示訊息字串 cin.getline (first, 80, '\n'); // 取得名字字串 cout << " 請輸入英文姓氏: "; // 顯示訊息字串 cin.getline (last, 80, '\n'); // 取得姓氏字串 strcat(full, first); // 串接名字字串 strcat(full, " "); // 串接空白字串 strcat(full, last); // 串接姓氏字串 cout << " 您的全名為: " << full // 顯示全名字串 << endl << endl; return 0;}

Page 19: 第六章 字串與數值函數

Vc604.cpp(串接字串 ) 解說 :

Page 20: 第六章 字串與數值函數

Vc604.cpp(串接字串 ) 輸出範例:

Page 21: 第六章 字串與數值函數

Vc605.cpp(檢查字串字數計算 ) 原始程式碼:

// Vc605.cpp// 檢查字串練習 ( 字數計算 )

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <string.h> // 引入字串函數標題檔#include <ctype.h> // 引入字元測試與轉換函數標題檔

int main(){ int print = 0, digit = 0, lower = 0, upper = 0; int punct = 0, space = 0, control = 0, chinese = 0; char string[ ] = "Developer Studio 是一個整合式的開發環境 " "(Integrated Development Environment ; IDE) , " " 它提供 Visual Basic 、 Visual C++ 、與其他程式的開發環境。 "; int len = strlen(string); // 取得字串長度

Page 22: 第六章 字串與數值函數

Vc605.cpp(檢查字串字數計算 )for (int i = 0; i <= len; i++) // 字元檢查迴圈 { if (isprint(string[i]) != 0) // 若為可列印字元 { print++; if (isdigit(string[i]) != 0) // 為數字字元 digit++; else if (islower(string[i]) != 0) // 為小寫字元 lower++; else if (isupper(string[i]) != 0) // 為大寫字元 upper++; else if (ispunct(string[i]) != 0) // 為符號字元 punct++; else // 否則為空白字元 space++; } else if (iscntrl(string[i]) != 0) // 若為控制符號字元 { control++; } else // 否則為全形文字(中文)字元 { chinese++; i++; // 全形字為 2bytes ,要多移一個字元 } }

Page 23: 第六章 字串與數值函數

Vc605.cpp(檢查字串字數計算 ) 原始程式碼:

cout << " 英數符號字數: " << print; // 顯示訊息字串 cout << "\n   大寫字數: " << upper; // 顯示訊息字串 cout << "\n   小寫字數: " << lower; // 顯示訊息字串 cout << "\n   數字字數: " << digit; // 顯示訊息字串 cout << "\n   空白字數: " << space; // 顯示訊息字串 cout << "\n   符號字數: " << punct; // 顯示訊息字串 cout << "\n 控制符號字數: " << control; // 顯示訊息字串 cout << "\n 全形文字字數: " << chinese; // 顯示訊息字串 cout << endl << endl; return 0;}

Page 24: 第六章 字串與數值函數

Vc605.cpp(檢查字串字數計算 ) 解說 :

Page 25: 第六章 字串與數值函數

Vc605.cpp(檢查字串字數計算 ) 解說 :

Page 26: 第六章 字串與數值函數
Page 27: 第六章 字串與數值函數

Vc605.cpp(檢查字串字數計算 ) 解說 :

Page 28: 第六章 字串與數值函數

Vc605.cpp(檢查字串字數計算 ) 輸出範例:

Page 29: 第六章 字串與數值函數

Vc606.cpp(大寫轉小寫 ) 原始程式碼:

// Vc606.cpp// 大寫轉小寫練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <string.h> // 引入字串函數標題檔#include <ctype.h> // 引入字元測試與轉換函數標題檔

int main(){ char string[] = "Developer Studio"; cout << " 字串轉換前: " << string << endl; // 顯示轉換前字串 int len = strlen(string); // 取得字串長度 for (int i = 0; i <= len; i++) // 轉成小寫迴圈 { if (isupper(string[i]) != 0) // 若為大寫字元 string[i] = tolower(string[i]); // 轉成小寫字元 } cout << " 轉換小寫後: " << string << endl // 顯示轉換後字串 << endl; return 0;}

Page 30: 第六章 字串與數值函數

Vc606.cpp(大寫轉小寫 ) 解說 :

Page 31: 第六章 字串與數值函數

Vc606.cpp(大寫轉小寫 ) 輸出範例:

Page 32: 第六章 字串與數值函數

Vc607.cpp(小寫轉大寫 ) 原始程式碼:

// Vc607.cpp// 小寫轉大寫練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <string.h> // 引入字串函數標題檔#include <ctype.h> // 引入字元測試與轉換函數標題檔

int main(){ char string[] = "Developer Studio"; cout << " 字串轉換前: " << string << endl; // 顯示轉換前字串 int len = strlen(string); // 取得字串長度 for (int i = 0; i <= len; i++) // 轉成大寫迴圈 { if (islower(string[i]) != 0) // 若為小寫字元 string[i] = toupper(string[i]); // 轉成大寫字元 } cout << " 轉換大寫後: " << string << endl // 顯示轉換後字串 << endl; return 0;}

Page 33: 第六章 字串與數值函數

Vc607.cpp(小寫轉大寫 ) 解說 :

Page 34: 第六章 字串與數值函數

Vc607.cpp(小寫轉大寫 ) 輸出範例:

Page 35: 第六章 字串與數值函數

Vc608.cpp(設定欄位寬度九九乘法表 ) 原始程式碼:

// Vc608.cpp// 設定欄位寬度練習 ( 九九乘法表 )

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <iomanip.h> // 引入串列型態資料處理函數標題檔

int main(){ for (int i = 1; i <= 9; i++) // 被乘數迴圈 1 至 9 { for (int j = 2; j <= 9; j++) // 乘數迴圈 2 至 9 cout << j << '*' << i << '=' // 輸出乘數被乘數 << setw(2) << j * i << '\t'; // 設定輸出字元長度 cout << endl; } cout << endl; return 0;}

Page 36: 第六章 字串與數值函數

Vc608.cpp(設定欄位寬度九九乘法表 ) 解說 :

Page 37: 第六章 字串與數值函數

Vc608.cpp(設定欄位寬度九九乘法表 ) 輸出範例:

Page 38: 第六章 字串與數值函數

Vc609.cpp(三角函數 ) 原始程式碼:

// Vc609.cpp// 三角函數練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <iomanip.h> // 引入串列型態資料處理函數標題檔#include <math.h> // 數值函數標題檔

int main(){ double degree = (3.1415926) / 180; // degree= 徑度 / 度 double x; // 宣告變數 cout << setw(3) << "i" // 輸出欄位名稱 << setw(20) << "sin(i)" << setw(20) << "cos(i)" << setw(20) << "tan(i)" << endl << endl;

Page 39: 第六章 字串與數值函數

Vc609.cpp(三角函數 ) 原始程式碼:

for (double i = 0; i < 390; i += 30) // 輸出函數值迴圈 { x = degree * i; // 角度換算 cout << setw(3) << i // 輸出三角函數 << setw(20) << sin(x) << setw(20) << cos(x) << setw(20) << tan(x) << endl; } cout << endl; // 跳一行 return 0;}

Page 40: 第六章 字串與數值函數

Vc609.cpp(三角函數 ) 解說 :

Page 41: 第六章 字串與數值函數

Vc609.cpp(三角函數 ) 輸出範例:

Page 42: 第六章 字串與數值函數

Vc610.cpp(指數與對數 ) 原始程式碼:

// Vc610.cpp// 指數與對數練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <iomanip.h> // 引入串列型態資料處理函數標題檔#include <math.h> // 數值函數標題檔

int main(){ cout << setw(2) << "i" // 輸出欄位名稱 << setw(12) << "log(i)" << setw(18) << "log10(i)" << setw(14) << "exp(i)" << endl << endl; for (double i = 1; i <= 10; i++) // 輸出函數值迴圈 cout << setw(2) << i << '\t' // 輸出函數值 << log(i) << setw(8) << '\t' << log10(i) << setw(8) << '\t' << exp(i) << endl; cout << endl; // 跳一行 return 0;}

Page 43: 第六章 字串與數值函數

Vc610.cpp(指數與對數 ) 解說 :

Page 44: 第六章 字串與數值函數

Vc610.cpp(指數與對數 ) 輸出範例:

Page 45: 第六章 字串與數值函數

Vc611.cpp(次方與根號 ) 原始程式碼:

// Vc611.cpp// 次方與根號練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <iomanip.h> // 引入串列型態資料處理函數標題檔#include <math.h> // 數值函數標題檔

int main(){ cout << setw(2) << "i" << '\t' // 輸出欄位名稱 << setw(15) << "2 的 i 次方 " << setw(15) << " 根號 i" << endl << endl; for (double i = 1; i <= 10; i++) // 輸出函數值迴圈 cout << setw(2) << i << '\t' // 輸出函數值 << setw(12) << pow(2, i) << "\t\t" << sqrt(i) << endl; cout << endl; // 跳一行 return 0;}

Page 46: 第六章 字串與數值函數

Vc611.cpp(次方與根號 ) 解說 :

Page 47: 第六章 字串與數值函數

Vc611.cpp(次方與根號 ) 輸出範例:

Page 48: 第六章 字串與數值函數

Vc612.cpp(取整數 ) 原始程式碼:

// Vc612.cpp// 取整數練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <iomanip.h> // 引入串列型態資料處理函數標題檔#include <math.h> // 數值函數標題檔

int main(){ cout << "i\t" // 輸出欄位名稱 << setw(15) << " 小數進位 " << setw(15) << " 刪除小數 " << endl << endl; for (double i = 1; i <= 5; i += 0.5) // 輸出函數值迴圈 cout << i << '\t' // 輸出函數值 << setw(12) << ceil(i) << setw(15) << floor(i) << endl; cout << endl; // 跳一行 return 0;}

Page 49: 第六章 字串與數值函數

Vc612.cpp(取整數 ) 解說 :

Page 50: 第六章 字串與數值函數

Vc612.cpp(取整數 ) 輸出範例:

Page 51: 第六章 字串與數值函數

Vc613.cpp(取絕對值 ) 原始程式碼:

// Vc613.cpp// 取絕對值練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <iomanip.h> // 引入串列型態資料處理函數標題檔#include <math.h> // 數值函數標題檔

int main(){ cout << "i\t" // 輸出欄位名稱 << setw(15) << " 絕對值 " << endl << endl; for (double i = -1; i <= 1; i += 0.2) // 輸出函數值迴圈 { if (i < 1.0e-10 && i > -1.0e-10) // 若 i 趨近於 0 i = 0; // 則令 i=0 cout << i << '\t' // 輸出函數值 << setw(12) << fabs(i) << endl; } cout << endl; // 跳一行 return 0;}

Page 52: 第六章 字串與數值函數

Vc613.cpp(取絕對值 ) 解說 :

Page 53: 第六章 字串與數值函數

Vc613.cpp(取絕對值 ) 輸出範例:

Page 54: 第六章 字串與數值函數

Vc614.cpp(字串轉換數值 ) 原始程式碼:

// Vc614.cpp// 字串轉換數值練習

#include <iostream.h> // 引入標準輸入/輸出函數標題檔#include <iomanip.h> // 引入串列型態資料處理函數標題檔#include <stdlib.h> // 轉換數值函數標題檔

int main(){ char *s; double x; int i; long l; cout << setw(11) << " 字串 \t\t" << " 數值 " << endl; s = "-1998.12E-25 "; // 定義字串 x = atof( s ); // 轉換成浮點數 cout << setw(15) << s << "\t\t" << x << endl; s = " 686 pigs "; // 定義字串 i = atoi( s ); // 轉換成整數 cout << setw(15) << s << "\t\t" << i << endl; s = " 98686 dollars"; // 定義字串 l = atol( s ); // 轉換成長整數 cout << setw(15) << s << "\t\t" << l << endl; cout << endl; // 跳一行 return 0;}

Page 55: 第六章 字串與數值函數

Vc614.cpp(字串轉換數值 ) 解說 :

Page 56: 第六章 字串與數值函數

Vc614.cpp(字串轉換數值 ) 輸出範例:

Page 57: 第六章 字串與數值函數

The end