Private Sub Command1_Click()
If IsNumeric(Text1) And Len(Text1) = 7 Then '判断输入的是否为7位数字
Randomize
a = Format(Int(Rnd * 9999999 + 1), "0000000") '生成开奖号码
Label1.Caption = "本期开奖号码为:" & a
For i = 1 To 7 '逐位判断是否相同,b为相同个数
If Mid(Text1, i, 1) = Mid(a, i, 1) Then
b = b + 1
End If
Next
If b = 7 Then
Label2.Caption = "恭喜你中得一等奖"
ElseIf b = 6 Then
Label2.Caption = "恭喜你中得二等奖"
ElseIf b = 5 Then
Label2.Caption = "恭喜你中得三等奖"
'以此类推判断是否中奖
Else
Label2.Caption = "很遗憾,差一点就中奖了"
End If
Else
MsgBox "请输入一个七位数字"
Exit Sub
End If
End Sub
使用结构体链表模拟彩票的摇奖机,来保证每次随机都是不同的数字。
数组也可以模拟,但是删除元素比较麻烦,用链表更快捷。
定义数组指针,动态分配内存。
#include <stdio.h>#include <stdlib.h>
#include <time.h>
typedef struct List {
int number
struct List*next
} ArrayList
ArrayList*createList(int)//创建链表,参数是链表的节点数
void freeList(ArrayList*)//循环释放所有节点的内存
int deleteNode(ArrayList*,int)//删除节点,返回节点的数据
main() {
//动态创建内存
int*numbers=(int*)calloc(7,sizeof(int))
int*inNumbers=(int*)calloc(7,sizeof(int))
//动态创建数组链表存放1-33个编号
ArrayList*balls= createList(33)
int i
srand((unsigned)time(NULL))
printf("请输入7个不同的数字(1-33):\n")
for(i=0 i<7 i++) {
*(numbers+i)=deleteNode(balls,rand()%(33-i))
scanf("%d",inNumbers+i)
}
printf("随机产生的7个数字是:\n")
for(i=0 i<7 i++) {
printf("%d ",*(numbers+i))
}
printf("\n相同的数字:\n")
//查找
int j
for(i=0 i<7 i++) {
for(j=0 j<7 j++) {
if(*(numbers+i)==*(inNumbers+j)) {
printf("%d ",*(numbers+i))
break
}
}
}
//释放内存
free(numbers)
free(inNumbers)
freeList(balls)
}
ArrayList*createList(int n) {
ArrayList*head=NULL,*end=NULL,*node=NULL
end=head=(ArrayList*)malloc(sizeof(ArrayList))//head在删除和插入节点时有用。 不能删除head,也不能插入到head前面
int i
for(i=1 i<=n i++) {
node=(ArrayList*)malloc(sizeof(ArrayList))
node->number=i
end->next=node
end=node
}
end->next=NULL
return head
}
void freeList(ArrayList*head) {
ArrayList*node=head
while(head!=NULL) {
head=head->next
free(node)
node=head
}
}
int deleteNode(ArrayList*head,int index) { //index节点索引(0-32),返回被删除的球的编号
ArrayList*node=head->next,*temp=head
int i=0,number
while(i<index&&node!=NULL) {
temp=node
node=node->next
i++
}
if(node!=NULL) {
number=node->number
temp->next=node->next
free(node)
}
return number
}
运行结果
Dim a(0 To 4) As IntegerPrivate Sub Command1_Click()
For i = 1 To 30
If i = 30 Then
t = t &i
Else
t = t &i &","
End If
Next
For i = 0 To 4
j = Split(t, ",")
Randomize
temp = Int(Rnd * (UBound(j) + 1) + 0)
Text1(i) = j(temp)
If temp <>UBound(j) Then
t = Replace(t, "," &j(temp) &",", ",")
Else
t = Replace(t, "," &j(temp) &",", "")
End If
Next
End Sub
Private Sub Command2_Click()
For i = 1 To 30
If i = 30 Then
t = t &i
Else
t = t &i &","
End If
Next
For i = 0 To 4
j = Split(t, ",")
Randomize
temp = Int(Rnd * (UBound(j) + 1) + 0)
a(i) = j(temp)
If temp <>UBound(j) Then
t = Replace(t, "," &j(temp) &",", ",")
Else
t = Replace(t, "," &j(temp) &",", "")
End If
Next
Print "中奖号码为:" &a(0) &vbTab &a(1) &vbTab &a(2) &vbTab &a(3) &vbTab &a(4)
For i = 0 To 4
For k = 0 To 4
If a(i) = Text1(k) Then n = n + 1
Next
Next
If n <>0 Then
MsgBox "恭喜你中了" &6 - n &"等奖"
Else
MsgBox "很遗憾,您没中奖"
End If
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)