指针、冒泡函数、数组
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 temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << '\t';
}
cout << endl;
}
int main()
{
//指针、冒泡函数、数组
//1、创建数组
int arr[10] = { 40,29,95,100,72,89,67,39,55,47 };
//数组长度
int len = sizeof(arr) / sizeof(arr[0]);
//2、创建函数,实现冒泡排序
bubbleSort(arr, len);
//3、打印排序后的数组
printArray(arr, len);
cout << arr << endl;
cout << &arr[0] << endl;
cout << endl;
return 0;
}
从后面排序,第一个数最小
void swap(int arr1[], int i, int j)
{
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
void bubbleSort(int* arr, int len)
{
for (int i = 0; i < len - 1;i++)
{
for (int j = len - 1; j >= i; j--)
{
if (arr[j] < arr[j - 1])
{
swap(arr, j, j - 1);
}
}
}
}
void printArray(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << '\t';
}
cout << endl;
}
int main()
{
int arr[] = { 7, 3, 5, 9, 4, 2, 10, 6, 1 };
int len = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, len);
printArray(arr, len);
cout << endl;
return 0;
}
从前往后,保持第一个数最小
void swap(int arr1[], int i, int j)
{
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
void bubbleSort(int* arr, int len)
{
for (int i = 0; i < len - 1;i++)
{
for (int j = i + 1; j < len; j++)
{
if (arr[j] < arr[i])
{
swap(arr, j, i);
}
}
for (int j = 0; j < len; j++)
{
cout << arr[j] << '\t';
}
cout << endl;
}
}
void printArray(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << '\t';
}
cout << endl;
}
int main()
{
int arr[] = { 7, 3, 5, 9, 4, 2, 10, 6, 1 };
int len = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, len);
printArray(arr, len);
cout << endl;
return 0;
}
埋个坑-结构体数组
//结构体数组
//1.定义结构体
struct Student
{
string name;
int age;
int score;
};
//
int main()
{
//2.创建结构体数组
Student stuArray[3] =
{
{"张三",19,100},
{"李四",20,98},
{"王五",18,99}
};
//3.给结构体数组中的元素赋值
stuArray[2].name = "赵六";
//4.遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << stuArray[i].name << " " << stuArray[i].age
<< " " << stuArray[i].score << endl;
}
cout << sizeof(stuArray[0]) << endl;
string name = "张三";
cout << name.length() << endl;
cout << sizeof(name) << endl;
char ch[13] = "I love China";
cout << ch << endl;
const char* ch1 = "张三";
char* ch2 = "张三";
cout << ch1 << endl;
int len = sizeof(stuArray) / sizeof(stuArray[0]);
cout << len << endl << endl << endl;
cout << endl;
return 0;
}
埋个坑-结构体嵌套
//1.定义结构体
struct Student
{
string name = "";
int age = 1;
string sex;
};
struct Teacher
{
int no = 1000000;
string name = "";
int age = 0;
//string sex;
char sex;
Student stu;
};
//
int main()
{
Teacher t;
//Teacher t = { 1000,"王老师",30,"男",{"李强",18,"男"} };
t.no = 1000;
t.name = "王老师";
t.age = 30;
t.sex = '男'; //报错:截断
t.stu.age = 18;
t.stu.name = "李强";
t.stu.sex = "男";
cout << t.no << " " << t.name << " "
<< t.age << " " << t.sex
<< " " << t.stu.name << " "
<< t.stu.age << " " << t.stu.sex << endl;
//通过指针指向结构体变量
/*Student* p = &stu;
cout << p->name << " " << p->age
<< " " << p->sex << endl;*/
cout << endl;
return 0;
}
值传递/地址传递-函数参数
//1.定义结构体
struct Student
{
string name;
int age;
string sex;
};
void printStudent(Student s)
{
cout << "自定义值传递函数 中打印!" << endl;
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "性别:" << s.sex << endl
<< "··················\n";
}
void printStudent1(Student* s)
{
s->age = 18;
cout << "自定义地址传递函数 中打印!" << endl;
cout << "姓名:" << s->name << endl;
cout << "年龄:" << s->age << endl;
cout << "性别:" << s->sex << endl
<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
//
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生的所有信息
Student s = { "张三", 20, "男" };
cout << "main 中打印!" << endl;
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "性别:" << s.sex << endl
<< "************************************\n";
printStudent(s);
printStudent1(&s);
cout << "main 中打印!" << endl;
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "性别:" << s.sex << endl
<< "************************************\n";
cout << endl;
return 0;
}
const结构体指针
//1.定义结构体
struct Student
{
string name;
int age;
string sex;
};
void printStudent(Student);
void printStudent1(Student*);
//
int main()
{
Student s = { "王磊", 22, "男" };
printStudent(s);
cout << endl;
return 0;
}
void printStudent(Student s)
{
s.age = 100;
cout << "自定义值传递函数 中打印!" << endl;
cout << "姓名:" << s.name << endl;
cout << "年龄:" << s.age << endl;
cout << "性别:" << s.sex << endl
<< "··················\n";
}
//将函数中的形参改为指针,可以减少内存空间
//(每次调用都只是调用四字节的指针)
//而且不会赋值新的副本
//为了防止误操作,即s->age=18,修改结构体实体信息,采用const
void printStudent1(const Student* s)
{
//s->age = 18; //不能修改
cout << "自定义地址传递函数 中打印!" << endl;
cout << "姓名:" << s->name << endl;
cout << "年龄:" << s->age << endl;
cout << "性别:" << s->sex << endl
<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
}
3个教师带5个学生-结构体
//1.定义结构体
struct Student
{
string sName;
string sex;
};
struct Teacher
{
string tName;
Student sArray[5];
};
void printStudent(Student);
void printStudent1(const Student*);
void allocateSpace(Teacher t[], int len)
{
string nameSeed = "abcde";
string nameSeed2 = "12345";
for (int i = 0; i < len; i++)
{
t[i].tName = "Teacher_";
t[i].tName += nameSeed[i];
for (int j = 0; j < 5; j++)
{
t[i].sArray[j].sName = "Student_";
t[i].sArray[j].sName += nameSeed2[j];
if (j % 2 == 0)
{
t[i].sArray[j].sex = "female";
}
else
{
t[i].sArray[j].sex = "male";
}
}
}
}
void printInfo(Teacher t[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "教师姓名:" << t[i].tName << endl;
for (int j = 0; j < 5; j++)
{
cout << " 学生姓名:" << t[i].sArray[j].sName
<< " 性别:" << t[i].sArray[j].sex << endl;
}
}
}
int main()
{
Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
printInfo(tArray, len);
cout << endl;
return 0;
}
对五个同类型的结构体对象排序
struct Hero
{
string name;
int age;
string sex;
};
void heroSort(Hero* h, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (h[j].age > h[j + 1].age)
{
Hero temp = h[j];
h[j] = h[j + 1];
h[j + 1] = temp;
}
}
}
}
void printHero(Hero* h, int len)
{
cout << "姓名\t" << "年龄\t" << "性别\n";
for (int i = 0; i < len; i++)
{
cout << h[i].name << "\t" << h[i].age
<< "\t" << h[i].sex << endl;
}
}
int main()
{
Hero h[5] =
{
{ "刘备",23,"男" },
{ "关羽",20,"男" },
{ "赵云",22,"男" },
{ "张飞",20,"男" },
{ "貂蝉",19,"女" }
};
int len = sizeof(h) / sizeof(h[1]);
heroSort(h, len);
printHero(h, len);
cout << endl;
return 0;
}
冒泡排序
#include <iostream>
#include <cmath>
#include <limits>
#include <string>
#include <ctime>
#include "swap.h"
using namespace std;
//从小到大
void sort1(int* arr, int n)
{
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//从大到小
//void sort2(int* arr, int n)
//{
// for (int i = 0; i < n - 1; i++)
// {
// for (int j = n - 1; j > n - 1 - i; j--)
// {
// if (arr[j] < arr[j - 1])
// {
// int temp = arr[j];
// arr[j] = arr[j - 1];
// arr[j - 1] = temp;
// }
// }
// }
//}
void sort2(int* arr, int n)
{
for (int i = n - 1; i > 0; i--)
{
for (int j = n - 1; j > n - 1 - i; j--)
{
if (arr[j] < arr[j - 1])
{
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << "\t";
}
cout << endl;
}
int main()
{
int array[] = { 5, 9, 1, 9, 5, 3, 7, 6, 2 };
int len = sizeof(array) / sizeof(array[0]);
sort1(array, len);
cout << "第一种排序:从小到大!\n";
printArray(array, len);
sort2(array, len);
cout << "第二种排序:从大到小!\n";
printArray(array, len);
return 0;
}