|
今天,我把C語言課本上第211頁的那個程序改成了Windows窗口程序的版本 |
一派掌門 二十級 |
運行結果:
|
一派掌門 二十級 |
|
|
一派掌門 二十級 |
點擊OK按鈕後出結果:
|
|
一派掌門 二十級 |
 還能支持漢字(因為wchar_t字符數組支持漢字字符)
|
|
一派掌門 二十級 |
|
|
一派掌門 二十級 |
生成的程序文件:
|
|
一派掌門 二十級 |
|
|
一派掌門 二十級 |
 詳細信息
|
|
一派掌門 二十級 |
程序核心代碼:(鼠標點擊OK按鈕後執行的內容) void CInsertStrDlg::OnBnClickedOk() { // TODO: Add your control notification handler code here //CDialogEx::OnOK(); int len1, len2, i, pos; UpdateData(); len1 = mString1.GetLength(); len2 = mString2.GetLength();
/* Find the position */ for (pos = 0; pos < len1; pos++) { if (mString1.GetAt(pos) == mString2.GetAt(0)) break; // found; } // When not found, pos == len1
/* Move */ mResult = mString1 + CString(' ', len2 + 1); // lengthen the string if (pos != len1) { for (i = len1; i >= pos - 1 && i >= 0; i--) { // At first when i == len1, the character is '\0' // and then '\0' will be moved to the end mResult.SetAt(i + len2 - 1, mString1.GetAt(i)); } }
/* Copy chars from s2 to s1 */ // copy the first character of s2 if (pos == len1) mResult.SetAt(len1 + len2, '\0'); // when not found, the moving wasn't executed, so '\0' was not copied for (i = 0; i < len2; i++) mResult.SetAt(pos + i, mString2.GetAt(i));
UpdateData(false); }
其中mString1綁定的是第一個文本框,mString2是第二個文本框,mResult是第三個只讀的文本框。
|
|
一派掌門 二十級 |
初始化函數(BOOL CInsertStrDlg::OnInitDialog())中的初始化代碼: // TODO: Add extra initialization here mString1 = "abcdef"; mString2 = "d45"; UpdateData(false); 這是在窗口程序啟動後默認在兩個文本框中顯示的內容。
|
|
一派掌門 二十級 |
窗口佈局:
|
|
一派掌門 二十級 |
Ctrl + D鍵設置的Tab鍵訪問順序:  一定要把第一個文本框的號碼設為比其他TAB可切換控件的號碼小,這樣程序啟動後光標才能默認落在第一個文本框中。
|
|
一派掌門 二十級 |
最後一個文本框的Read Only(只讀)要設置為True:
|
|
一派掌門 二十級 |
 這是給文本框綁定變量的方法。
|
|
一派掌門 二十級 |
 Category要選擇Value類型,變量類型為CString,變量名為mString1。 窗口上其他的文本框類似。 設置後程序中就可以直接讀取文本框中的內容了。 UpdateData();是把文本框中的內容更新到變量中。 UpdateData(false);是把變量中的內容顯示到文本框中。
|
|
一派掌門 二十級 |
 程序屬性值
|
|
一派掌門 二十級 |
|
|
一派掌門 二十級 |
|
|
見習魔法師 二級 |
可以哦。
|
|
一派掌門 二十級 |
可以用C語言庫函數把insert函數改寫成更簡單且更容易理解的版本: #include <stdio.h> #include <stdlib.h> #include <string.h>
void insert(char *s1, const char *s2) { int len1 = strlen(s1); int len2 = strlen(s2); int pos; char *ch = strchr(s1, *s2); if (ch == NULL) // when not found memcpy(s1 + len1, s2, len2 + 1); // including '\0' at the end else { pos = *ch - *s1; memmove(ch + len2 - 1, ch, len1 - pos + 1); // including '\0' at the end memcpy(ch, s2, len2); } }
int main(int argc, char *argv[]) { char str[50] = "abcdefgh"; char s2[5] = "d45"; insert(str, s2); puts(str); strcpy(str, "abcdef"); strcpy(s2, "45"); insert(str, s2); puts(str); strcpy(str, "definition"); strcpy(s2, "desk"); insert(str, s2); puts(str); return 0; }
|
|
一派掌門 二十級 |
可以用C語言庫函數把insert函數改寫成更簡單且更容易理解的版本: #include <stdio.h> #include <stdlib.h> #incl...
#include <stdlib.h> 這句話可以不要
|
|
一派掌門 二十級 |
void insert(char *s1, const char *s2) { int len1 = strlen(s1); int len2 = strlen(s2); int pos; char *ch = strchr(s1, *s2); // 在s1中查找s2的第一個字符,返回該字符在s1中的位置地址,如果沒有找到,返回NULL if (ch == NULL) // 如果沒有找到 memcpy(s1 + len1, s2, len2 + 1); // 直接把s2字符串連同末尾的\0一起複製到s1[len1]處,也就是s1的字符串結尾處(\0處) else { // 以abcdef, d45為例 pos = *ch - *s1; // 計算找到的字符d在s1中的位置(數字,從0開始),計算結果為3 memmove(ch + len2 - 1, ch, len1 - pos + 1); // 移動字符串空出空間。把def\0向右移動len2-1個位置(也就是2個位置),共移動len1 - pos + 1個字節,也就是4個字節。 memcpy(ch, s2, len2); // 複製字符串。把d45(不含末尾的\0)複製到abc[ded]ef的[ded]處覆蓋掉。 // 其實程序在這裏還可以進行修改。因為字符「d」沒必要複製 } }
|
|