งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

การประมวลผลสายอักขระ

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "การประมวลผลสายอักขระ"— ใบสำเนางานนำเสนอ:

1 การประมวลผลสายอักขระ
(String Processing)

2 Outline ทบทวน String การประมวลผลสายอักขระ (String Processing)
Pattern Matching Encryption/Decryption Compression

3 ทบทวน String การประกาศตัวแปรเพื่อเก็บข้อความ การรับและการแสดงผลข้อความ
การกำหนดค่าให้กับตัวแปรเก็บข้อความ ฟังก์ชันที่ใช้กับข้อความและอักขระ

4 การประกาศตัวแปรเพื่อเก็บข้อความ
รูปแบบ char ชื่อตัวแปร[จำนวนอักขระ] โดยที่จำนวนอักขระต้องมากกว่าจำนวนอักขระที่เก็บจริง 1 ช่อง เพราะช่องสุดท้ายต้องเก็บอักขระ NULL ซึ่งเขียนแทนด้วย ‘\0’ เพื่อบอกให้ตัวแปลภาษารู้ว่าเป็นข้อความ ตัวอย่าง char name[10]; หมายถึง ตัวแปรชื่อ name เก็บข้อความยาว 9 อักขระ char color[ ]; หมายถึง ตัวแปรชื่อ color เก็บข้อความโดยไม่กำหนดขนาด ซึ่งในกรณีนี้ตัวแปลภาษาจะกำหนดขนาดให้เท่ากับจำนวนอักขระบวก 1

5 การรับข้อมูล การรับข้อความโดยใช้ฟังก์ชัน scanf()
รูปแบบ: scanf (“%s”, ชื่อตัวแปร) ตัวอย่าง #include <stdio.h> char massage[ 20]; void main ( ) { scanf (“%s”, message); printf (“%s”, message); }

6 การรับข้อมูล #include <stdio.h> void main ( ) { char ch;
รับข้อมูลทีละตัวอักษรด้วยฟังก์ชัน getchar ( ) การทำงาน>> เมื่อผู้ใช้กรอกตัวอักษรแล้ว จะต้องกดปุ่ม Enter โปรแกรมจึงจะกลับไปทำงานต่อ โดยอักขระที่ผู้ใช้กรอก จะปรากฏขึ้นมาให้เห็นบนหน้าจอด้วย รูปแบบ: ชื่อตัวแปร = getchar() ตัวอย่าง #include <stdio.h> void main ( ) { char ch; ch = getchar();  printf("You type a character is ...%c \n",ch); }

7 การรับข้อมูล #include <stdio.h> void main ( ) { char str[20];
รับข้อความด้วยฟังก์ชัน gets ( ) รูปแบบ: gets(ชื่อตัวแปร) ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str);  printf("You type a string is ...%s \n",str); }

8 การแสดงผลลัพธ์ #include <stdio.h> void main ( ) { char str[20];
การแสดงผลข้อความโดยใช้ฟังก์ชัน printf รูปแบบ: printf (“%s”, ชื่อตัวแปร); ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str);  printf("You type a string is ...%s \n",str); }

9 การแสดงผลลัพธ์ for(i=0;i<20;i++) #include <stdio.h>
การแสดงผลทีละตัวอักษรโดยใช้ฟังก์ชัน printf รูปแบบ: printf (“%c”, ชื่อตัวแปร); ตัวอย่าง #include <stdio.h> void main ( ) { char str[20]; gets(str); for(i=0;i<20;i++)   printf("You type a string is ...%c \n",str[i] ); }

10 การแสดงผลลัพธ์ #include <stdio.h> void main ( ) { char ch;
การแสดงผลทีละตัวอักษรด้วยฟังก์ชัน putchar() รูปแบบ: putchar (argument) argument : ตัวแปร, ค่าคงที่ , ฟังก์ชัน ตัวอย่าง #include <stdio.h> void main ( ) { char ch; ch = ‘A’;  putchar(ch); }

11 การแสดงผลลัพธ์ puts (“Easy and Fun”); #include <stdio.h>
การแสดงผลข้อความด้วยฟังก์ชัน puts() รูปแบบ: puts(string) เมื่อ string คือตัวแปรที่เก็บข้อความหรือข้อความที่อยู่ใต้เครื่องหมาย “ ” ตัวอย่าง #include <stdio.h> char message[ ] = “C Language”; void main ( ) { puts (message); puts (“Easy and Fun”); } ผลการรัน C Language Easy and Fun

12 ตัวอย่างโปรแกรม ผลลัพธ์ Enter your name : manee dee
#include<stdio.h> char x [30]; void main ( ) { printf (“Enter your name :”); gets(x); printf (“Your name :%s\n”, x); } ผลลัพธ์ Enter your name : manee dee Your name : manee dee

13 การกำหนดค่าให้กับตัวแปรเก็บข้อความ
Example #include<stdio.h> void main ( ) { char mass[11] = “C Language”; char book[4] = {‘A’, ‘B’, ‘C’, ‘\0’}; printf (“%s\n”, mass); printf (“%s\n”, book); printf (“%c\n”, mass[3]); } ผลการรัน C Language ABC a

