Presentation is loading. Please wait.

Presentation is loading. Please wait.

Q6. 某學校將學生的電話號碼存貯在一個文字檔'telist.txt'。 在這交字檔中,每行有14個字符,代表學生班別、班號和電話號碼,

Similar presentations


Presentation on theme: "Q6. 某學校將學生的電話號碼存貯在一個文字檔'telist.txt'。 在這交字檔中,每行有14個字符,代表學生班別、班號和電話號碼,"— Presentation transcript:

1 Q6. 某學校將學生的電話號碼存貯在一個文字檔'telist.txt'。 在這交字檔中,每行有14個字符,代表學生班別、班號和電話號碼,
由空白字符分隔,如下所示: 班別 班號 電話號碼 1 A 2 3 4 5 6 7 8 空白字符 空白字符 部分文字檔如下所示: 陳先生編寫了一個程序來改動學生的電話號碼。這個程序需把該交字檔更新,並在VDU上顯示如下的輸出。 1B 1C 1C 1D 1E Enter class name: 1D Enter class no.: 04 Enter new telephone no.: Record has been modified successfully. Enter class name: 1D Enter class no.: 44 No such record! CS2004

2 fp=fopen("telist.txt","r+"); // read+write count=0; while(!feof(fp)){
1B 1C 1C 1D 1E FILE *fp; char data[100][20]; int count; void init(){ char line[20]; fp=fopen("telist.txt","r+"); // read+write count=0; while(!feof(fp)){ fgets (line, 20, fp); line[strlen(line)-1]='\0'; strcpy (data[count], line); count++; } fclose(fp); CS2004

3 int search(char class[], char no[]){ int i, pos=-1; // not found
data[i] 1A 04 1B 1C 1C 1D 1E int search(char class[], char no[]){ int i, pos=-1; // not found char myclass[3], cno[3]; for(i=0; i<count; i++){ strncpy (myclass, data[i], 2); // 1D strncpy (cno, data[i]+3, 2); // 05 if((strcmp(myclass,class))==0 && (strcmp(cno,no))==0){ pos=i; break; } return pos; CS2004

4 void updateTel(int pos){ char newtel[9]; int i; printf ("輸入新電話號碼: ");
gets (newtel); for (i=0;i<8;i++) data[pos][i ]=newtel[i]; printf ("紀錄已更新\n"); } void updateRec(){ char class[3], no[3]; int pos; do{ printf("班別: "); gets(class); if(strlen(class)==0) break; fflush(stdin); printf("號數: "); gets(no); }while(1); } +6 pos = search(class, no); if (pos>0) updateTel(pos); else printf("找不到!\n"); s[0] s[6] 1B 1C 1C 1D 1E CS2004

5 for (i=0;i<count;i++) fprintf (fp, "%s\n", data[i]);
void updateFile(){ int i; rewind(fp); fclose(fp); } for (i=0;i<count;i++) fprintf (fp, "%s\n", data[i]); main(){ init(); // tellist.txt  data[i] updateRec(); // change tel updateFile(); // data[i]  tellist.txt } CS2004

6 ABCDEFGHIJKLMNOPQRSTUVWXYZ
const int shift=13; char inStr[80]="ABC"; int i, tmp; main(){ while(strlen(inStr)>0){ printf("Please input the text原文 : "); gets(inStr); printf("The coded message is加密後 : "); for(i=0;i<strlen(inStr);i++){ } printf("\n"); ABCDEFGHIJKLMNOPQRSTUVWXYZ if ((inStr[i]>='A') && (inStr[i]<='Z')){ tmp = inStr[i]+shift; if (tmp>90) tmp=26; inStr[i] = tmp; } printf ("%c", inStr[i]); CS2004

7 (a)當輸入下列文字後,試寫出該程序的輸出。 (i) BAR (ii) ONE (b)試簡述如何利用上列程序,把加密文字解碼。
"A..." 65+13=78 N "AB.." 66+13=79 O ONE BAR if ((inStr[i]>='A') && (inStr[i]<='Z')){ tmp = inStr[i] if (tmp ) tmp inStr[i] = tmp; } printf ("%c", inStr[i]); shift; < =26; CS2004

8 ABCD...YZ GHIJ...EF 小芬獲得小明的加密文字。她被告知可利用下列步驟,破譯小明的加密文字
步驟1: 數出在加密文字中每個字母出現的次數。 步驟2: 一般來說,從統計數字得知‘E’是在文字段落中最常用的字母。 因此,小芬將在加密文字中出現次數最多的字母更換為‘E’, 把下一個按字母順序排列的字母更換為‘F’,如此類推。 例如, 若'Y'是在加密文字中出現次數最多的字母,便將'Y'更換為'E', 其後更換'Z'為'F',更換'A'為'G',更換'B'為'H',…,如此類推。 (c)小芬編寫了一個程序,數出用戶指定文字檔內,每個字母出現的次數,並找出出現次數最多的宇母。試寫出下列方格內的內容來完成這個程序。 char filename[20], inStr[80]; FILE *inFile; int freq[26], i; char c, max; ABCD...YZ GHIJ...EF CS2004

9 for(c=0; c<26; c++) freq[c]=0;
main(){ for(c=0; c<26; c++) freq[c]=0; puts("Please input the name of the file to be processed :"); printf("abc.txt %c",'\r'); gets(filename); if(strlen(filename)==0) strcpy(filename,"abc.txt"); inFile=fopen(filename,"r"); while (!feof(inFile)){ fgets(inStr,80,inFile); for (i=0;i<strlen(inStr);i++) if (isupper(inStr[i])) freq[inStr[i] ]++; } fclose(inFile); -65 max=0; i = freq[max]; for (c=1; c<26; c++){ if (freq[c]>0) printf("freq[%c] = %d\n",c+'A',freq[c]); if (freq[c]>freq[max]) max=c; } printf ("The most frequent 常見 letter is %c\n", max+65); CS2004

10 (d)試舉出這個方法,破譯含大楷字母段落的兩個弱點。
'E' 並非唯一『出現次數最多』的宇母,因為 - 可能有多於一個『出現次數最多』的宇母 - 文章中有很多縮寫字或特別名詞 - 文章字數不足,沒有代表性 Letter 'E' is not the only most frequent letter because - there may be more than 1 highest frequent letter - the passage contains too many abbreviations 縮寫 or special terms - the passage is not long enough CS2004

11 Q9. 剔除了空白,正讀和反讀都一樣的詞或句子,稱為迴文(palindrome)。 兩個迴文的例子如下所示
(1) Rats live on no evil star (2) Was it a car or a cat I saw 彼得編寫了一個程序,查證一段字符串是否迥文。下列為程序的樣本輸出: 樣本輸出1 Please input a string: Was it a car or a cat I saw It is a palindrome. 樣本輸出2 Please input a string: no pain no gain It is not a palindrome. CS2004

12 字符串變量,用來存貯剔除了空白字符的字符串
標識符 描述 inp 字符串變量,用來存貯輸入字串 nospace 字符串變量,用來存貯剔除了空白字符的字符串 i,j 整數變量,用作計數器 result 布爾變量,用來存貯查證的結果 作答本題時,考生不得加入新的變量,否則不獲給分。 (a)編寫過程RemoveSp,剔除存貯在變量s內的字符串中的空白字符, 並把這字符串存貯至變量s。 (b)編寫過程upper,將變量s中的字母改成大楷。 (c)編寫過程palTest,查證變量s中的字符串是否迴文。 CS2004

13 void removeSpace (char s[]){ char nospace[80]; int i,j=0;
strcpy(nospace,s); strcpy(s,nospace); } Was it a car or a cat I saw WasitacaroracatIsaw for(i=0; i<strlen(s); i++) if (s[i]!=' ') nospace[j++] = s[i]; nospace[j]='\0'; void upper (char s[]){ int i; for(i=0; i<strlen(s); i++) s[i]=toupper(s[i]); } WasitacaroracatIsaw WASITACARORACATISAW CS2004

14 int palTest(char s[]){ int i,j,n; n=strlen(s);
for(i=0; i<n/2; i++ ) if(s[i]!=s[n-1-i]) return 0; return 1; } n=5 j = n-1; ,j-- ABCBA 01234 if(s[i]!=s[j]) return 0; main(){ char inp[80]; do{ printf("Please input a string: "); gets(inp); removeSpace(inp); upper(inp); }while(1); } if(palTest(inp)==1) printf("It is a palindrome.\n"); else printf("It is not a palindrome.\n"); CS2004


Download ppt "Q6. 某學校將學生的電話號碼存貯在一個文字檔'telist.txt'。 在這交字檔中,每行有14個字符,代表學生班別、班號和電話號碼,"

Similar presentations


Ads by Google