C语言根据随机数编写一个抽奖程序

C语言根据随机数编写一个抽奖程序,第1张

#include<stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <memory.h>

#include <string.h>

#include <time.h>

#define AWARDER_NUM 10

typedef struct tag_award

{

char ID[20]

char name[20]

char telNo[20]

char address[50]

}AWARDER

AWARDER * awarder = new AWARDER[AWARDER_NUM]

int first_award[1] = {-1}

int second_award[2] = {-1,-1}

int third_award[5] ={-1,-1,-1,-1,-1}

int awarder_num = 0

void info_login()

{

if(awarder_num >= AWARDER_NUM)

{

AWARDER * new_awarder = new AWARDER[awarder_num + AWARDER_NUM]

memcpy(new_awarder,awarder,awarder_num*sizeof(AWARDER))

delete awarder

awarder = new_awarder

}

printf("身份z号:")

scanf("%s",awarder[awarder_num].ID)

printf("姓名:")

scanf("%s",awarder[awarder_num].name)

printf("电话号码:")

scanf("%s",awarder[awarder_num].telNo)

printf("家庭住址:")

scanf("%s",awarder[awarder_num].address)

awarder_num++

}

void info_delete()

{

char name[20]

printf("输入您要删除的抽奖者的姓名:")

scanf("%s",name)

for(int i=0i<awarder_numi++)

{

if (strcmp(awarder[i].name,name) == 0)

{

printf("您选择删除的抽奖者的信息如下:\n")

printf("身份z号:%s\n",awarder[i].ID)

printf("姓名:%s\n",awarder[i].name)

printf("电话号码:%s\n",awarder[i].telNo)

printf("家庭住址:%s\n",awarder[i].address)

break

}

}

if(i == awarder_num)

{

printf("没有找到您输入的抽奖者!\n")

}

else

{

char c

printf("您确定要删除吗?(Y//N)")

fflush(stdin)

scanf("%c",&c)

if(c == 'Y' || c == 'y')

{

AWARDER * new_award = new AWARDER[awarder_num-1]

if(i <= 0)

memcpy(new_award,awarder+1,sizeof(AWARDER) * (awarder_num - 1))

else if( i >= awarder_num -1)

memcpy(new_award,awarder,sizeof(AWARDER) * (awarder_num - 1))

else

{

memcpy(new_award,awarder,sizeof(AWARDER) * i)

memcpy(new_award+i,awarder+i+1,sizeof(AWARDER) * (awarder_num - i - 1))

}

awarder_num--

delete awarder

awarder = new_award

}

}

}

void info_modify()

{

char name[20]

printf("输入您要修改的抽奖者的姓名:")

scanf("%s",name)

for(int i=0i<awarder_numi++)

{

if (strcmp(awarder[i].name,name) == 0)

{

printf("您选择的抽奖者的信息如下:\n")

printf("身份z号:%s\n",awarder[i].ID)

printf("姓名:%s\n",awarder[i].name)

printf("电话号码:%s\n",awarder[i].telNo)

printf("家庭住址:%s\n",awarder[i].address)

break

}

}

if(i == awarder_num)

{

printf("没有找到您输入的抽奖者!\n")

}

else

{

printf("请输入该抽奖者的新信息:\n")

printf("身份z号:")

scanf("%s",awarder[i].ID)

printf("姓名:")

scanf("%s",awarder[i].name)

printf("电话号码:")

scanf("%s",awarder[i].telNo)

printf("家庭住址:")

scanf("%s",awarder[i].address)

}

}

bool check_over(int cur_award,int awarderNo)

{

if(awarderNo == 1)

{

if(cur_award == first_award[0])

return true

}

else if(awarderNo <= 3)

{

if(cur_award == first_award[0])

return true

for(int i=0i<awarderNo-1i++)

if(cur_award == second_award[i])

return true

}

else if(awarderNo <= 8)

{

if(cur_award == first_award[0])

return true

for(int i=0i<2i++)

if(cur_award == second_award[i])

return true

for(i=0i<awarderNo-3i++)

if(cur_award == third_award[i])

return true

}

return false

}

