1 / 32

3 數 學運算

3.1 鍵盤輸入 3-2 3.1.1 輸入函數 cin 3-2 3.1.2 多重輸入 cin 3-2 3.2 輸出格式化 3-3 3.2.1 設定輸出長度 setw 3-4 3.2.2 設定有效數字 setprecision 3-6 3.2.3 設定輸出旗號 setiosflags 3-7 3.3 輸入格式化 3-11 3.3.1 設定輸入長度 setw 3-11 3.3.2 cin 成員函數 3-12 3.4 算術運算 3-16 3.4.1 算術運算符號 3-17. 3.4.2 上限與下限溢位 3-19

Download Presentation

3 數 學運算

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3.1鍵盤輸入 3-2 3.1.1 輸入函數cin 3-2 3.1.2 多重輸入cin 3-2 3.2輸出格式化 3-3 3.2.1 設定輸出長度setw 3-4 3.2.2 設定有效數字setprecision 3-6 3.2.3 設定輸出旗號setiosflags 3-7 3.3輸入格式化 3-11 3.3.1 設定輸入長度setw 3-11 3.3.2 cin成員函數 3-12 3.4算術運算 3-16 3.4.1 算術運算符號 3-17 3.4.2 上限與下限溢位 3-19 3.4.3 轉換資料型態 3-20 3.5指定運算 3-24 3.5.1 單一指定 3-24 3.5.2 多重指定 3-25 3.5.3 混合指定 3-25 3.6數值函數 3-27 3.6.1 亂數函數 3-27 3.6.2 三角函數 3-32 3.6.3 指數與對數 3-34 3.6.4 冪次與開方 3-35 3.6.5 小數進位與切除小數 3-36 3.6.6 取絕對值 3-37 3 數學運算

  2. 3.1 鍵盤輸入 • 標準輸入元件是鍵盤,所以本節將介紹如何在DOS系統下,使用cin函數讀取鍵盤輸入的字元與字串。

  3. 3.1.1 輸入函數cin • #include <iostream> • cin >> 變數; • 範例 int length; //宣告整數變數length cin >> length; //將輸入資料存入length

  4. 3.1.2 多重輸入cin • #include <iostream> • cin >> 變數1 >> 變數2 >> . . . >> 變數n; • 範例 int width, height; //宣告變數width與height cin >> width >> height; //分別存入width與height

  5. 3.2 輸出格式化 • 3.2.1 設定輸出長度setw • 3.2.2 設定有效數字setprecision • 3.2.3 設定輸出旗號setiosflags • 3.2.4 cout成員函數

  6. 3.2.1 設定輸出長度setw • #include <iomanip> • setw(指定長度) • 範例 int number = 25; cout << '(' << number << ")\n"; //輸出(25) cout << '(' << setw(3) << number << ")\n"; //輸出( 25) cout << '(' << setw(5) << number << ")\n"; //輸出( 25)

  7. 3.2.2 設定有效數字setprecision • #include <iomanip> • setprecision(有效位數) • 範例 const double PI = 3.141592653; cout << setprecision(10) << PI << endl; //輸出3.141592653十位 cout << setprecision(8) << PI << endl; //輸出3.1415927八位有效 cout << setprecision(6) << PI << endl; //輸出3.14159六位有效

  8. 3.2.3 設定輸出旗號setiosflags • #include <iomanip> • setiosflags(ios::格式旗號); • 範例 float number = 35.7 cout << setprecision(4) << number; //顯示35.7 cout << setiosflags(ios::fixed) << setprecision(2) << number; //顯示35.7 cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(2) << number; //顯示35.70

  9. 3.3 輸入格式化 • 3.3.1 設定輸入長度setw • 3.3.2 cin成員函數

  10. 3.3.1 設定輸入長度setw • #include <iomanip> • setw(指定長度) • 範例 char string[4]; //宣告字串變數string cin >> setw(4) >> string; //設定輸入字數並取得輸入

  11. 3.3.2 cin成員函數 • cin.width(欄位寬度) cin.width(5); //相當於cin >> setw(5); • cin.getline(變數, 長度, '\n') char sentence[81]; //宣告字串變數 cin.getline(sentence, 81, ‘n’); //取得包含空白句子 • cin.get(字元變數) char key; //宣告字元變數key cin.get(key); //取得鍵盤按鍵 • cin.ignore(長度) cin >> num; //讀取數值並存入num cin.ignore(); //忽略前一輸入字元Enter cin.get(key); //取得鍵盤按鍵

  12. 3.4 算術運算 • C++ 的數學運算式(mathematical expressions)與一般數學運算式相容,它代表一個數值的敘述 。 • 範例 int a = 5, b = 3, c = 6; int x = 3 * a + 2 * b + c;

  13. 3.4.1 算術運算符號

  14. 3.4.2 上限與下限溢位 • 上限溢位(overflows)就是指定一個較大型態的資料給一個較小型態的變數。 • 範例 short n1 = 32767; //n1=0x7fff n1 = n + 1; //n1=0x8000=-32768溢位 unsigned short n2 = 65535; //n2=0xffff n2 = n2 + 1; //n2=0x0000=0上限溢位

  15. 3.4.2 上限與下限溢位 (續) • 下限溢位(underflows)也是指定一個較大型態的資料給一個較小型態的變數。 • 範例 short n3 = -32768; //n3=0x8000 n3 = n3 - 1; //n3=0x7fff=32767溢位 unsigned short n4 = 0; //n4=0x0000 n4 = n4 - 1; //n4=0xffff=65536溢位

  16. 3.4.3 轉換資料型態 • 指定型態(資料|變數) • 由小轉大 • 範例一 int n; short n1 = 32767; //n1=32767 n = int(n1 + 1); //n=32767+1=32768 • 範例二 int n; unsigned short n2 = 65535; //n2=65535 n = int(n2 + 1); //n=65535+1=65536

  17. 3.4.3 轉換資料型態 (續) • 範例三 int n; short n3 = -32768; //n3=-32768 n = int(n3 - 1); //n=-32768-1=-32769 • 範例四 int n; unsigned short n4 = 0; //n4=0 n = int(n4 - 1); //n=0-1=-1 • 範例五 int a=5; int b=3; float c = (float(a) / float(b));

  18. 3.4.3 轉換資料型態 (續) • 由大轉小 • 範例一 int intVar = 65500; //intVar=65500 signed short shortVar = short(intVar); //shortVar=-36 • 範例二 short shortLet = 65; //shortLet=65 char charLet = char(shortLet); //charLet='A' • 範例三 float floatNum = 70000.0f; //floatNum=70000.0 int intNum = int(floatNum); //intNum=70000 short shortNum = short(floatNum); //shortNum=4464

  19. 3.5 指定運算 • 指定運算符號(assignment operators)包括 • 單一指定運算符號(=) • 多重指定運算符號 • 混合運算符號(+=、-=、*=、/=、%=)

  20. 3.5.1 單一指定 • 資料型態 變數名稱1, 變數名稱2, … • 變數名稱1 = 初值, 變數名稱2 = 初值, … short shortVar; //宣告短整數變數shortVar shortVar = 5; //shortVar的初值等於5 . shortVar = 10; //改變shortVar的值為10 • 資料型態 變數名稱1 = 初值, 變數名稱2 = 初值, … short shortVar = 5; //shortVar的初值等於5

  21. 3.5.2 多重指定 • 變數名稱1 = 變數名稱2 … = 初值; • 範例一 int a, b, c, d; //宣告整數變數a, b, c, d a = b = c = d = 10; //令a=b=c=d=10 • 範例二 const int VALUE = 100; //宣告常數符號 int var1, var2, var3; //宣告變數 var1 = var2 = var3 = VALUE; //令多個變數=100

  22. 3.5.3 混合指定 • 變數名稱 op= 資料; • 範例 a = a + 10; //a+10存回a a += 10; //a+10存回a

  23. 3.6 數值函數 • C++ 語言提供一些常用的數學函數(mathematical functions),如三角函數、指數與對數函數、冪次與開方函數、取整數函數、取絕對值函數、還有產生亂數函數等等。 • 這些數學函數包含於cmath標題檔中,亂數函數則包含於cstdlib標題檔中,所以使用這些函數之前必須先插入包含該函數的標題檔到使用者程式的前置處理區。

  24. 3.6.1 亂數函數 • 產生固定亂數 • #include <cstdlib> • int rand(void) • 範例 cout << rand() << endl; //輸出亂數 cout << rand() << endl; //輸出亂數 cout << rand() << endl; //輸出亂數

  25. 3.6.1 亂數函數 (續) • 產生種子亂數 • #include <cstdlib> • void srand (unsigned 種子數) • 範例 unsigned seed; cin >> seed; //輸入種子數 srand(seed); //設定亂數種子數 cout << rand() << endl; //輸出亂數 cout << rand() << endl; //輸出亂數 cout << rand() << endl; //輸出亂數

  26. 3.6.1 亂數函數 (續) • 產生隨機亂數 • #include <ctime> • time(*指標) • 範例 void srand (time(NULL)); cout << rand() << endl; //輸出亂數 cout << rand() << endl; //輸出亂數 cout << rand() << endl; //輸出亂數

  27. 3.6.1 亂數函數 (續) • 調整亂數範圍 • 下限 + rand() % (上限 –下限 + 1) • 範例 int x, y; x = 1 + rand() % (10 - 1 + 1); //1~10的整數亂數 y = rand() % (99 + 1); //0~99的整數亂數

  28. 3.6.2 三角函數 • #include <cmath> • double sin(double 徑度) • double cos(double 徑度) • double tan(double 徑度) • 範例 x = 30 * (3.14159 / 180); //x = 30 double a = sin(x); //a = sin(30) double b = cos(x); //b = cos(30) double c = tan(x); //c = tan(30)

  29. 3.6.3 指數與對數 • #include <cmath> • double exp(數值) • double log(數值) • double log10(數值) • 範例 double a = log(2); //a = ln(2) double b = log10(2); //b = log10(2) double c = exp(2); //c = e2

  30. 3.6.4 冪次與開方 • #include <cmath> • double pow(底數, 冪次) • double sqrt(數值) • 範例 double a = pow(2, 3); //a=23 double b = sqrt(3); //b=根號3

  31. 3.6.5 小數進位與切除小數 • #include <cmath> • double ceil(數值) • double floor(數值) • 範例 int m1 = ceil(3.33) //m1 = 4 int n1 = floor(3.33); //n1 = 3 int m2 = ceil(-3.33) //m2 = -3 int n2 = floor(-3.33); //n2 = -4

  32. 3.6.6 取絕對值 • #include <cmath> • double fabs (數值) • 範例 int a = fabs(5.25); //a = 5.25 int b = fabs(-3.75); //b = 3.75

More Related