我正在创建一个随机数生成器,当我遇到一些错误时,它将 Min、Max、Avg、随机数和冒泡排序的数字保存到一个 excel 文件中,由于某种原因,我无法从 c++ MFC 调用这个函数,如下所示,我怎样才能使这项工作?
//***************Number generator function*******************
void number_Generator(double dblArray[], int length)
{
srand((unsigned)time(0));
double rndDbl;
int rndInt;
double rndAvg = 0;
int counter = 0;
double temp = 0;
Final_Avg = rndAvg / counter; // final average to display
double lDbl=0, hDbl=Random_Cap;
int lInt = 0, hInt=1;
double dblRange=(hDbl-lDbl)+1;
int intRange=(hInt-lInt)+1;
for(int index=0; index<Samples_To_Create; index++)
{
rndInt = lInt+int(intRange*rand()/(RAND_MAX + 1.0));
rndDbl = lDbl+double(dblRange*rand()/(RAND_MAX + 1.0));
// random number if statement
if (rndInt == 0){
rndDbl = -(rndDbl);
}
//start of Min/Max if statements
if (rndMin == 0){
rndMin = rndDbl;
}
else if (rndDbl < rndMin){
rndMin = rndDbl;
}
if (rndMax == 0){
rndMax = rndDbl;
}
else if (rndDbl > rndMax){
rndMax = rndDbl;
} //end of Min Max if statements
temp = rndDbl;
rndAvg += temp;
dblArray[counter] = temp;
counter++;
}
}
}
它是从按钮单击处理程序中调用的:
void CECET_MFC_Dialog_Based_IntroDlg::OnBnClickedCreate()
{
UpdateData(true);
number_Generator(dblArray, 100); //100 means generate 100 random numbers
UpdateData(false);
}
公共访问变量作为未声明的标识符出现,即使它们的定义如下所示。
CECET_MFC_Dialog_Based_IntroDlg::CECET_344_MFC_Dialog_Based_IntroDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CECET_344_MFC_Dialog_Based_IntroDlg::IDD, pParent)
, Final_Avg(0)
, rndMax(0)
, rndMin(0)
, Samples_To_Create(0)
, Random_Cap(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CECET_MFC_Dialog_Based_IntroDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT3, Final_Avg);
DDX_Text(pDX, IDC_EDIT4, rndMax);
DDX_Text(pDX, IDC_EDIT5, rndMin);
DDX_Text(pDX, IDC_EDIT2, Samples_To_Create);
DDX_Text(pDX, IDC_EDIT1, Random_Cap);
}