【无标题】随机生成10个电话号。编写抽奖程序,抽出一二三等奖,号码不允许重复获奖。

##用类实现

#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)获取当前时间,同一秒内生成的随机数是一样的,所以需要等待。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值