Dark horse programmer's ingenuity-structure case

#include <iostream>
using namespace std;
#include<string>
#include<ctime>
struct student1
{
    string name;
    int score;
};
struct teature
{
    string name;
    student1 stu[5];
};
void allocate(teature tea[],int len)
{
    string name = "ABCDE";
    
    for (int i = 0; i < len;i++)
    {
        tea[i].name = "teature";
        tea[i].name += name[i];
        for (int j = 0; j < 5; j++)
        {
            tea[i].stu[j].name ="student_";
            tea[i].stu[j].name += name[j];
            int random = rand() % 61 + 40;
            tea[i].stu[j].score = random;

        }

    }
}
int main()
{
    //随机数种子
    srand((unsigned int)time(NULL));
    teature tea[3];
    int len = sizeof(tea) / sizeof(tea[0]);
    allocate(tea, len);
    for (int i = 0; i < len; i++)
    {
        cout << "老师姓名:" <<tea[i].name << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << "\t学生姓名 " << tea[i].stu[j].name << " 分数:" << tea[i].stu[j].score<<endl;
        }
    }
    system("pause");
    return 0;
}

#include<iostream>
#include<string>
using namespace std;
struct hero
{
	string name;
	int age;
	string xingbie;
};
void sortage(hero Heroarray[], int len)
{
	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (Heroarray[j].age > Heroarray[j + 1].age)
			{
				hero temp = Heroarray[j];
				Heroarray[j] = Heroarray[j + 1];
				Heroarray[j + 1]= temp;
			}
		}
	}
	for (int i = 0; i < len; i++)
	{
		cout << "姓名:" << Heroarray[i].name << "\t 年龄:" << Heroarray[i].age << "  性别:" << Heroarray[i].xingbie << endl;
	}
}
int main()
{
	hero HeroArray[3] = { {"tom",12,"男"},{"jerry",16,"男"},{"amy",11,"女"} };
	int len = sizeof(HeroArray) / sizeof(HeroArray[0]);
	sortage(HeroArray, len);
	system("pause");
	return 0;
}

 

Guess you like

Origin blog.csdn.net/yyyllla/article/details/109307543