?? randomarrayfromtxt.cpp
字號:
#include "stdafx.h"
#include "RandomArrayFromTxt.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRandomArrayFromTxt
CRandomArrayFromTxt::CRandomArrayFromTxt()
{
}
CRandomArrayFromTxt::~CRandomArrayFromTxt()
{
}
BEGIN_MESSAGE_MAP(CRandomArrayFromTxt, CWnd)
//{{AFX_MSG_MAP(CRandomArrayFromTxt)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRandomArrayFromTxt message handlers
void CRandomArrayFromTxt::GetRandomArrayFromTxt(int m_Collect_Times,double *a_Random,int m_Txt_Line,int m_Txt_Row,int m_Txt_Spacing)
{
int m,mx,my,cl;
srand((unsigned)time(NULL)); // 生成時間種子
m=rand()%m_Txt_Line; // 返回一個0-m_Txt_Line-1的隨機數,即查表的起始位置
mx=m%m_Txt_Row; // 查表起始位置的列號,文件頭為0行0列
my=m/m_Txt_Row; // 查表起始位置的行號
cl=(m_Collect_Times+mx-1)/m_Txt_Row+1; // 需要從數表中讀取的行數
// 打開txt文本的數表。
CStdioFile file1( "C:\\Documents and Settings\\Yg\\桌面\\7-15-1130\\Rd\\Debug\\ramdom1000.txt",CFile::modeNoTruncate | CFile::modeRead | CFile::typeText);
CString strc;
int ct=0; // 用于記錄當前已經取得的隨機數個數
for(m=0;m<my;m++)
{
file1.ReadString(strc);
}
// 取出有用的行,直到取夠為之
for(int n=0;n<cl;n++)
{
if(n+my==m_Txt_Line)
file1.SeekToBegin(); // 數表不夠長,重新定位到文件頭
file1.ReadString(strc);
if(n==0) // 如果是取得的第一行
{
int aw1,aw2;
aw1=strc.GetLength();
for(m=0;m<mx+1;m++) // 過掉前面無用的空格
{
aw1=strc.GetLength();
aw2=strc.Find(" "); // 找到作為間隔的前三個空格所在位置
strc=strc.Right(aw1-aw2-m_Txt_Spacing); // 取這三個空格右邊的所有字符串
}
// 下面取出剩下的字符串中每三個空格前面的字符串,就是所要查一個數據
for(m=0;m<m_Txt_Row-mx&&ct<m_Collect_Times;m++)
{
aw2=strc.Find(" "); // 找到作為間隔的后三個空格所在位置
if(mx==m_Txt_Row-1||m==m_Txt_Row-mx-1)
a_Random[ct]=atof(strc);
else
a_Random[ct]=atof(strc.Left(aw2)); // 保存數據到數組
aw1=strc.GetLength();
aw2=strc.Find(" "); // 找到作為間隔的前三個空格所在位置
strc=strc.Right(aw1-aw2-m_Txt_Spacing); // 取這三個空格右邊的所有字符串
ct++;
}
}
else
{
GetRandomArrayFromALine(strc,a_Random,m_Collect_Times,m_Txt_Row,m_Txt_Spacing,ct);
ct+=m_Txt_Row;
}
}
file1.Close();
}
// 從一行中取出數字
void CRandomArrayFromTxt::GetRandomArrayFromALine(CString strc,double *a_Random,int m_Collect_Times,int m_Txt_Row,int m_Txt_Spacing,int ct)
{
int aw1,aw2;
aw1=strc.GetLength();
aw2=strc.Find(" ");
strc=strc.Right(aw1-aw2-m_Txt_Spacing); // 取這三個空格右邊的所有字符串
for(int m=0;m<m_Txt_Row&&ct<m_Collect_Times;m++)
{
aw2=strc.Find(" ");
if(m==m_Txt_Row-1)
a_Random[ct]=atof(strc);
else
a_Random[ct]=atof(strc.Left(aw2)); // 保存數據到數組
aw1=strc.GetLength();
aw2=strc.Find(" "); // 找到作為間隔的前三個空格所在位置
strc=strc.Right(aw1-aw2-m_Txt_Spacing); // 取這三個空格右邊的所有字符串
ct++;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -