基于C语言的随机点名器设计
项目描述 使用C语言,制作一个在命令行运行的点名程序,该程序运行时可指定班级的花名册文件,运行后在界面上显示随机抽取名字的过程,速度由快到慢,逐渐定格到某一个“幸运儿”上,程序结束。 项目实现:
主函数模块:main.c
#include <stdio.h>
#include "function.h"
int main(int argc, char *argv[])
{
function();
return 0;
}
功能模块:function.c
#include "function.h"
#include "Random_name.h"
#include "Init_list.h"
void menu()
{
printf(" *************************************************\n");
printf(" * 随机点名器 *\n");
printf(" ************************************************ \n");
printf(" ********************************制作人:邢仕冲****\n");
printf(" ---------------------- --------------------- \n");
printf(" ********************************************* \n");
printf(" * 1.单人抽取 * * 2.查找剩余人数 * \n");
printf(" ********************** ********************* \n");
printf(" * 3.文本初始化 * 4. 多人抽取 * \n");
printf(" ********************************************** \n");
printf(" * 5.退出系统 * \n");
printf(" ********************************************** \n");
printf(" -------------------- ----------------------\n");
}
void function()
{
int num;
int new_num;
int Init_num;
int temp;
int flag = 0, flag1 = 0, flag2 = 0, flag3 = 0;
int mulit_num;
int lucky_num;
char Class_txt[32];
char Class_txt_Init[32];
printf("输入班级编号文档:");
scanf("%s", Class_txt);
printf("输入班级名单初始化文档:");
scanf("%s", Class_txt_Init);
printf("输入班级的人数:");
scanf("%d", &num);
menu();
Init_num = num;
mulit_num = num;
new_num = num;
while (1)
{
int n;
scanf("%d", &n);
switch (n)
{
case 1:
{
if (flag2 == 1)
{
num = mulit_num;
flag2 = 0;
}
Random_name(num, Class_txt);
num--;
temp--;
flag = 0;
flag1 = 0;
if (num == 0)
{
printf("已抽取完成!\n");
}
break;
}
case 2:
{
if (flag == 0)
{
printf("剩余人数:%d\n", num);
if (num == 0)
printf("已抽取完,请输入3,使文本初始化\n");
}
else if (flag == 1)
{
if (flag1 == 1)
temp = Init_num;
printf("剩余人数:%d\n", temp);
if (temp == 0)
printf("已抽取完,请输入3,使文本初始化");
}
else if (flag == 2)
{
printf("剩余人数:%d\n", mulit_num);
}
break;
}
case 3:
{
Init_list(Init_num, Class_txt_Init, Class_txt);
printf("文本初始化成功!\n");
flag = 1;
flag1 = 1;
flag3 = 1;
num = Init_num;
temp = Init_num;
break;
}
case 4:
{
printf("输入所要抽取的人数:");
scanf("%d", &lucky_num);
if (flag3 == 1)
{
new_num = Init_num;
flag3 = 0;
}
for (int i = 0; i < lucky_num; i++)
{
Random_name(new_num, Class_txt);
new_num--;
}
flag = 2;
mulit_num = mulit_num - lucky_num;
flag2 = 1;
break;
}
case 5:
Init_list(Init_num, Class_txt_Init, Class_txt);
break;
default:
Init_list(Init_num, Class_txt_Init, Class_txt);
break;
}
if (n > 4)
break;
}
}
function.h
#ifndef _FUNCTION_H
#define _FUNCTION_H
#include <stdio.h>
void function();
void menu();
#endif
随机点名实现模块 Random_name.c
#include "Random_name.h"
void Random_name(int Num, char *list)
{
char buf[Num][SIZE];
char buf_new[Num][SIZE];
int n, flag;
FILE *fp_list = fopen(list, "r");
if (NULL == fp_list)
{
perror("fopen failed!");
return;
}
srand((unsigned)time(NULL));
for (int i = 0; i <= Num; i++)
{
fgets(buf[i], SIZE, fp_list);
if (*(buf[i] + (strlen(buf[i]) - 1)) == '\n')
{
*(buf[i] + (strlen(buf[i]) - 1)) = '\0';
}
}
#if 0
for(int i = 0; i<Num; i++)
{
printf("%s\n", buf[i]);
}
#endif
for (int j = 3000; j > 0; j -= 30)
{
n = 0 + rand() % Num;
printf("%s\r", buf[n]);
usleep(j);
}
for (int j = 500000; j > 0; j -= 50000)
{
n = 0 + rand() % Num;
printf("%s\r", buf[n]);
usleep(j);
}
printf("幸运儿:%s\n", buf[n]);
for (int i = 0; i < Num; i++)
{
if (strcmp(buf[n], buf[i]) == 0)
{
strcpy(buf[i], "");
flag = n;
}
strcpy(buf_new[i], buf[i]);
}
#if 0
for(int i = 0; i<Num; i++)
{
printf("%s\n", buf_new[i]);
}
#endif
fclose(fp_list);
FILE *fp_list_new = fopen(list, "w");
if (NULL == fp_list_new)
{
perror("fopen failed!");
return;
}
for (int j = 0; j < Num; j++)
{
fputs(buf_new[j], fp_list_new);
if (flag == j)
{
fputs(buf_new[j + 1], fp_list_new);
j = j + 1;
}
fputs("\n", fp_list_new);
}
fclose(fp_list_new);
}
Random_name.h
#ifndef _RANDOM_NAME_H_
#define _RANDOM_NAME_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#define SIZE 8
void Random_name(int Num, char *list);
#endif
初始化模块:Init_list.c
```c
#include "Init_List.h"
void Init_list(int count, char *Init_list, char *list)
{
char Init[count][SIZE];
FILE *fp_list_Init = fopen(Init_list, "r");
for (int i = 0; i < count; i++)
{
fgets(Init[i], SIZE, fp_list_Init);
}
fclose(fp_list_Init);
FILE *list_Init = fopen(list, "w");
for (int j = 0; j < count; j++)
{
fputs(Init[j], fp_list_Init);
}
fclose(list_Init);
}
Init_list.h
#ifndef _INIT_LIST_H
#define _INIT_LIST_H
#include <stdio.h>
#include <stdlib.h>
#define SIZE 8
void Init_list(int count, char *Init_list, char *list);
#endif
|