#include <iostream>
#include <stdio.h>
#include <mysql.h>
#include <string.h>
#include <math.h>
#include <ctime>
using namespace std;
//水仙花数
void shuixianhuashu() {
int num = 100;
do {
num++;
int gewei = (num % 100) % 10;
int shiwei = (num % 100) / 10;
int baiwei = num / 100;
if (pow(baiwei, 3) + pow(shiwei, 3) + pow(gewei, 3) == num) {
cout << num << "是水仙花数" << endl;
}
} while (num < 1000);
}
//拍桌子游戏
void paizhuozi() {
for (int i = 0; i < 100; i++) {
if (i / 10 == 7 || i % 10 == 7 || i % 7 == 0) {
//cout << i / 10 << i % 10 << i % 7 << endl;
cout << i << "拍桌子" << endl;
}
else {
cout << i << endl;
}
}
}
// 数组求长度函数
template<class T>
int length(T& arr)
{
return sizeof(arr) / sizeof(arr[0]);
}
void biggest() {
int arr[5] = { 300,350,200,400,250 };
int temp = 0;
cout << length(arr) << endl;
for (int i = 0; i < length(arr); i++) {
if (temp < arr[i + 1]) {
temp = arr[i + 1];
}
else {
}
}
cout << temp << endl;
}
// 学生数据类型 结构体
// 结构体数组
struct Student {
string sName;
int score;
};
struct Teacher {
string tName;
struct Student sArray[5];
};
void allocateSpace(struct Teacher tArray[], int len) {
string nameSeed = "ABCDE";
for (int i = 0; i < len; i++) {
tArray[i].tName = "Teacher_";
tArray[i].tName += nameSeed[i];
for (int j = 0; j < 5; j++) {
tArray[i].sArray[j].sName = "Student_";
tArray[i].sArray[j].sName += nameSeed[j];
int random = rand() % 61+40;
tArray[i].sArray[j].score = random;
}
}
};
void printInfo(struct Teacher tArray[], int len) {
for (int i = 0; i < len; i++) {
cout << "Teacher's name:" << tArray[i].tName << endl;
for (int j = 0; j < 5; j++) {
cout << "\tStudent's name:" << tArray[i].sArray[j].sName <<
" Score:"<< tArray[i].sArray[j].score <<endl;
}
}
};
void structexp() {
srand((unsigned int)time(NULL));//随机数种子
struct Teacher tArray[3];
allocateSpace(tArray, length(tArray));
printInfo(tArray, length(tArray));
};
struct hero {
string name;
int age;
string sex;
};
void heroSort(hero heroArray[],int len) {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len - i - j; j++) {
if (heroArray[j].age < heroArray[j + 1].age) {
struct hero temp = heroArray[j];
heroArray[j]= heroArray[j + 1];
heroArray[j + 1]= temp;
}
}
}
for (int i = 0; i < len; i++) {
cout << heroArray[i].name << heroArray[i].age << heroArray[i].sex << endl;
}
}
void structexp2() {
hero heroArray[5] = {
{"刘备",23,"M"},
{"关羽",22,"M"},
{"张飞",20,"M"},
{"赵云",21,"M"},
{"貂蝉",19,"W"},
};
heroSort(heroArray, 6);
}
int main() {
structexp2();
system("pause");
return 0;
}
【C++基础练习】第一部分
最新推荐文章于 2024-08-28 14:35:43 发布