##用类实现
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <ctime>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
class People {
private:
int c[11];
public:
People(){}
void show();
void set_data(int a[]);
};
void People::set_data(int a[])
{
int i;
for (i = 0; i < 11; i++) {
c[i] = a[i];
}
}
void People::show() {
for (int i = 0; i < 11; i++) {
cout << c[i];
}
cout << endl;
}
int chose_number()
{
srand(time(0));
int n = rand() % 10;
//cout<<n<<endl;
return n;
}
/*-----抽奖函数------*/
int raffle()
{
static int mark[10];
static int q = 0;
int t;
while (1) {
int flag = 0;//t是否存在数组中
t = chose_number();
for (int i = 0; i < 10; i++) {
if (mark[i] == t) {
flag = 1;
break;
}
}
if (flag == 0) { break; }//不在,退出循环
}
mark[q++] = t; //将t加入数组
return t;
}
/*----------随机生成10个电话号码--------*/
void write_data()
{
fstream outfile;
srand(time(0));
outfile.open("data1.txt", ios::out | ios::binary);
if (!outfile.is_open()) {
cout << "文件打开失败!" << endl;
}
else {
for (int i = 0; i < 10; i++) {
outfile << 1871; int a;
for (int j = 0; j < 7; j++) {
a = rand() % 10;
outfile << a;
}
outfile << "\n";
}
outfile.close();
}
}
int main()
{
//write_data();
char c[20];
int tmp[11];
/*------读数据,并装入数组------*/
People a[10]; //对象数组
ifstream in;
in.open("data1.txt");
for (int i = 0; i < 10; i++) {
in.getline(c, 20, '\n');
for (int j = 0; j < 11; j++) {
tmp[j] = c[j] - '0';
}
a[i].set_data(tmp);
}
in.close();
int t; //存放中奖者序号
/*------抽奖过程------*/
cout << "简单抽奖程序\n\n";
system("pause");
cout << "\n开始抽一等奖(1名)\n";
system("pause");
cout << "中奖号码为:"; t = raffle(); a[t].show();
cout << "\n开始抽二等奖(2名)\n";
cout << "第1个二等奖,"; system("pause"); cout << "中奖号码为:"; t = raffle(); a[t].show();
cout << "第2个二等奖,"; system("pause"); cout << "中奖号码为:"; t = raffle(); a[t].show();
cout << "\n开始抽三等奖(5名)\n";
for (int i = 0; i < 5; i++) {
cout << "第" << i + 1 << "个三等奖, "; system("pause");
cout << "中奖号码为:"; t = raffle(); a[t].show();
}
}
不用类实现
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
/*-------生成随机号码---------*/
void write_data()
{
fstream outfile;
srand(time(0));
outfile.open("data.txt", ios::out | ios::binary);
if (!outfile.is_open()) {
cout << "文件打开失败!" << endl;
}
else {
for (int i = 0; i < 10; i++) {
outfile << 1871; int a;
for (int j = 0; j < 7; j++) {
a = rand() % 10;
outfile << a;
}
outfile<< "\n";
}
outfile.close();
}
}
/*----生成随机数----*/
int chose_number()
{
srand(time(0));
int n = rand() % 10;
return n;
}
/*-----读取文件并输出------*/
void putout_data(int n)
{
ifstream infile;
infile.open("data.txt");
if (!infile.is_open()) {
cout << "文件打开失败!" << endl;
}
else {
string buf;
infile.seekg(12*n, ios::beg);
//infile.read(buf,sizeof(buf));
getline(infile,buf,'\n');
cout << buf << endl;
}
infile.close();
}
/*-------抽奖函数-----------*/
void raffle()
{
static int mark[10];
static int q = 0;
int t;
while (1) {
int flag = 0;//t是否存在数组中
t = chose_number();
for (int i = 0; i < 10; i++) {
if (mark[i] == t) {
flag = 1;
break;
}
}
if (flag == 0) { break; }//不在,退出循环
}
mark[q++] = t; //将t加入数组
putout_data(t);
}
int main()
{
write_data();
cout << "简单抽奖程序\n\n";
system("pause");
cout << "\n开始抽一等奖(1名)\n";
system("pause");
cout << "中奖号码为:"; raffle();
cout << "\n开始抽二等奖(2名)\n";
cout << "第1个二等奖,"; system("pause"); cout << "中奖号码为:"; raffle();
cout << "第2个二等奖,"; system("pause"); cout << "中奖号码为:"; raffle();
cout << "\n开始抽三等奖(5名)\n";
for (int i = 0; i < 5; i++) {
cout << "第"<<i+1<<"个三等奖, "; system("pause");
cout<<"中奖号码为:"; raffle();
}
}
两个在抽取号码过程中都会卡顿,猜想是time(0)获取当前时间,同一秒内生成的随机数是一样的,所以需要等待。