14 ฟังก์ชันที่ใช้กับข้อความและอักขระ
#include <string.h> ชื่อฟังก์ชัน รูปแบบ ความหมาย strcpy( ) strcpy(str1,str2) คัดลอกข้อมูลจาก str2 ไปยัง str1 strcat( ) strcat(str1,str2) ใช้เชื่อมต่อข้อความ str1 กับ str2 เข้าด้วยกัน และเก็บผลลัพธ์ไว้ใน str1 strlen( ) strlen(s) ใช้หาความยาวของข้อความ s strcmp() strcmp(str1,str2) เปรียบเทียบข้อความ str1 กับ str2

15 ฟังก์ชันที่ใช้กับข้อความและอักขระ
#include <ctype.h> ชื่อฟังก์ชัน รูปแบบ ความหมาย tolower( ) tolower(ch) เปลี่ยนตัวอักษรจากตัวใหญ่ให้เป็นตัวเล็ก toupper( ) toupper(ch) เปลี่ยนตัวอักษรจากตัวเล็กให้เป็นตัวใหญ่

16 ตัวอย่างโปรแกรมการใช้ฟังก์ชันของ string
if (res>0) { printf("greater\n"); strcpy(temp,name1); strcpy(name1,name2); strcpy(name2,temp); printf("%s %s\n",name1,name2); } else if (res<0) printf("less\n"); else // res == 0 printf("same and length = "); printf("%d\n",strlen(name1)); return 0; #include<stdio.h> #include<string.h> int main() { char name1[10], name2[10], temp[10]; int res; printf("enter 2 string : "); scanf("%s %s",name1,name2); // cannot use like this //name1 = "dararat"; res = strcmp(name1,name2);

17 Don’t Sleep Yet…

18 String Processing Pattern Matching Encryption/Decryption Compression

19 Pattern Matching given a string of n characters called text (T) ,and a string of m (m<=n) characters called pattern (P) find a substring of the text that match the pattern

20 Pattern Matching Algorithms
Brute-Force Boyer-Moore Knuth-Morris-Pratt

21 Pattern Matching Algorithms
Input: An array T[n] of n characters representing a text and an array P[m] of m character representing a patterm Output: The index of the first character in the text that starts a matching substring or -1 if the search is unsuccessful

22 Brute-Force algorithm
Algorithm for i  to n-m do j 0 while j< m and P[j] = T[i+j] do j j+1 if j = m return i return -1

23 Brute-Force algorithm
Example 1 T = “abacaabaccabacababb” P = “abacab” abacaabaccabacababb abacab …. Result = “match” 27 comparisons

24 Brute-Force algorithm
Example 2 T = “a pattern matching algorithm” P = “algor” apattern matching algorithm algor a lgor Result = “match” ? comparisons

25 Boyer-Moore algorithm
create a lookup table from P เช่น T = “abacaabadcabacababb” P = “abacab” cha a b c d Last(cha) 4 5 3 -1 ***Last(cha) = last position of the character in P

26 Boyer-Moore algorithm
compare from right to left 1-by-1 if not equal at position i in T, if Ti is in lookup table n = |last(Ti)-last(Pi)| shift comparison to the next n location of T if Ti is not in lookup table shift comparison to the next m (ex. m=6) location of T

27 Boyer-Moore algorithm
Example 1 T = “abacaabadcabacababb” P = “abacab” (m = 6) abacaabadcabacababb abacab cha a b c d Last(cha) 4 5 3 -1 13 comparisons

28 Boyer-Moore algorithm
Example 2 T = “a pattern matching algorithm” P = “algor” (m = 5) a pattern matching algorithm algor cha a l g o r Last(cha) 1 2 3 4 ? comparisons

29 Boyer-Moore algorithm
แบบฝึกหัด T = “a pattern matching algorithm” P = “rithm” (m = 5) cha Last(cha)

30 Encryption/Decryption
Cryptographic computations encryption/decryption , digital signatures To support secure communication over the Internet – using cryptographic computations for information security services

31 Encryption/Decryption
Before sending text, the original text – called plaintext , is encrypted into an unrecognizable string – called ciphertext After receiving the ciphertext, it is decrypted back to the plaintext encryption Plaintext Ciphertext decryption

32 Encryption/Decryption
Basic techniques : Substitution , Transposition , Bit manipulation the essential thing for each method is the secret key Well-known Algorithms DES (Data Encryption Standard) RSA (Rivest-Skamir-Adleman) Knapsack

33 Encryption/Decryption
Example Substitution (n = 4) Transposition (3x6) Bit manipulation (b = 3)

34 Compression given a string X (ascii, unicode), encode into a small binary string Y Huffman coding algorithm – based on character frequency to construct a binary tree , uses greedy method

35 Compression char. freq. a b d e f h i k n o r s t u v 9 5 1 3 7 4 2
Eample “a fast runner need never be afraid of the dark” frequency of each character char. a b d e f h i k n o r s t u v freq. 9 5 1 3 7 4 2

36 Compression a = ‘010’ r = ‘011’ e = ‘100’ d = ‘1010’ Eample (ต่อ)
Huffman tree 46 1 27 19 1 1 12 15 10 1 1 1 9 5 e a r 1 7 d 3

37 การบ้าน Knuth-Morris-Pratt algorithm


ดาวน์โหลด ppt การประมวลผลสายอักขระ

งานนำเสนอที่คล้ายกัน


Ads by Google