评委打分案例
1.利用容器
#include <iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<algorithm>
class Person
{
public:
Person(string name, int score)
{
this->m_name = name;
this->m_score = score;
}
string m_name;
int m_score;
};
void xuanshou(vector<Person>& v)
{
string m = "ABCDE";
for (int i = 0; i < 5; i++)
{
string name = "选手 ";
name += m[i];
int score = 0;
Person p(name, score);
v.push_back(p);
}
}
void setscore(vector<Person>& v)
{
deque<int>d;
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
for (int i = 0; i < 10; i++)
{
int score = rand() % 41 + 60;
d.push_back(score);
}
sort(d.begin(), d.end());
d.pop_back();
d.pop_front();
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
sum += *dit;
}
int aver = sum / d.size();
it->m_score = aver;
}
}
void showperson(vector<Person>& v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名: " << it->m_name << "平均分: " << it->m_score << endl;
}
}
int main()
{
srand((unsigned int)time(NULL));//随机时间种子
vector<Person> v;
xuanshou(v);
setscore(v);
showperson(v);
system("pause");
}
2.利用STL容器
#include <iostream>
#include <string>
#include<vector>
#include<deque>
#include <algorithm>
using namespace std;
class Player
{
public:
string name;
int score;
Player(){}
Player(string n,int s):name(n),score(s){}
};
bool mtcompare(Player& p1, Player& p2)
{
return (p1.score > p2.score);
}
void printmy(int n)
{
cout << n << " ";
}
//创建选手信息
void Setplay(vector<Player>& v)
{
string nameseed = "ABCDE";
for (int i = 0; i < 5; i++)
{
string n = "选手";
n += nameseed[i];
Player p;
p.name = n;
p.score = 0;
v.push_back(p);
}
}
//打分
void Setscore(vector<Player>& v)
{
for (vector<Player>::iterator it = v.begin(); it != v.end(); it++)
{
deque<int>d;
for(int i=0;i<10;i++)
{
(*it).score = rand() % 41 + 60;
d.push_back((*it).score);
}
/*for_each(d.begin(), d.end(), printmy);
cout << endl;*/
//排序一下
sort(d.begin(), d.end());
//删除最高分和最低分
d.pop_front();
d.pop_back();
int total = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
{
total += (*dit);
}
int ave = total / d.size();
(*it).score = ave;
}
}
//打印排序输出
void get(vector<Player>& v)
{
sort(v.begin(), v.end(), mtcompare);
for (vector<Player>::iterator it = v.begin(); it != v.end(); it++)
{
cout << (*it).name << " " << "分数为:" << (*it).score << endl;
}
}
int main()
{
vector<Player>v;
Setplay(v);
Setscore(v);
get(v);
}
成绩案例
class Exam
{
public:
Exam(string name, char grade) :m_name(name), mode(GRADE), grade(grade) {};
Exam(string name, bool pass) :m_name(name), mode(PASS), pass(pass) {};
Exam(string name, int percent) :m_name(name), mode(PERCENT), percent(percent) {};
void show()
{
cout << m_name << ":";
switch (mode)
{
case Exam::GRADE:
cout << grade;
break;
case Exam::PASS:
cout << (pass ? "pass" : "fall");
//cout << pass;
break;
case Exam::PERCENT:
cout << percent;
break;
default:
break;
}
cout << endl;
}
private:
string m_name;
enum
{
GRADE, PASS, PERCENT
}mode;
union
{
char grade;
bool pass;
int percent;
};
};
int main0102()
{
Exam s1("English", 's');
Exam s2("daxuewuli", true);
Exam s3("c++", 95);
s1.show();
s2.show();
s3.show();
return 0;
}
职工结婚状况统计案例
class person
{
public:
string name;
char sex;
int age;
enum
{
we, merry, divoce
}mode;
struct date
{
int year;
int mounth;
int day;
};
struct marrystate
{
struct date marrydate;
string nv;
int child;
};
struct divoced
{
struct date divodate;
int child;
};
union
{
int sigle;
struct marrystate marry;
struct divoced divoce;
};
public:
person(string m_name,char m_sex,int m_age,int n) :name(m_name),sex(m_sex),age(m_age),sigle(n), mode(we) {};
person(string m_name, char m_sex, int m_age, struct marrystate marry1) :name(m_name), sex(m_sex), age(m_age),marry(marry1) ,mode(merry){};
person(string m_name, char m_sex, int m_age, struct divoced divoce1) :name(m_name), sex(m_sex), age(m_age),divoce(divoce1) ,mode(divoce){};
void show()
{
switch (mode)
{
case we:
cout << name << " " << sex << " " << age << " "<<"未婚"<<endl;
break;
case merry:
cout << name << " " << sex << " " << age << " " << "已婚" << endl;
cout << marry.marrydate.year << "年" << marry.marrydate.mounth << "月" << marry.marrydate.day << "日" << endl;
cout << "配偶名字: " << marry.nv << endl;
cout << "孩子数量: " << marry.child << endl;
break;
case divoce:
cout << name << " " << sex << " " << age << " " << "离婚" << endl;
cout << divoce.divodate.year << "年" << divoce.divodate.mounth << "月" << divoce.divodate.day << "日" << endl;
cout << "孩子数量: " << divoce.child << endl;
break;
default:
break;
}
}
~person()
{
}
};
int main0103()
{
class person p1("泰罗", 'n', 10, { {1974,10,24},"迪迦",3});
class person p2("袁宇恒", 'n', 10, 1);
p1.show();
p2.show();
}
运用枚举来运行cpu
//4-19
#include<iostream>
using namespace std;
#define pi 3.14
enum Core{single,two,four};
enum Worldlen{bit32,bit64};
enum Xian{support,diesupport};
class Cpu
{
public:
Cpu(unsigned m_hz,Core type,Worldlen len,Xian x):hz(m_hz),coretype(type),wordlen(len),xian(x){}
void show()
{
cout << "频率:" << hz << endl;
cout << "CORE: ";
switch (coretype)
{
case single:cout << "单核" << endl;
break;
case two:cout << "双核" << endl;
break;
case four:cout << "四核" << endl;
break;
default:
break;
}
cout << "字节长:";
switch (wordlen)
{
case bit32:cout << "bit-32 " << endl;
break;
case bit64:cout << "bit -64 " << endl;
break;
default:
break;
}
cout << "线程:";
switch (xian)
{
case support:cout << "支持多线程" << endl;
break;
case diesupport:cout << "不支持多线程" << endl;
break;
default:
break;
}
}
private:
unsigned hz;
Core coretype;
Worldlen wordlen;
Xian xian;
};
int main()
{
Cpu c(300000, two, bit32, support);
c.show();
}