#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include<stdlib.h>
#include <time.h>
#include "Employee.h"
using namespace std;
const int MAX = 30;//员工数
void main(){
Employee employee[MAX];
ifstream emIn("employeeInfo.txt",ios::in);
int size;
if (!emIn) {
cout << "文件不存在" << endl;
//system("pause");
return;
}
//cout << "文件存在" << endl;
for (size = 0;size < MAX;size++) {
emIn >> employee[size].m_name >> employee[size].m_id >> employee[size].m_level;//为对象赋值
//cout << employee[size].m_name << " " << employee[size].m_id << " " << employee[size].m_level << endl;
}
emIn.close();//关闭文件流
//开始抽奖
//先抽3等。从所有对象中抽取5个
int a,b,c,d,e;
srand(time(0));
a = rand() % MAX + 1;
employee[a].m_bool =1;
b = rand() % MAX + 1;
employee[b].m_bool = 1;
c = rand() % MAX + 1;
employee[c].m_bool = 1;
d = rand() % MAX + 1;
employee[d].m_bool = 1;
e = rand() % MAX + 1;
employee[e].m_bool = 1;
cout << "中了三等奖的是:" << employee[a].m_name<<" "<< employee[b].m_name << " " <<employee[c].m_name << " "
<< employee[d].m_name << " " <<employee[e].m_name << endl;
//抽二等奖
int c2[30];//定义一个数组用来取随机值
cout << "中了二等奖的是:";
for (int i = 0; i < 3;i++)
{
c2[i] = rand() % MAX + 1;
while (employee[(c2[i])].m_bool == 1 && employee[(c2[i])].m_level>=3) //如果中奖了那么继续取随机值,级别大于等于3也不行
{
c2[i+1] = rand() % MAX + 1;
i = i + 1;
}
c2[i] = rand() % MAX + 1;
cout << employee[(c2[i])].m_name << " ";
}
cout << endl;
//抽一等奖
cout << "中了一等奖的是:";
int i = 0;
c2[i] = rand() % MAX + 1;
while (employee[(c2[i])].m_bool == 1 && employee[(c2[i])].m_level >= 3)
{
c2[i +1] = rand() % MAX + 1;
i = i + 1;
}
c2[i] = rand() % MAX + 1;
cout << employee[(c2[i])].m_name << " ";
system("pause");
}
#include <fstream>
#include <iomanip>
#include <string>
#include<stdlib.h>
#include <time.h>
#include "Employee.h"
using namespace std;
const int MAX = 30;//员工数
void main(){
Employee employee[MAX];
ifstream emIn("employeeInfo.txt",ios::in);
int size;
if (!emIn) {
cout << "文件不存在" << endl;
//system("pause");
return;
}
//cout << "文件存在" << endl;
for (size = 0;size < MAX;size++) {
emIn >> employee[size].m_name >> employee[size].m_id >> employee[size].m_level;//为对象赋值
//cout << employee[size].m_name << " " << employee[size].m_id << " " << employee[size].m_level << endl;
}
emIn.close();//关闭文件流
//开始抽奖
//先抽3等。从所有对象中抽取5个
int a,b,c,d,e;
srand(time(0));
a = rand() % MAX + 1;
employee[a].m_bool =1;
b = rand() % MAX + 1;
employee[b].m_bool = 1;
c = rand() % MAX + 1;
employee[c].m_bool = 1;
d = rand() % MAX + 1;
employee[d].m_bool = 1;
e = rand() % MAX + 1;
employee[e].m_bool = 1;
cout << "中了三等奖的是:" << employee[a].m_name<<" "<< employee[b].m_name << " " <<employee[c].m_name << " "
<< employee[d].m_name << " " <<employee[e].m_name << endl;
//抽二等奖
int c2[30];//定义一个数组用来取随机值
cout << "中了二等奖的是:";
for (int i = 0; i < 3;i++)
{
c2[i] = rand() % MAX + 1;
while (employee[(c2[i])].m_bool == 1 && employee[(c2[i])].m_level>=3) //如果中奖了那么继续取随机值,级别大于等于3也不行
{
c2[i+1] = rand() % MAX + 1;
i = i + 1;
}
c2[i] = rand() % MAX + 1;
cout << employee[(c2[i])].m_name << " ";
}
cout << endl;
//抽一等奖
cout << "中了一等奖的是:";
int i = 0;
c2[i] = rand() % MAX + 1;
while (employee[(c2[i])].m_bool == 1 && employee[(c2[i])].m_level >= 3)
{
c2[i +1] = rand() % MAX + 1;
i = i + 1;
}
c2[i] = rand() % MAX + 1;
cout << employee[(c2[i])].m_name << " ";
system("pause");
}