13,825
社区成员
发帖
与我相关
我的任务
分享
bool Finish = false;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int F1[1]/*一等奖*/,
F2[5]/*二等奖*/,
F3[10]/*三等奖*/;
TStaticText *ShowText;
int count=500;
int *tls=new int[500];
try {
//1-500已经在列表当中
for(int i = 0; i < 500; i++)
tls[i] = i+1;
//>>>>
//randomize();
//<<<<
//抽10个三等奖
for(int j3=0;j3<10;j3++){
int Index = random(count);
//>>>>
ShowText = dynamic_cast<TStaticText *>(FindComponent("F3_"+IntToStr(j3)));
Finish = false;
Caption = "正在抽取第"+IntToStr(j3+1)+"/10位三等奖...";
while (!Finish){
if (Index == count - 1) {
Index = 0;
}
else
Index ++;
ShowText->Caption = IntToStr(tls[Index]);
Application->ProcessMessages();
}
//<<<<
F3[j3] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F3[j3];
}
count --;
}
//>>>>
//randomize();
Caption = "10位三等奖抽取完毕.";
Finish = false;
while (!Finish){
Sleep(1);
Application->ProcessMessages();
}
//<<<<
//抽5个二等奖
for(int j2=0;j2<5;j2++){
int Index = random(count);
//>>>>
ShowText = dynamic_cast<TStaticText *>(FindComponent("F2_"+IntToStr(j2)));
Finish = false;
Caption = "正在抽取第"+IntToStr(j2+1)+"/5位三等奖...";
while (!Finish){
if (Index == count - 1) {
Index = 0;
}
else
Index ++;
ShowText->Caption = IntToStr(tls[Index]);
Application->ProcessMessages();
}
//<<<<
F2[j2] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F2[j2];
}
count --;
}
//>>>>
//randomize();
Caption = "5位二等奖抽取完毕.";
Finish = false;
while (!Finish){
Sleep(1);
Application->ProcessMessages();
}
//<<<<
//抽出一等奖
int Index = random(count);
//>>>>
ShowText = F1_0;
Finish = false;
Caption = "正在抽取第1/1位一等奖...";
while (!Finish){
if (Index == count - 1) {
Index = 0;
}
else
Index ++;
ShowText->Caption = IntToStr(tls[Index]);
Application->ProcessMessages();
}
//<<<<
F1[0] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F1[0];
}
count --;
//抽奖完毕,一等奖在F1中,二等奖在F2中,三等奖在F3中
AnsiString Result;
Result.sprintf("抽奖完毕!\r\n一等奖:%d\r\n二等奖:%d,%d,%d,%d,%d\r\n三等奖:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",F1[0],F2[0],F2[1],F2[2],F2[3],F2[4],F3[0],F3[1],F3[2],F3[3],F3[4],F3[5],F3[6],F3[7],F3[8],F3[9]);
ShowMessage(Result);
}
__finally{
delete[] tls;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Finish = true;
}
//---------------------------------------------------------------------------
int F1[1]/*一等奖*/,
F2[5]/*二等奖*/,
F3[10]/*三等奖*/;
int count=500;
int *tls=new int[500];
try {
//1-500已经在列表当中
for(int i = 0; i < 500; i++)
tls[i] = i+1;
randomize();
//抽10个三等奖
for(int j3=0;j3<10;j3++){
int Index=random(count);
F3[j3] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F3[j3];
}
count --;
}
randomize();
//抽5个二等奖
for(int j2=0;j2<5;j2++){
int Index=random(count);
F2[j2] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F2[j2];
}
count --;
}
randomize();
//抽出一等奖
int Index=random(count);
F1[0] = tls[Index];
if (count != Index) {
tls[Index] = tls[count-1];
tls[count-1] = F1[0];
}
count --;
//抽奖完毕,一等奖在F1中,二等奖在F2中,三等奖在F3中
}
__finally{
delete[] tls;
}