随机创建五个英雄,给通过函数给其各项属性赋值,例如初始血量、英雄类型、英雄得分等,均采用随机赋值的方式,使用两种冒泡排序、一种通过血量一种通过得分,在建立一个函数用于选择是血量还是得分,最后在根据需求判断是否还需要继续。
#include#include #include using namespace std; //设计一个英雄的结构体,首先给英雄个属性赋值,再给英雄一个得分,如何冒泡排序,对得分排序打印 struct Hero { string name; string type; int num_blood; int score; int id; }; void give_values(Hero hero[]) { string player[] = { "lpl","lck","外卡" }; string name_arr[] = { "-卡特琳娜", "-麦林炮手", "-德莱厄斯" }; string type_arr[] = { "刺客","ADC","战士" }; int perminary_blood = 1000; int score = 60; int change = rand() % 41; for (int i = 0;i < 5;i++) { //创建英雄 hero[i].name = player[rand() % 3];//随机创建不同玩家使用的英雄,共计五个。 //给英雄名字赋值 hero[i].name += name_arr[rand() % 3]; //整体名字赋值 //给英雄类型赋值 hero[i].type = type_arr[rand() % 3]; //给得分赋值 hero[i].score = score; hero[i].score += rand() % 41; //给初始血量赋值 hero[i].num_blood = perminary_blood; hero[i].num_blood += rand() % perminary_blood; //给id赋值 hero[i].id = i+1; } } //打印英雄属性值 void print(Hero hero[],int len) { for (int i = 0;i < len;i++) { cout <<"序号:"< > a; go_on(hero, len, a); string o = ""; while (true) { //序号判断是否输入错误,错误则一直询问,不需要的话就推出,需要就再给一次机会 cout << "还需操作吗? Y or N" << endl; cin >> o; if (o == "N") { cout << "拜拜" << endl; break; } else if (o =="Y") { cout << "请选择" << endl; cin >> a; go_on(hero, len, a); break; } } } int main() { srand((unsigned int)time(NULL)); //随机种子 struct Hero hero[5]; //创建结构提 int len = sizeof(hero) / sizeof(hero[0]); give_values(hero); //给英雄赋值 choose_revense(hero, len); //选择函数 return 0; system("pause"); }
截图验证



