|  | 今天,我把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”没必要复制
 }
 }
 |