首先我们要实现一个点名器项目要明白怎样实现点名器那种快速闪过的刷新效果呢!
答案是使用"\r"来实现(此处需要知道的小细节,我们在Windows下编辑的文本文件,事实上在每行的后面都会有一个\0\n,但是在linux里面就只有一个\0)
下面讲一下该项目的实现的主要思路(主要思路 打开文件后把文件写入结构体,然后用id去排序,通过设置id随机数来随机输出某一个id对应的人名(通过设置初始的时间节点并且设置其扫描到目标后减速,以及没有扫描到的话就延时来实现输出)
int main(int argc, char const *argv[])
{
int i,j=0;
count();
printf("Welcome to Henry's name select sofeware!\n");
printf("Please enter the number of people you want\n");
system("espeak -s 100 -f wel.txt");//此处的语速语调可以自己调,详情可以自己百度
usleep(1000);
readtxt();
nameselect();
while(1)
{
printf("If want to use again ,please type 1 if not type 2 to close the system!");
scanf("%d",&i);
switch(i)
{
case 1:
nameselect();
j++;
break;
case 2:
exit(0);
}
if(j==10)
break;
}
return 0;
}
//此处是点名功能的具体实现
int nameselect(int id)//要点几个人
{
int x = 0; // 用于存放人数
printf("Please type how many student what you need:");
while (1)
{
int sc = scanf("%d", &x);
getchar();
if( sc == 0 || x < 1 || x > 106 )
{
printf("Please type right number(1-106): ");
}
else break;
}
// 每循环一次,就获得一个随机数,这个随机数就是 id
for(int i=1; i<=x; i++)
{
int t = 100*1000; // 设定延迟时间的初始值: 100ms
time_t n = time(NULL);
srand(n); // 获得随机数种子
int rand_id = rand() % (M+1); // 取随机数范围: 0 ~ (M+1)
// 只有当通过 随机数 产生的 rand_id 没有出现过的时候,
// 才将那个 id 对应的名字输出
// 否则 i--, 再继续上一次循环
if( repeatID(rand_id) )
{
i--;
continue;
}
//appear[i] = rand_id;
// 将出现过的 随机学号 保存起来(把对应的随机学号存起来)
// 从名单的第 1 个名字开始,遍历数组,直至出现随机数对应的名字
for(int j=1; j<=rand_id; j++)
{
char *find = strchr(stu[j].s, '\n'); // 查找 换行符 用\r替换\n
if(find) // 如果 find 不为空指针
*find = '\r'; // 就使 光标 移到左边(实现每段刷新)
usleep(t);
printf("the %2d :",i);
if( j < (rand_id * 2/3) ) // 设置速度变化的节点(可以取多也可以取少,看心情)
{
printf("%15s", stu[j].s);
t -= 420; // 延迟时间不断减少,即循环变快(太快可能看不清,注意取舍)
}
else if( j == rand_id ) // 直至出现 rand_id,循环结束
{
printf("%15s\n", stu[j].s);
break;
}
else
{
printf("%15s", stu[j].s);
t += 8000; // 延迟时间不断增加,即循环变慢
}
fflush(stdout);
}
}
return 0;
}
//此处是账户系统的具体实现
int count()
{
char account[10] = {"110110"};
char password[10]={"123456"};
char name[20], pwd[20];
int i = 0;
printf("Welcome to the login screenWelcome!\n ");
printf("Please enter the correct user name and password (case sensitive)\n ");
while(i<3)
{
printf("User name(Six characters):\n");
//scanf("%s",&name);
fgets(name,sizeof(name),stdin);
printf("Password((Six characters)):\n");
// scanf("%s",&pwd);
fgets(pwd,sizeof(pwd),stdin);
if((strncmp(name,account,6) == 0) && (strncmp(password,pwd,6) == 0))
{
printf("Login succeeded. Connecting to the system...\n");
break;
}
else if((strncmp(name,account,6) != 0)||(strncmp(password,pwd,6) != 0))
{
printf("Login failed, please check case or contact Henry!\n");
i++;
}
}
// printf("%d\n",i);
if(i == 3)
{
printf("You have entered the wrong password for 3 times, the system is shut down, and the alarm has been reported \n");
system("espeak -s 100 -f test.txt");
system("python3 smtpsending.py");
exit(0);
}
}
具体的代码详见文件分享