目录
问题描述:
1、编程生成文本文件data.txt,文件内容是10个手机号码(手机号码前4位为1871,
后面随机生成)(尝试用多种方式实现)
2、编程:从文本文件data.txt中读取抽奖者的个人信息 (尝试用多种方式实现)
3、预设一、二、三等奖的人数(一等奖 1 名、二等奖 2 名、三等奖 5名)
4、按下任意键开始抽奖,并把中奖人的手机号显示在屏幕上,反复抽出一、二、三
等奖。如果抽完就不能再抽。
分析:
为保证抽奖的公正性,要使用随机函数生成获奖者。
抽奖者不能重复获奖。
任务一、生成手机号
方法一、使用C兼容,生成七位随机数
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generate(const char* filename, int count) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
perror("Error opening file");
return;
}
srand(time(NULL)); // 初始化随机数种子
for (int i = 0; i < count; ++i) {
int number = rand() % 90000000 + 10000000; // 生成7位随机数
fprintf(file, "1871%07d\n", number);
}
fclose(file);
}
int main() {
generate("data.txt", 10);
printf("Phone numbers have been generated and written to data.txt\n");
return 0;
}
运行结果截图
可以发现,使用这种方法产生的7位随机数,除开头1871外,前三位均为100,通过查阅相关网站和资料,了解到这是因为rand函数所产生随机数的范围为:0—32767,最大只有5位,因此这种方法存在一定的问题
方法二 、使用C兼容,循环生成七位数
方法二对方法一存在的问题做了改善
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void generate(const char* filename, int count) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
perror("Error opening file");
return;
}
srand(time(NULL)); // 初始化随机数种子
for (int i = 0; i < count; ++i) {
int number = 0;
// 循环生成7位随机数
for (int j = 0; j < 7; ++j) {
number = number * 10 + (rand() % 10); // 生成0-9的随机数并累加
}
fprintf(file, "1871%07d\n", number);
}
fclose(file);}
int main() {
generate("data.txt", 10);
printf("Phone numbers have been generated and written to data.txt\n");
return 0;
}
通过循环,每次生成0-9的个位数累加,同时包含了前导0,增大了随机性,非常完美
方法三、使用C++,循环生成7位数
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
void generate(const std::string& filename, int count) {
std::ofstream file(filename.c_str());
if (!file.is_open()) {
std::cerr << "Error opening file" << std::endl;
return;
}
std::srand(std::time(NULL)); // 初始化随机数种子
for (int i = 0; i < count; ++i) {
int number = 0;
// 循环生成7位随机数
for (int j = 0; j < 7; ++j) {
number = number * 10 + (std::rand() % 10); // 生成0-9的随机数并累加
}
file << "1871" << std::setw(7) << std::setfill('0') << number << "\n";
}
file.close();
}
int main() {
generate("data.txt", 10);
std::cout << "Phone numbers have been generated and written to data.txt" << std::endl;
return 0;
}
生成号码截图:
此时生成的电话号码才是真正随机的
任务二、抽奖程序的编写
代码程序
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <ctime>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
using namespace std;
//定义类
class People {
private:
std::vector<int> c;
public:
People() : c(11) {}
void show();
void set_data(const std::vector<int>& a);
};
//类外函数实现
void People::set_data(const std::vector<int>& a) {
c = a;
}
void People::show() {
for (unsigned int i = 0; i < c.size(); ++i) {
std::cout << c[i]; }
std::cout << std::endl;
}
/*-----抽奖函数------*/
int chose_number() {
std::srand(std::time(0));
return std::rand() % 10;
}
int raffle(std::vector<bool>& usedNumbers) {//这里也可以用静态函数,不过我用了bool类型的动态数组存储号码状态,false则表示未抽中
while (true) {
int t = chose_number();
if (!usedNumbers[t]) {//已被选过的号码为!ture,不会进入循环,需要重新抽取
usedNumbers[t] = true;
return t;
}
}
}
//用上面写的程序撰写数据,生成电话号码
void data(const std::string& filename, int count) {
std::ofstream file(filename.c_str());
if (!file.is_open()) {
std::cerr << "Error opening file" << std::endl;
return;
}
std::srand(std::time(NULL)); // 初始化随机数种子
for (int i = 0; i < count; ++i) {
int number = 0;
// 循环生成7位随机数
for (int j = 0; j < 7; ++j) {
number = number * 10 + (std::rand() % 10); // 生成0-9的随机数并累加
}
file << "1871" << std::setw(7) << std::setfill('0') << number << "\n";
}
file.close();
}
//抽奖准备
int main() {
data("data.txt", 10);
std::vector<People> a(10); // 对象向量
std::ifstream in("data.txt");
std::string line;
std::vector<int> tmp(11);
// 读数据,并装入数组
int index = 0; // 索引变量
while (std::getline(in, line)) {
int i;
for (i = 0; i < line.size() && i < tmp.size(); ++i) {
tmp[i] = line[i] - '0';
}
a[index++].set_data(tmp);
}
std::vector<bool> usedNumbers(10, false);
/*------抽奖过程------*/
cout << "准备好获奖了吗?\n\n";
system("pause");
cout << "\n开始抽一等奖(1名)\n";
system("pause");
cout << "中奖号码为:";
int t = raffle(usedNumbers);
a[t].show();
cout << "\n开始抽二等奖(2名)\n";
cout << "第1个二等奖,"; system("pause"); cout << "中奖号码为:"; t = raffle(usedNumbers); a[t].show();
cout << "第2个二等奖,"; system("pause"); cout << "中奖号码为:"; t = raffle(usedNumbers); a[t].show();
cout << "\n开始抽三等奖(5名)\n";
for (int i = 0; i < 5; i++) {
cout << "第" << i + 1 << "个三等奖, "; system("pause");
cout << "中奖号码为:"; t = raffle(usedNumbers); a[t].show();
}
return 0;
}
测试截图(共两次)
难点分析
- 随机数生成
- 读取数据,抽奖过程的逻辑实现
- 使用了<vector>容器做对象(stl模板库)
- 程序运行速度较慢,可能是循环较多的原因