1 / 111

有趣的程式設計  Programming Concept

有趣的程式設計  Programming Concept. 視覺化程式設計 Visual Programming (Visual Basic) 蔡文能 tsaiwn@csie.nctu.edu.tw. Agenda. Programming Concepts An informal introduction Language Basics Flow Control ( 流程控制 ) Introduction to Visual BASIC BASIC Language Event Driven concept VB IDE ( 整合發展環境 )

sari
Download Presentation

有趣的程式設計  Programming Concept

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. 有趣的程式設計 Programming Concept 視覺化程式設計 Visual Programming (Visual Basic) 蔡文能 tsaiwn@csie.nctu.edu.tw

  2. Agenda • Programming Concepts • An informal introduction • Language Basics • Flow Control (流程控制) • Introduction to Visual BASIC • BASIC Language • Event Driven concept • VB IDE (整合發展環境) • Java example

  3. Program 程式 = 規劃事情讓電腦做 • Programs = Algorithms + Data Structures • 寫程式基本招式 (第三代語言) + (物件導向) • 宣告指述(敘述) Declaration statement • 設定值指述 Assignment statement • 控制指述 Control statement • 子程式(副程式或函數)定義與叫用 • 類別(Class)與物件導向(Object Oriented)

  4. Programming Languages • 電腦語言(程式語言)的發展 • 第一代是機器語言: 1567 3578 • 第二代是組合語言: Load 5, 67 ; Store 5,78 • 第三代電腦語言不用管電腦的CPU有何種指令! 所謂與機器無關(Machine independent)的電腦語言 • 第三 代程式語言加上物件導向(Object Oriented) • 第四代? 第五代? (見後面投影片) • SGML, HTML, XML, XUL • XUL = XML User-interface Language

  5. 電腦硬體五大單元 2+3=?

  6. 電腦如何運作? Memory address 0 Fetch, Decode, Execute 1 系統區 2 Instruction Pointer . . . CPU IP 程式+靜態data SP HEAP Stack Pointer . STACK . . 9875 9876 系統區

  7. Generations of programming languages 5GL ? Prolog ? 機器語言 VHLL非常高階語言 4GL 高階語言 HLL 組合語言

  8. 1st-generation :Machine Language • 0101001001010010010100100101001001 • 0010 • 01001001 • 01001010 10100110 • 4a a6 • 80 20 2f • 3a 9d 81 3e

  9. 2nd-generation : Assembly Language • Load 100 • Loadi 100 • Load [100] • Load R5, 100 • Load R5, [100] • Store R5, 100 • Move AX,[100] • Move [100],ax • ADD AX,BX • Sub AX,BX • OR ax,dx • JLT there • JMP there • CMP AX, 200 • JGE there • Push AX

  10. The evolution of programming paradigms

  11. A function that computes the average of a list of numbers constructed from the simpler functions Sum, Count, and Divide

  12. The composition of a typical imperative program or program unit

  13. The same variable declarations in different languages

  14. A two-dimensional array (陣列) with two rows and nine columns INTEGER Scores(2,9) int Scores[2][9];

  15. sqrt 是程式庫裡的一個 函數(function, 函式) 印出根號 2 的 C 程式 C型式的註解 請抄入 stdio.h 檔 /* 寫個程式印出根號2 */ #include<stdio.h> double sqrt(double); // #include<math.h> int main( ) { printf(" sqrt 根號 2 =%f\n ", sqrt(2.0) ); // C++ 式的註解 comment printf("Bye bye!\n"); return 0; } C++型式的註解

  16. 印出根號 3的 C 程式 強調是C的 /* 寫個程式印出根號3 */ #include<stdio.h> extern "C" double sqrt(double); // #include<math.h> int main( ) { printf(" sqrt 根號 3 =%f\n ", sqrt(3.0) ); // C++ 式的註解 comment printf("Bye bye!\n"); return 0; } 每次要重改程式嗎? C++ 與 C 文法略有不同

  17. 使用變數 &y傳 address 印出根號 x 的 C 程式 /* 寫個程式印出根號 x */ #include <stdio.h> #include<math.h> double y; int main( ) { printf(" Give me x :"); scanf("%lf" , &y); printf("Sqrt(%f)=%f\n", y, sqrt(y) ); return 0; }

  18. 會一直讀入 x 並印出根號 x 的 C 程式(使用 while loop) #include <stdio.h> #include <math.h> int main( ) /* This program can find square root of any number that >=0 and will stop when you enter a value that < 0 阿版權是蔡文能的, 歡迎拷貝分享初學者! */ { float onevar; /* 變數名稱隨便我取的啦 */ double ans; /* 用來放答案 */ printf("Find square root of X. Negative value will cease the program\n"); printf("X=? "); /* 還是問"X=?" 比較簡潔有力, 不用說onevar */ scanf("%f", &onevar); while(onevar >=0 ) { /*只要onevar大於或等於零我們就一直重複做{...} */ ans=sqrt(onevar); /*其實sqrt的參數也應該為double 型別*/ printf(" SQRT(%f)=%f\n", onevar, ans); printf("X=? "); scanf("%f", &onevar); } /* while(onevar */ return 0; }

  19. 自己如何寫 求 x 的平方根 P4.c P5.c 目的:練習 想 Algorithm 長除法求平方根? 你試著寫成程式 . .? 適合手動做的方法不一定適合電腦做!

  20. p4.c – 練習 想 Algorithm(1/2) /* This program can find square root of any number that >=0 and will stop when you enter a value that < 0 */ /* This version is trying to show you that we can write our function/procedure to solve the problem, though the mysqrt() is actually calling the system standard function. We will rewrite the mysqrt() in next version. *****************************/ void hello(void); /*我會用到一個沒參數的函數叫hello() */ double mysqrt(double); /*也會叫用一個叫mysqrt()的函數,參數是一個double數 */ #include <math.h> /* sqrt()是在math.h內宣告的 */ int main() /* 主程式開始囉 */ { float x; hello(); /* give the user some message */ printf("X=? "); scanf("%f", &x); while(x >=0 ) { printf(" SQRT(%f)=%7.3f\n", x, mysqrt(x) ); printf("X=? "); scanf("%f", &x); } /* while */ return 0; } /**************主程式到此為止***********/ 自己如何寫求 SQRT?

  21. p4.c – 練習 想 Algorithm(2/2) /*…*/ void hello() { printf("This program will ask you to input a value, say X."); printf("Then it will find the square root of X."); printf("The above process will continue ..."); printf("Negative value will cease the program."); } /* hello() */ double mysqrt(double y) { double myans; myans = -1.0; if(y < 0) printf(" can not process negative number"); else myans = sqrt(y); /* 還沒想好, 先借用程式庫的 sqrt( ) */ return myans; } /*mysqrt*/ 自己如何寫求 SQRT?

  22. p5.c – mysqrt( ) Algorithm(1/2) void hello(void); /*我發誓我會用到一個沒參數的函數叫hello() */ double mysqrt(double); /*也會叫用一個叫mysqrt()的函數,參數是一個double數 */ int main() /* 主程式開始囉 */ { float x; hello(); /* give the user some message */ printf("X=? "); scanf("%f", &x); while(x >=0 ) { printf(" SQRT(%f)=%7.3f\n", x, mysqrt(x) ); printf("X=? "); scanf("%f", &x); } /* while */ return 0; } /**************主程式到此為止***********/ void hello() { printf("This program will ask you to input a value, say X."); printf("Then it will find the square root of X."); printf("The above process will continue ..."); printf("Negative value will cease the program."); } /* hello() */ 自己如何寫求 SQRT?

  23. p5.c – mysqrt( ) Algorithm(2/2) double mysqrt(double x) { /* 這次真的要自己寫求平方根的部份囉! */ double myans; myans = -1; if(x < 0) printf(" can not process negative number\n"); else { myans = 0; /* we guess the root is zero */ while(myans*myans < x) myans++; /* 猜0, 1, 2,...直到太大 */ if(myans*myans>x){ myans =myans-1; /*太大了, 快減掉一 ; 可寫成myans--*/ while(myans*myans < x) myans = myans+0.1; }; if(myans*myans>x){ myans-=0.1; /* this means : myans = myans -0.1 */ while(myans*myans < x) myans += 0.01; } if(myans*myans>x){ myans -= 0.01; while(myans*myans < x) myans += 0.001; } /* I think the precison is good enought now */ } /*else*/ return myans; /** return myans value as our answer */ } /* end of mysqrt*/ 自己如何寫求 SQRT?

  24. mysqrt( ) 問題與思考? • 若想要求到更準呢? • 這程式中函數mysqrt( )所求出的平方根若非剛好就會太大, 這並非最好的做法! • 應該找出最接近的答案, 例如, 求2的平方根時, 最後應考慮 1.415*1.415 與 1.414*1.414 何者較接近2, 如此比較合理! •  加一句 if 檢查看看就可以, 想一想!

  25. 程式 隨便寫 九九乘法表 練習兩層的 for loop Control statement for(i=1; i<=9; ++i) { for(k=1; k<=9; k++) { /****/ } }

  26. 九九乘法表 p6.c #include <stdio.h> int main() { int i,j; printf("\nX |"); for(i=1; i <= 9; i++) printf("%5d", i); printf("\n"); for(i=1 ; i <= 10 ; i++) printf("---- "); for(i=1 ; i<= 9 ; i++) { printf("\n%d |" , i); for(j=1 ; j <= 9 ; j++) printf("%5d", i*j); } printf("\n======\n"); return 0; } 練習兩層的 for loop

  27. 九九乘法表 p6.c 核心 #include <stdio.h> int main() { int i,k; for(i=1 ; i<= 9 ; i++) { printf("第%d 列: " , i); for(k=1 ; k <= 9 ; k++) printf("%5d", i*k); printf("\n"); } printf("\nBye bye!\n"); return 0; } 練習兩層的 for loop

  28. Language Basics • Variables and Data types • Operators • Arithmetic Operators • Relational and Conditional Operators • Shift and Logical Operators • Assignment Operators • Other Operators • Expressions, Statements, and Blocks • Control Flow Statements • The while and do-while Statements • The for Statement • The if/else Statements • The switch Statement • Exception Handling Statements • Branching Statements • 任何高階電腦語言都該有的指述: • /* 註解 */ //也是註解 • 宣告/定義 變數和常數 • 運算符號與運算式和 Assignment = • 算術, 關係運算, 邏輯 • { compound statements; } • 控制指述(control statements) • Sequence • Alternative (selection) • if-else, switch • Loop: for, while, do while • Function/procedure call

  29. for Loop vs. while Loop (C語言) for(i=1 ; i<= 9 ; i++) { /* Loop body */ } 這三個寫法意義完全一樣 i=1; for( ; i<= 9 ; ) { /* Loop body */ i++; } i=1; while( i<= 9 ) { /* Loop body */ i++; }

  30. for Loop vs. while Loop (C 語言)流程 for( ;  ; ) ;  ; while( ) {  ; ; } == 0  != 0    

  31. while Loop (C 語言; C++/Java適用) (1/3) 拿盤子, 拿飲料, 找好位子; while(肚子還餓) { 吃一盤; 喝一杯; 喘一口氣; } 結帳; 回家; All you can eat !

  32. while Loop (2/3) 拿盤子, 拿飲料, 找好位子; while(肚子還餓) { 吃一盤; if(有急事 || 很飽了)break; if(不會渴) continue; 喝一杯; 喘一口氣; } 結帳; 回家; All you can eat !

  33. while Loop (3/3) 拿盤子, 拿飲料, 找好位子; while(不夠本) { 吃一盤; if(有急事 || 很飽了)break; if(不會渴) continue; 喝一杯; 喘一口氣; } 結帳; 回家;

  34. C/C++ Data Types • int - Whole number • long - Large whole numbers (4 bytes integer) • short - small whole numbers (-32768 ~ =32767) • float - Single precision floating point numbers 7 digits of accuracy. • double - Double precision floating point numbers 15 digits of accuracy • boolean - true or false (新版 C++ only) • char- 任何文字符號

  35. 問題與思考 (如何自定資料結構?)solution: struct • 只能用基本的 data type 嗎? • User defined data type? • 考慮寫程式處理全班成績資料, 包括加總平均並排序(Sort)然後印出一份照名次排序的以及一份照學號排序的全班資料 • 如何做 Sort (排序) ? • Sort 時兩學生資料要對調, 要如何對調? • 有很多 array ? 存學號的 array, 存姓名的 array? 存成績的 array? … Bubble sort, Insertion sort, Selection sort

  36. Why struct? • Struct 可以把相關資料 group 在一起 struct student x[99], tmp; /* … */ tmp = x[i]; x[i] = x[k]; x[k] = tmp; • 增加程式可讀性 • 程式更容易維護

  37. What is Object? Class? (1/2) • object 就是“東西”, 就是以前的“變數” • class 就是“類別”, 某種東西的型別 int m, n; /* int 是整數類別 */ /* m, n 都是變數 */ Student x, y; /* Student 是我們自訂類別 */ /* x, y 都是 object */

  38. What is Object? Class? (2/2) • object is an instance of some class • 文法上, C++的class 就是以前的 struct class Student { public: /* …與寫 struct 同 */ }; struct Node { private: /* …與寫 class 同 */ }; ADT(Abstract Data Type): 把資料與其相關函數 (data and their related functions)集合在一個程式單元(program unit), 例如 class 裡面; 讓 東西 用起來更像 東西! 這就是物件導向的概念!

  39. The complete program preparation process

  40. Compiler • Source code  Object code • Interpreter • Source code  Object code  Result Data Compiler vs. Interpreter

  41. Visual Basic 簡介 • BASIC --Bill Gates 的最愛 • Beginner’s All-purpose Symbolic Instruction Code • Basic History • Visual Basic History • 1991 Visual Basic 1.0 • Visual Basic 的特色 • Visual 的特色是「看得見的、視覺的」,就好像「未來執行的結果在設計階段就看得見」 • Object Based (不是 Object Oriented! 有些書寫錯) • Visual Basic 6.0 的幾個版本 • 普及版 • 專業版:增加資料庫程式、主從架構應用程式、網頁製作… • 企業版:再增加遠端存取物件、分散式應用開發…

  42. http://wikipedia.org/

  43. 進入 VB 的IDE (整合發展環境) • 「開始」功能表 • 「程式集/Microsoft Visual Basic 6.0」 Visual Basic 6.0群組 • Microsoft Visual Basic 6.0 • Microsoft Visual Basic 6.0工具 • 線上手冊 • 應用程式安裝精靈

  44. Visual Basic 6.0群組 • Microsoft Visual Basic 6.0 • Microsoft Visual Basic 6.0工具 • 線上手冊 • 應用程式安裝精靈

  45. 查閱 VB6 的線上手冊– MSDN Library

  46. 進入VB 的工作環境 選擇「標準執行檔」 VB 啟動畫面

  47. VB 的視窗群

  48. Example:進入VB的即時運算視窗 • 啟動 VB,然後選取功能表的「檢視」裡面的即時運算視窗。 • 可以改變即時運算視窗的大小。 • 可以改變即時運算視窗的位置。

  49. BASIC 程式的Character set – 字符集(字元集) • 26個大寫英文字母,從A到Z。 • 26個小寫英文字母,從a到z。 • 10個阿拉伯數字,從0到9。 • 28個特殊符號 。 • 所有的中文字 。 BASIC 程式的Reserved words – 保留字 「保留字」(Reserved Word)是由文字符號組合而成,並且由BASIC內部加以定義,像英語裡的單字一樣,具有特殊的意義及使用規則,因此使用者必須依規定來使用這些字,不可隨意使用。例如Print 就是一個保留字,它的用途是印字,所以我們不可當變數如Print=2,否則 VB 會不接受。

  50. BASIC 程式的基本句– 指述(statement) • 運用BASIC的字元集與保留字,我們可以組合成一個個的語句來指示電腦做事情,這一個個的語句稱為「指述」(Statement;敘述) 。 • 立即指述:在即時運算視窗輸入的敘述,一按 Enter鍵後 VB 就會馬上執行,稱為立即指述 。 • 間接指述:至於輸入到程式視窗的指述,則要等到我們開始執行VB應用程式(例如按下開始命令鈕 )才會被執行,稱為間接指述。 “雙引號夾住為字串”

More Related