VC++的EDIT问题

darkendarken 2005-11-30 07:50:07
本人初学,遇到个问题!!_!!
  就是在一个MFC AppWizard.exe里的dialog加个EDIT框
  怎么把从键盘得到的string类型的比如
  1,2,3
  4,5,6
  6,7,8
  在edit里就是这么输入的,矩阵大小不定
  如何转换成内存中的int类型多维数组比如int a[3][3]
  这个解析函数应该怎么写?
我胡乱写了个,但是不对
哪位大哥帮研究下,谢谢了
CString strTest1 = cInput1;
CString strNum1 , strNum12;
int nRet1,nRet2,nDot1,nEnter1=0;
nRet1 = strTest1.Find('\r' , nEnter1);
i = 0;
j = 0;
while (nRet1 !=-1)
{
strNum1 = strTest1.Mid(nEnter1,nRet1 - nEnter1);
nRet2 = strNum1.Find(',' , nDot1);
while (nRet2 != -1)
{
strNum12 = strNum1.Mid(nDot1, nRet2 - nDot1);
Allocation[i][j] = atoi(strNum12);
nDot1 = nRet2 + 1;
nRet2 = strNum1.Find(',' , nDot1);
j++;
}
strNum12 = strNum1.Right (strNum1.GetLength() - nDot1);
Allocation[i][j] = atoi(strNum12);
nEnter1 = nRet1 + 1;
nRet1 = strTest1.Find('\r', nEnter1);
i++;
}

...全文
122 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
djfu 2005-11-30
  • 打赏
  • 举报
回复
a[i][k] = sa[i * nMaxCol + k]; ===> atoi(sa[i * nMaxCol + k].GetBuffer(0), a[i][k], 10);

This is the code I just write without compile and debug in VC IDE environment,
so if problems occurs, you should debug it and eridicate the errors.

djfu 2005-11-30
  • 打赏
  • 举报
回复
NOTICE!!!

void AnalyzeALine(CString sLine, CStringArray &sa, int &nElementCount)
{
int nColCount = 0;
char seps[] = " ,";
char *token = NULL;
CString sElement;
while( token != NULL )
{
token = strtok( NULL, seps );
sElement.Format("%s", token);
sa.Add(sElement);

nColCount++;
}

}

should be alterd to :

void AnalyzeALine(CString sLine, CStringArray &sa, int &nElementCount)
{
int nColCount = 0;
char seps[] = " ,";
char *token = NULL;
CString sElement;
while( token != NULL )
{
token = strtok( NULL, seps );
sElement.Format("%s", token);
sa.Add(sElement);

nColCount++;
}
nElementCount = nColCount; // added
}
djfu 2005-11-30
  • 打赏
  • 举报
回复
#include <string.h>
#include <stdio.h>
#include <map>

void AnalyzeALine(CString sLine, CStringArray &sa, int &nElementCount)
{
int nColCount = 0;
char seps[] = " ,";
char *token = NULL;
CString sElement;
while( token != NULL )
{
token = strtok( NULL, seps );
sElement.Format("%s", token);
sa.Add(sElement);

nColCount++;
}

}

void AnalyzeText()
{
CString sText, sALine;
CString sa;
map< int, int> mp;

char seps[] = " \n";
char *token = NULL;
int nRow = 0, nCol = 0, nMaxCol = 0, nRowNo = 0, nElementCount = 0;

// determin the max columon count
token = strtok( sText.GetBuffer(0), seps );
while( token != NULL )
{
token = strtok( NULL, seps );
sALine.Format("%s", token);
AnalyzeALine(SALine,sa, nElementCount);
if(nElementCount > nMaxCol)
nMaxCol = nElementCount;

mp.insert(make_pair(nRowNo, nElementCount));
nRow++; nRowNo++;
}

// allocate space
int **a;
int i,j;
a = new int[nRow];
for( i = 0; i < nRow; i ++)
a[i] = new int[nMaxCol];

a[][] = {0};
i = 0;
int nRowElementCount;
int k;
for(j = 0; j < sa.GetCount(); j ++)
{
nRowElementCount = mp[i];
for(k =0; k < nRowElementCount; k ++)
{
a[i][k] = sa[i * nMaxCol + k];
}
}

// print the Matrix now
for(i = 0; i < nRow; i++)
{
for(j = 0; j < nMaxCol; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
RobinCome 2005-11-30
  • 打赏
  • 举报
回复
看着你的代码有点晕啊,
先将输入保存为一个字符串;
主要是确定数组的行列喽,逗号或空格数+1就是列数(注意比较取最大值),回车数当然就是行数,这样就可以定义一个二维数组来存放操作数吧。
然后,过滤掉符号 用for()逐行赋值。
嗯,成了
darkendarken 2005-11-30
  • 打赏
  • 举报
回复
踢一脚

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