组队项目四则运算成果

1、四则运算成果展示

2、编程中遇到的问题

  (1)由于采用的是MFC进行的编程,首先最大的问题就是CString和String、int、还有char*之间的转换,经过学习,转换方式如下:

  String和CString的转换:

  string str="ksarea";
  CString cstr(str.c_str());//或者CString cstr(str.data());初始化时才行
  cstr=str.c_str();或者cstr=str.data();
  str=cstr.GetBuffer(0); //CString -> string
  cstr.Format("%s", str.c_str()); //string->CString
  cstr.Format("%s", str.data()); //string->CString
  str = LPCSTR(cstr); //CString->string
  /*c_str()和data()区别是:前者返回带'/0'的字符串,后者则返回不带'/0'的字符串*/

  CString和int的转换

  int i=123;
  CString str;
  str.Format("%d",i);//int->CString 其他的基本类型转化类似
  i=ato i(str);//CString->int 还有(atof,atol)
  CStringcstr="ksarea";
  char*和CString的转换
  
char* p temp=cstr.getbuffer(0);
  char* str;
  strcpy(str,ptemp);//CString->char*
  cstr.releasebuffer(-1);
  char*str="xxx";
  CStringc str=str;//char*->CString string类型不能直接赋值给CString
 
  (2)Static静态框的显示文本问题:
  MFC采用的CStdioFile类型进行的编程
//创建文件
CStdioFile file;
file.Open("ts.txt",CFile::modeCreate|CFile::modeWrite);
//写入文件
CString str;
str.Format("%s\r\n","hello!I am talkingmute!");(在末尾添加\r\n会导致写出来的文档修改后出现格式错乱,只能写\n)
file.Seek(0,CFile::end);
file.WriteString( str );
//关闭文件
file.Close();
比如:读文件的例子
CString strText = “”;
CString szLine = “”;
//打开文件
CStdioFile file;
file.Open("ts.txt",CFile::modeRead);
//逐行读取字符串
while( file.ReadString( szLine ) )
{
strText += szLine;
}
MessageBox(strText);
//关闭文件
file.Close();

3、收获

  了解了关于MFC编程和各个框架之间的关系,例:字体设置和显示
  
GetDlgItem(IDC_STATIC2)->SetWindowText("您共答对"+ppp+"道题");
    GetDlgItem(IDC_BUTTON1)->SetWindowText("开始出题");
    GetDlgItem(IDC_BUTTON2)->SetWindowText("计算结果");
    GetDlgItem(IDC_BUTTON3)->SetWindowText("结束");
    CFont font;
    font.CreatePointFont(185, "华文新魏");
    GetDlgItem(IDC_STATIC1)->SetFont(&font);
    CStdioFile Rfile(_T("a.txt"), CFile::modeRead); //定义并初始化一个CStdioFile类的对Rfile
    //一行一行的读入字符串
    while (Rfile.ReadString(csExt))
    {    
        strText += csExt + '\n';
    }
    GetDlgItem(IDC_STATIC1)->SetWindowText(strText);

  2、插入变量:

  

  
原文地址:https://www.cnblogs.com/ji5jin45/p/5381343.html