1.数组升序
#include<iostream>
using namespace std;
//冒泡排序
void bubbleSort(int* arr, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1-i; j++) {
if (arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
//打印输出
void printArray(int* arr, int len) {
for (int i = 0; i < len ; i++) {
cout << arr[i] <<" ";
}
cout << "\b" << endl;
}
int main() {
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(arr[0]);
printArray(arr, len);
bubbleSort(arr, len);
printArray(arr, len);
system("pause");
return 0;
}
2.打印分数
#include<iostream>
#include<stdio.h>
#include<string>
#include<ctime>
using namespace std;
//Student必须写在Teacher前面
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; //40~100分
tArray[i].sArray[j].score=random;
}
}
}
//打印输出
void printInfo(struct Teacher tArray[], int len) {
for (int i = 0; i < len; i++) {
cout << "老师姓名:" << tArray[i].tName << endl;
for (int j = 0; j < 5; j++) {
cout << "\t学生姓名:" << tArray[i].sArray[j].sName <<
" 考试分数:" << tArray[i].sArray[j].score << endl;
}
}
}
int main() {
srand((unsigned int)time(NULL));
struct Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
printInfo(tArray, len);
system("pause");
return 0;
}
3.英雄排序
#include<iostream>
#include<string>
using namespace std;
struct Hero {
string name;
int age;
string sex;
};
//冒泡排序
void bubbleSort(struct Hero heroArray[], int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (heroArray[j].age > heroArray[j + 1].age) {
struct Hero tmp = heroArray[j];
heroArray[j] = heroArray[j + 1];
heroArray[j + 1] = tmp;
}
}
}
}
//打印输出
void printArray(struct Hero heroArray[], int len) {
for (int i = 0; i < len; i++) {
cout <<"英雄:" <<heroArray[i].name<< " 年龄:" <<heroArray[i].age << " 性别:" <<heroArray[i].sex << endl;
}
}
#include<iostream>
#include<string>
using namespace std;
struct Hero {
string name;
int age;
string sex;
};
extern void bubbleSort(struct Hero heroArray[], int len);
extern void printArray(struct Hero heroArray[], int len);
int main() {
struct Hero heroArray[5] = {
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
int len = sizeof(heroArray) / sizeof(heroArray[0]);
cout << "排序前:" << endl;
printArray(heroArray, len);
bubbleSort(heroArray, len);
cout << "排序后:" << endl;
printArray(heroArray, len);
system("pause");
return 0;
}
4.test1
#include<iostream>
#include<string>
using namespace std;
class Bag{
public:
Bag(int b=30):br(b){cout<<br<<" ";}
int fun() const {
cout<<"const在函数上的用法";
return this->br;
}
private:
int br;
};
template<class T1,typename T2,typename T3>
int fun(T1 a,T2 b,T3 c) {
return 1;
}
int main(){
Bag a[2];
a[0].fun();
const int b=0;
int const c=0;
cout<<b<<' '<<c<<endl;
int d=1;
const int *p=&d;
cout<<p<<' '<<*p<<endl;
cout<<&p<<endl;
//开始先赋值,再判断,然后是否进入循环语句
for(int i=0;i<0;i++)
printf("1\n");
// cout<<"你好"<<endl;
// string str;
// cin>>str;
// cout<<str<<endl;
Bag *p1;//=NULL;
p1=NULL;
delete p1;//delete可作用于空指针
system("pause");
return 0;
}
5.test2
#include<iostream>
#include<string>
#include <cstring>
using namespace std;
class Base{
public:
void fun1(){cout<<"Base";}
virtual void fun2(){cout<<"Base";}
};
class Derived:public Base{
public:
void fun1(){cout<<"Derived";}
void fun2(){cout<<"Derived";}
};
void f(Base b)//BaseBase//void f(Base& b)//BaseDerived
{
b.fun1();
b.fun2();
}
int main()
{
// int b=10;
// int &a;要赋初值
// int c=1;
// char c='d';
// cout<<c<<endl;
// string str;
// str="12";
// string *p;
// p=&str;
// int a=p;
// cout<<a<<endl;
char* p;
p="1";
int a=*p;
cout<<a<<endl;
char g[10]="f3r";
cout<<g<<endl;
//string c;
char c[10];
strcpy(c,"12e");//'24'
cout<<c<<endl;
char d='2';//'22';
cout<<d<<endl;
// char e[2]='22';
// cout<<e<<endl;
Derived obj;
f(obj);
return 0;
}
标签:struct,int,void,练习,len,C++,heroArray,include
From: https://blog.csdn.net/qq_69866029/article/details/136996954