如何用C语言写一个抽奖程序
/*
预先输入并存储参与抽奖者的信息,主要通过结构数组实现,每条记录用结构表示,包括身份证号、姓名、电话、地址等信息。抽奖开始时,参与者的身份证号在屏幕上快速闪烁,按任意键停止闪烁,屏幕上显示中奖者的信息。可以反复抽取若干个一等奖、二等奖、三等奖。
设计并实现以下功能:
(1)输入:完成抽奖信息的输入;
(2)删除:删除抽奖信息;
(3)修改:输入的数据可以再次编辑修改;
(4)显示:显示所有中奖者的信息;
(5)幸运抽奖:
A.抽1一等奖,抽过的不能再抽;
B.抽两个二等奖,抽完了就不能再抽了;
C.抽5个三等奖,抽完了不能再抽;
(6)退出程序。*/
#包括
#包括
#包括
#包括
#包括
#包括
#定义订单编号10
typedef结构tag_award
{
char ID[20];
char name[20];
char telNo[20];
char地址[50];
} AWARDER
a warder * a warder = new a warder[a warder _ 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;
无效信息_登录()
{
if(awarder _ num & gt;=订单数量)
{
AWARDER * new _ a warder = new a warder[a warder _ NUM+a warder _ NUM];
memcpy(new_awarder,awarder,a warder _ num * sizeof(a warder));
删除订单;
awarder = new _ awarder
}
Printf("身份证号:");
scanf("%s ",awarder[awarder_num]。ID);
printf(" name:");
scanf("%s ",awarder[awarder_num]。姓名);
Printf("电话号码:");
scanf("%s ",awarder[awarder_num]。telNo);
Printf("家庭住址:");
scanf("%s ",awarder[awarder_num]。地址);
awarder _ num++;
}
void info_delete()
{
char name[20];
Printf("输入要删除的抽奖名称:");
scanf("%s ",名称);
for(int I = 0;我& ltawarder _ numi++)
{
if (strcmp(awarder[i].name,name) == 0)
{
Printf("您选择删除的抽奖信息如下:\ n ");
Printf ("ID号:%s\n ",awarder[i]。ID);
Printf ("Name: %s\n ",awarder[i]。姓名);
Printf("电话号码:%s\n ",awarder[i]。telNo);
Printf("家庭地址:%s\n ",awarder[i]。地址);
打破;
}
}
if(i ==订单数量)
{
Printf("您输入的抽奖没有找到!\ n ");
}
其他
{
char c;
Printf("确定要删除吗?(Y//N)");
fflush(stdin);
scanf("%c ",& ampc);
if(c == 'Y' || c == 'y ')
{
AWARDER * new_award =新AWARDER[AWARDER _ num-1];
如果(我& lt= 0)
memcpy(new_award,awarder+1,sizeof(AWARDER)*(AWARDER _ num-1));
else if(I & gt;=订单编号-1)
memcpy(new_award,awarder,sizeof(AWARDER)*(AWARDER _ num-1));
其他
{
memcpy(new_award,awarder,sizeof(AWARDER)* I);
memcpy(new_award+i,awarder+i+1,sizeof(AWARDER)*(AWARDER _ num-I-1));
}
awarder _ num-;
删除订单;
awarder = new _ award
}
}
}
void info_modify()
{
char name[20];
Printf("输入要修改的抽奖名称:");
scanf("%s ",名称);
for(int I = 0;我& ltawarder _ numi++)
{
if (strcmp(awarder[i].name,name) == 0)
{
Printf("您选择的抽奖信息如下:\ n ");
Printf ("ID号:%s\n ",awarder[i]。ID);
Printf ("Name: %s\n ",awarder[i]。姓名);
Printf("电话号码:%s\n ",awarder[i]。telNo);
Printf("家庭地址:%s\n ",awarder[i]。地址);
打破;
}
}
if(i ==订单数量)
{
Printf("您输入的抽奖没有找到!\ n ");
}
其他
{
Printf("请输入本次抽奖的新信息:\ n ");
Printf("身份证号:");
scanf("%s ",命令[i]。ID);
printf(" name:");
scanf("%s ",命令[i]。姓名);
Printf("电话号码:");
scanf("%s ",命令[i]。telNo);
Printf("家庭住址:");
scanf("%s ",命令[i]。地址);
}
}
bool check_over(int cur_award,int awarderNo)
{
if(awarderNo == 1)
{
if(cur_award == first_award[0])
返回true
}
else if(awarderNo & lt;= 3)
{
if(cur_award == first_award[0])
返回true
for(int I = 0;我& ltawarderNo-1;i++)
if(当前奖励==第二奖励[i])
返回true
}
else if(awarderNo & lt;= 8)
{
if(cur_award == first_award[0])
返回true
for(int I = 0;我& lt2;i++)
if(当前奖励==第二奖励[i])
返回true
for(I = 0;我& ltawarderNo-3;i++)
if(cur_award == third_award[i])
返回true
}
返回false
}
无效奖励()
{
if(awarder _ num & lt;8)
{
Printf("人数太少,请加够人数再画!\ n ");
返回;
}
srand((长)时间(0));
char hit _ id[20];
printf(" \ n开始抽一等奖(1),按任意键开始...\ n ");
getch();
而(!kbhit())
{
first _ award[0]= rand()% a warder _ num;
memcpy(hit_id,awarder[first_award[0]]。ID,18);
printf(" \ r ");
printf("\r %s ",hit _ id);
}
printf(" \ n \ n开始抽二等奖(2)");
for(int I = 0;我& lt2;i++)
{
printf(" \ n第%d个二等奖,按任意键开始...\n ",I+1);
getch();
而(!kbhit())
{
second _ award[I]= rand()% a warder _ num;
while(check _ over(second _ award[I],1+i))
second _ award[I]= rand()% a warder _ num;
memcpy(hit_id,awarder[second_award[i]]。ID,18);
printf(" \ r ");
printf("\r %s ",hit _ id);
}
}
printf(" \ n \ n开始抽三等奖(5)");
for(I = 0;我& lt5;i++)
{
printf(" \ n第%d个三等奖,按任意键开始...\n ",I+1);
getch();
而(!kbhit())
{
third _ award[I]= rand()% a warder _ num;
while(check _ over(third _ award[I],3+i))
third _ award[I]= rand()% a warder _ num;
memcpy(hit_id,awarder[third_award[i]]。ID,20);
printf(" \ r ");
printf("\r %s ",hit _ id);
}
}
}
void info_show()
{
Printf("一等奖得主:\ n ");
Printf("\t ID号:% s \ n ",Awarder [first _ award [0]]。id);
Printf ("\ tname:% s \ n ",Awarder [first _ award [0]]。姓名);
printf(" \ t电话号码:% s \ n ",Awarder [first _ award [0]]。telno);
printf(" \ t家庭地址:% s \ n ",Awarder [first _ award [0]]。地址);
Printf("二等奖得主:\ n ");
for(int I = 0;我& lt2;i++)
{
Printf ("%d二等奖得主:\ n ");
Printf("\t ID号:% s \ n ",Awarder [second _ award [i]]。id);
Printf ("\ tname:% s \ n ",Awarder [second _ award [i]]。姓名);
printf(" \ t电话号码:% s \ n ",Awarder [second _ award [i]]。telno);
printf(" \ t家庭地址:% s \ n ",Awarder [second _ award [i]]。地址);
}
Printf("三等奖得主:\ n ");
for(int I = 0;我& lt5;i++)
{
Printf ("%d三等奖得主:\ n ");
Printf("\t ID号:% s \ n ",Awarder [third _ award [i]]。id);
Printf ("\ tname:% s \ n ",Awarder [third _ award [i]]。姓名);
printf(" \ t电话号码:% s \ n ",Awarder [third _ award [i]]。telno);
printf(" \ t家庭地址:% s \ n ",Awarder [Third _ Award [I]]。地址);
}
}
/*提示信息*/
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操作;
fflush(stdin);
scanf("%d ",& amp操作);
开关(操作)
{
案例1:
info _ log in();
打破;
案例二:
info _ delete();
打破;
案例三:
info _ modify();
打破;
案例4:
award();
打破;
案例5:
info _ show();
打破;
案例6:
返回false
默认值:
打破;
}
printf(" \ n操作已完成。\ n ");
返回true
}
void main()
{
while(raise());
}