本文实例为大家分享了C语言实现简单的抽奖系统的具体代码,供大家参考,具体内容如下
效果图
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include <windows.h>
struct data{
char id[20];
char name[20];
};
int t1,t2,t3;
struct data information[110]; //普通人员信息
struct data prize1[110]; //总获奖人员信息
struct data prize2[110]; //总获奖人员信息
struct data prize3[110]; //总获奖人员信息
int people=0; //参与抽奖总人数
int people1=0,people2=0,people3=0; //普通一等奖、二等奖、三等奖的人数
char strid[20];
//从文件中读取信息,放到普通人员信息中
void Read_information();
//打印菜单
void Print_menu();
//设置奖项人数
void Set_awards();
//开始抽奖
void Begin_luck();
//删除该学号人员在普通人员中的信息
void Delet_information();
void main()
{
Read_information();
Print_menu();
}
void Read_information()//从文件中读取信息
{
FILE *fp=fopen("抽奖人员名单.txt","r");
while(!feof(fp))
{
//存储该信息到普通人员信息中,便于设置内幕人员
fscanf(fp,"%s%s",information[people].id,information[people].name);
people++;
}
fclose(fp);
}
void Print_menu()
{
int choose;
while(1)
{
system("cls");
printf("\n\n");
printf("\t\t\t*********************************************************\n");
printf("\t\t\t*\t <-欢迎来到抽奖系统->\t\t\t*\n");
printf("\t\t\t*\t <-目前参与抽奖的总人数为%d人->\t\t*\n", people);
printf("\t\t\t*\t < 一等奖设置为%d人 >\t\t\t*\n",people1);
printf("\t\t\t*\t < 二等奖设置为%d人 >\t\t\t*\n",people2);
printf("\t\t\t*\t < 三等奖设置为%d人 >\t\t\t*\n",people3);
printf("\t\t\t*\t\t\t\t\t\t\t*\n");
printf("\t\t\t*\t\t开始抽奖请按1\t\t\t\t*\n");
printf("\t\t\t*\t\t设置奖项人数2\t\t\t\t*\n");
printf("\t\t\t*\t\t结束程序请按0\t\t\t\t*\n");
printf("\t\t\t*\t\t\t\t\t\t\t*\n");
printf("\t\t\t*********************************************************\n");
printf("\n\n\t\t\t\t\t请输入选项:");
scanf("%d",&choose);
switch(choose)
{
case 0:
return;
case 1:
Begin_luck();
break;
case 2:
Set_awards();
break;
}
}
}
//设置奖项人数
void Set_awards()
{
int choose;
while(1)
{
system("cls");
printf("请输入三等奖人数(正整数):\n");
scanf("%d",&t3);
if( t3<0 || t3>people)
{
printf("输入不合法,请输入1到%d之内的数字",people);
getch();
}
else
{
people3=t3;
break;
}
}
while(1)
{
system("cls");
printf("请输入二等奖人数:\n");
scanf("%d",&t2);
if( t2<0 || t2>people)
{
printf("输入不合法,请输入1到%d之内的数字",people);
getch();
}
else
{
people2=t2;
break;
}
}
while(1)
{
system("cls");
printf("请输入一等奖人数:\n");
scanf("%d",&t1);
if( t1<0 || t1>people)
{
printf("输入不合法,请输入1到%d之内的数字",people);
getch();
}
else
{
people1=t1;
break;
}
}
printf("设置完毕按任意键返回\n");
getch();
return;
}
void Begin_luck() //开始抽奖
{
if(people1==0 || people2==0 || people3==0)
{
printf("请设置奖项人数!!!!\n");
printf("按任意键返回\n");
getch();
return;
}
if( (people1+people2+people3) > people )
{
printf("中奖人数超过总人数,请重新设置!!!!\n");
printf("按任意键返回\n");
getch();
return;
}
int i,j=0,choose,t;
system("cls");
printf("下面开始抽取三等奖人员,按任意键继续\n");
getch();
while(people3--)
{
if(people3<0)
break;
srand((unsigned)time(0));
t=rand()%people;
prize3[j++]=information[t];
strcpy(strid,information[t].id);
Delet_information();
}
printf("以下是三等奖名单,按任意键继续\n");
for(i=0;i<j;i++)
printf("%s\n",prize3[i].name);
getch();
printf("下面开始抽取二等奖人员,按任意键继续\n");
getch();
while(people2--)
{
if(people2<0)
break;
srand((unsigned)time(0));
t=rand()%people;
prize2[j++]=information[t];
strcpy(strid,information[t].id);
Delet_information();
}
printf("以下是二等奖名单,按任意键继续\n");
for(;i<j;i++)
printf("%s\n",prize2[i].name);
getch();
printf("下面开始抽取一等奖人员,按任意键继续\n");
getch();
while(people1--)
{
if(people1<0)
break;
srand((unsigned)time(0));
t=rand()%people;
prize1[j++]=information[t];
strcpy(strid,information[t].id);
Delet_information();
}
printf("以下是一等奖名单,按任意键继续\n");
for(;i<j;i++)
printf("%s\n",prize1[i].name);
getch();
people=0;
Read_information();
people1=t1,people2=t2,people3=t3;
}
void Delet_information() //删除该学号人员在普通人员中的信息
{
int i,j;
for(i=0;i<people;i++)
if(strcmp(information[i].id,strid)==0)
{
people--;
for(j=i;j<people;j++)
information[j]=information[j+1];
return;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。