1 / 66

C++ 程式設式

C++ 程式設式. 杜勇進. C++ 風格 (DEV-C++ 版 ). #include <iostream> #include <stdlib.h> using namespace std; // 可省略 int main(int argc, char *argv[]) { cout << "Hello! C++" << endl; system("PAUSE"); // 自動產生 return 0; // 自動產生 }. C++ 風格 (VC++ 版 ). #include <iostream.h>

kyrene
Télécharger la présentation

C++ 程式設式

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. C++程式設式 杜勇進

  2. C++風格(DEV-C++版) #include <iostream> #include <stdlib.h> using namespace std; // 可省略 int main(int argc, char *argv[]) { cout << "Hello! C++" << endl; system("PAUSE"); //自動產生 return 0; //自動產生 }

  3. C++風格(VC++版) #include <iostream.h> int main(int argc, char *argv[]) { cout << "Hello! C++" << endl; }

  4. 基本資料型別 • 整數(4byte) int int a,b; • 長整數(4byte) long long c,d; • 單精確度實數(4byte) float float e,f; • 倍精確度實數(8byte) double double x,y; • 字元(1byte) char char s='A' ,t; • 無 void void z;

  5. C語言運算子 • 1.算術運算子︰+ ,- ,* ,/ , % • 2.增量和減量運算子:++ , -- • 3.關係運算子: > , < , >= , <= , == , != • 4.邏輯運算子: ! , && , || • 5.位元運算子: ~ , & , | , , >> , << • 6.指定運算子:= , += , -= , *= , /= , %= , &= , |= , = , <<= , >>= • 7.條件運算子: ? : • 8.位址和指標運算子: & , * • 9.長度運算子: sizeof()

  6. 算術運算子︰+ ,- ,* ,/ , % • 3 + 5 • 9 - 2 • 8 * 7 • 4.5 / 3 實數 • 5 / 2 整數 • 18 / 3 • 18 % 3 求餘數

  7. 增量和減量運算子:++ , -- • ++ 加一 • -- 減一 • 前置:先增減再參考 • 後置:先參考再增減 • a=5;a++; ++a; • a=8; b=3;c=a++ + ++b;

  8. 增量和減量運算子:++ , -- • a=3; b=8;c = --a - b++; • a=4; b=9;c = a-- - --b;

  9. C++ 輸入與輸出 • 輸出: cout << x << y << endl; • 輸入:cin >> a >> b; • 有格式輸出: cout << hex<< setw(5) << a ; cout.setf(ios::fixed); cout.precision(5); cout << setw(18) << c << endl;

  10. 輸入二數,相加減 #include <iostream.h> int main(int argc, char *argv[]) { int a,b,sum,dif; cin >> a >> b; sum = a+b; dif = a-b; cout << "和=" << a+b<< endl; cout << "差=" << a-b << endl; }

  11. C++ 輸出有格式範例 #include <iostream.h> #include <iomanip.h> void main(int argc, char *argv[]) { int a=100; double c=12.345; cout << a << endl; cout << "sum=" << hex << setw(5) << a << endl; //16進制寬度5位數 cout.setf(ios::fixed); //用定點數表示 cout.precision(5); //精確度5位數 cout << setw(18) << c << endl; //寬度18 }100sum= 64 12.34500

  12. 簡單練習(二) • 輸入二整數,輸出和、差、積、商、餘數 • 輸入一整數(三位數),求相反印出例輸入:675輸出:576 • 輸入圓的半徑,輸出面積與周長 • 輸入三角形的底與高,輸出面積

  13. 關係運算子: > , < , >= , <= , == , != • 大於 > • 小於 < • 大於或等於 >= • 小於或等於 <= • 等於 == • 不等於 !=

  14. 邏輯運算子: ! , && , || • 不(NOT) ! • 或(OR) || • 及(AND) && • (5>3) && (4<7) • (a>b) || (x < y) • !(a > b)

  15. 指定運算子 • = , += , -= , *= , /= , %= , &= , |= , <<= , >>= • a = a+8; ==> a += 8; • b =b -3; ==> b -= 3; • c = c*6 ; ==> c *= 6; • d = d/2; ==> d /= 2;

  16. 條件運算子 ? : • if (a > b ) then c=5 else c=9; ==> c = (a > b) ? 5 : 9; • if (a < c ) then w=8 else w=x;==> w = (a<c) ? 8 : x; • 不管真或假所要執行的指令左邊皆相同

  17. 定址和指標運算子: & , * • *指標 • & 位址 int a=5, b=8, c;int *p, *q;p = &a;q = &b;c = *p;*p = *q;*q = c;printf("%d%d\n",a,b);

  18. 指標變數vs普通變數 • 一個變數在記憶體內佔一個位置 • int a,b; <==普通變數,位置內放值 • int *p,*q; <==指標變數,位置內放位址

  19. 運算子的優先和結合性 • (16) () [] -> 由左至右(15) - ~ ! * & (單運算子) 由右至左(14) ++ -- sizeof cast(單運算子) 由右至左(13) * / % 由左至右(12) + - 由左至右(11) >> << 由左至右(10) < > <= >= 由左至右(9) == != 由左至右(8) & 由左至右(7) 由左至右(6) | 由左至右(5) && 由左至右(4) || 由左至右(3) ? : 由右至左(2) = , += , -= , *= , /= , %= , &= , |= , = , <<= , >>= 由右至左(1) , 由左至右

  20. IF敘述 • if (c) s; • if (c) s1; else s2; • e.g. if (a>4) x=y; • if (a == b) a=8; else c=9;

  21. 巢狀IF a=3; b=2; x=9; y=2; p=3; q=3; if (a>b) if (x>y) if (p==q) t=9; else a=2; t=8; t=?

  22. IF練習 • 輸入二整數,印出大的 • 輸入三整數,印出最大的 • 輸入四整數,印出最小的 • 輸入三整數,印出由小至大 • 輸入二整數x,y,求x • 輸入一元二次方程式的係數,輸出根 ax2+bx+c=0

  23. 輸入二整數,印出大的 #include <iostream.h> void main() { int a,b; cin >> a >> b; if (a >b) cout << "max=" << a << endl; else cout << "max=" << b << endl; }

  24. 輸入三整數,印出最大的 #include <iostream.h> void main() { int a,b,c,max; cin >> a >> b >>c; if (a >b) max=a; else max=b; if (c > max) max=c; cout << "max=" << max << endl; }

  25. 輸入二整數x,y, 求xy(VC++) #include <iostream.h> #include <math.h> int main() { int x,y; cin >> x >> y; cout << "結果=" << pow(x,y) << endl; }

  26. C語言常用算數函數 • #include <math.h> • double pow(double x, double y) x的y次方 • double sqrt(double x) 求x的平方根 • double fabs(double x) 求實數x的絕對值 • int abs(int x) 求整數x的絕對值

  27. C語言常用字串函數 • #include <string.h> • int strcmp(char *s1,*s2) 字串比較 • char *strcpy(char *dest, char *src) 字串拷貝 • char *strstr(char *src,char *sub) 在字串中 • int strlen(char *s1) 字串長度 • int atoi(char *s) 字串轉整數 • gets(char *s) 讀取一字串 • puts(char *s) 輸出一字串

  28. C語言常用字元函數 • int tolower(ch) ch轉成小寫 • int toupper(ch) ch轉成大寫 • int islower(ch) ch 為小寫嗎? • int isspace(ch) ch 為空白嗎? • int isdigit(ch) ch 為數字嗎? • int isalpnum(ch) ch 為文數字嗎? • int getchar() 讀入一字元 • void putchar(ch) 輸出一字元

  29. strstr #include <iostream.h> #include <string.h> void main(int argc, char *argv[]) { char source[20]="this is a book"; char sub[5]="is"; cout << strstr(source,sub); /* is is a book */ }

  30. 運算式1 False 運算式2 True 運算式3 敘述 For 敘述 • for (運算式1;運算式2;運算式3) 敘述;

  31. For 敘述簡單例子 • s=1+2+3+.......+10 s = 0; for (i=1; i<=10; i++) s += i;

  32. For 敘述練習 • s=1+3+5+......+99 • s=5+10+15+......+75 • s=1-2+3-4+......-N • S=1*2*3*....*10 • * *** ****** ********** ************* ***********

  33. s=1+3+5+......+99 #include <iostream.h> void main() { int s,i; s = 0; for (i=1; i<=99; i+=2 ) s += i; cout << "sum=" << s << endl; }

  34. s=1+3+5+......+N #include <iostream.h> void main() { int s,i; cout <<" 輸入N=?"; cin >> n; s = 0; for (i=1; i<=n; i+=2 ) s += i; cout << "sum=" << s << endl; }

  35. While 敘述 while (c) s; s=1+2+3+....+10 s=0; i=1; while (i <=10) { s += i; i++; } cout << "sum=" << s << endl;

  36. s=1+2+...+10 s=0; i=1; while (i <=10) s += i++; cout << s << endl;

  37. s=1+2+...+10=? s=0; i=1; while ( i++ <= 10) s += i; cout << "sum=" << s << endl;

  38. s=1+2+...+10=? s=0; i=10; while (i > 0) s += i-- ; cout << s << endl;

  39. while 敘述練習 • 輸入一連串正整數,求總和(若輸入-1表示結束) • 輸入一連串正整數,求個數(若輸入-1表示結束) • 輸入一連串正整數,求平均(若輸入-1表示結束) • s=1+3+5+.....+N • p=1*2*3*....*N

  40. do while 敘述 do s; while (c); s=1+2+3+...+10s=0;i=1;do s += i++;while (i>10);

  41. switch 敘述 switch (變數) { case 值1:敘述1; break; case 值2:敘述2; break; ........... default :敘述m; }

  42. break 敘述 • 跳出迥圈、區塊 • for (i=1; i <= 100; i++) {if (i %5==0) break; s += i; } cout << s;

  43. continue敘述 • 放棄此次,繼續下一次 • for (i=1; i <= 100; i++) {if (i %5==0) continue; s += i; } cout << s;

  44. switch 敘述範例 a=8; b=3; op = '+'; switch (op) { case '+' : c=a+b; case '-' : c=a-b; case '*' : c=a*b; default : c=0; } printf("c=%d\n",c); c=?

  45. switch 敘述範例 a=8; b=3; op = '+'; switch (op) { case '+' : c=a+b; break; case '-' : c=a-b; break; case '*' : c=a*b; break; default : c=0; } cout << c<< endl; c=?

  46. 定址和指標運算子: & , * • *指標 • & 位址 int a=5, b=8, c;int *p, *q;p = &a;q = &b;c = *p;*p = *q;*q = c;cout << a << b << endl;

  47. 陣列(Array) • 一群相同型別、固定大小的資料,共用一變數名稱,各別元素用索引來區到 • 索引值必須整數 • 靜態配置 • 下限為零 • 一維:int a[10]; ==> 0~9 • 二維:int b[3][4]; ==> 0~2, 0~3

  48. 陣列初值 • 一維: int a[5]={3,5,6,9,2}; • 二維: int b[3][4]= { {3,4,5,3}, {2,9,8,9}, {6,5,4,2} }; • 二維: int b[ ][4] = { {3,4,5,3}, {2,9,8,9}, {6,5,4,2} };

  49. 字串 • 字元陣列 char name[5]; • 初值 char name[5]="john"; • char name[5]={'j','o','h','n','\0'};

  50. 陣列與指標 • 陣列名稱代表一位址常數 • 指標變數內容為位址 int a[5]={2,3,7,9,8}; int *p,b,c; p = a; b= a[0]+a[1]; c= *p + *(p+1);

More Related