Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 參考書籍:古頤榛, Visual C++ 6教學範本 , 碁峰資訊股份有限公司。
第六章 字串與數值函數 參考書籍:古頤榛, Visual C++ 6教學範本 , 碁峰資訊股份有限公司。

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

3 本章內容所引入的標題檔

4 範例程式

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; }

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

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

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; }

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

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

11 Vc603.cpp(比較字串檢查密碼) 原始程式碼: // Vc603.cpp // 比較字串練習 (檢查密碼)
#include <iostream.h> // 引入標準輸入/輸出函數標題檔 #include <string.h> // 引入字串函數標題檔 int main() { char password[80] = "2000"; // 定義並啟始密碼 char instring[80]; cout << "您有 3 次機會,";

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 << " 次機會,"; // 顯示的字串 cout << "對不起!沒機會了。";

13 Vc603.cpp(比較字串檢查密碼) 原始程式碼: cout << endl << endl; return 0;
}

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

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

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

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

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; }

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

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

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); // 取得字串長度

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,要多移一個字元

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; }

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

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

26

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

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

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;

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

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

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;

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

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

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; } return 0;

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

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

38 Vc609.cpp(三角函數) 原始程式碼: // Vc609.cpp // 三角函數練習
#include <iostream.h> // 引入標準輸入/輸出函數標題檔 #include <iomanip.h> // 引入串列型態資料處理函數標題檔 #include <math.h> // 數值函數標題檔 int main() { double degree = ( ) / 180; // degree=徑度/度 double x; // 宣告變數 cout << setw(3) << "i" // 輸出欄位名稱 << setw(20) << "sin(i)" << setw(20) << "cos(i)" << setw(20) << "tan(i)" << endl << endl;

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;

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

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

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; }

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

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

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; }

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

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

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; }

49 Vc612.cpp(取整數) 解說 :

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

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;

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

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

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 = " E-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 = " dollars"; // 定義字串 l = atol( s ); // 轉換成長整數 cout << setw(15) << s << "\t\t" << l << endl; cout << endl; // 跳一行 return 0; }

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

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

57 The end


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

Similar presentations


Ads by Google