void award()

{

if(awarder_num <8)

{

printf("人数太少,请添加足够的人再抽奖!\n")

return

}

srand((long)time(0))

char hit_id[20]

printf("\n开始抽第一等奖(1名),按任意键开始...\n")

getch()

while(!kbhit())

{

first_award[0] = rand() % awarder_num

memcpy(hit_id,awarder[first_award[0]].ID,18)

printf("\r")

printf("\r %s",hit_id)

}

printf("\n\n开始抽第二等奖(2名)")

for (int i=0i<2i++)

{

printf("\n第%d个二等奖,按任意键开始...\n",i+1)

getch()

while(!kbhit())

{

second_award[i] = rand() % awarder_num

while(check_over(second_award[i],1+i))

second_award[i] = rand() % awarder_num

memcpy(hit_id,awarder[second_award[i]].ID,18)

printf("\r")

printf("\r %s",hit_id)

}

}

printf("\n\n开始抽第三等奖(5名)")

for (i=0i<5i++)

{

printf("\n第%d个三等奖,按任意键开始...\n",i+1)

getch()

while(!kbhit())

{

third_award[i] = rand() % awarder_num

while(check_over(third_award[i],3+i))

third_award[i] = rand() % awarder_num

memcpy(hit_id,awarder[third_award[i]].ID,20)

printf("\r")

printf("\r %s",hit_id)

}

}

}

void info_show()

{

printf("一等奖获得者:\n")

printf("\t身份z号:%s\n",awarder[first_award[0]].ID)

printf("\t姓名:%s\n",awarder[first_award[0]].name)

printf("\t电话号码:%s\n",awarder[first_award[0]].telNo)

printf("\t家庭住址:%s\n",awarder[first_award[0]].address)

printf("二等奖获得者:\n")

for (int i=0i<2i++)

{

printf("第%d个二等奖获得者:\n")

printf("\t身份z号:%s\n",awarder[second_award[i]].ID)

printf("\t姓名:%s\n",awarder[second_award[i]].name)

printf("\t电话号码:%s\n",awarder[second_award[i]].telNo)

printf("\t家庭住址:%s\n",awarder[second_award[i]].address)

}

printf("三等奖获得者:\n")

for (int i=0i<5i++)

{

printf("第%d个三等奖获得者:\n")

printf("\t身份z号:%s\n",awarder[third_award[i]].ID)

printf("\t姓名:%s\n",awarder[third_award[i]].name)

printf("\t电话号码:%s\n",awarder[third_award[i]].telNo)

printf("\t家庭住址:%s\n",awarder[third_award[i]].address)

}

}

/*提示信息*/

bool raise()

{

printf("\n请输入你需要的 *** 作代号:\n")

printf("\t1.录入:完成抽奖者信息的输入\n")

printf("\t2.删除:完成抽奖者信息的删除\n")

printf("\t3.修改:允许对已经录入的数据重新进行编辑、修改\n")

printf("\t4.抽奖:开始抽奖!\n")

printf("\t5.显示:显示所有中奖者的信息\n")

printf("\t6.退出。\n")

printf("==>")

int operate

fflush(stdin)

scanf("%d",&operate)

switch(operate)

{

case 1:

info_login()

break

case 2:

info_delete()

break

case 3:

info_modify()

break

case 4:

award()

break

case 5:

info_show()

break

case 6:

return false

default:

break

}

printf("\n *** 作完成。\n")

return true

}

void main()

{

while(raise())

}

代码如下,已测试通过:

int main()

{

int no,ret

printf("请输入你猜测的号码:\n")

scanf("%d",&no)

ret=rand()%10

printf("本次开奖结果=[%d]:\n",ret)

if(no==ret)

{

printf("恭喜你猜中了,中奖10元!\n")

}

else

{

printf("很遗憾,再来一次吧!\n")

}

return 0

}

可以把上面代码改成多次输入,即将输入和取随机数都放在while循环中。自己修改下吧


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11426008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存