此文章是学习c++语言所做的案例,包含c++基础所有的知识点,案例具体内容包含:
学习途径来源:哔哩哔哩_bilibili
- 最重小猪
- 猜数字
- 水仙花数
- 敲桌子
- 乘法口诀表
- 五只小猪称体重
- 数组元素逆置
- 冒泡排序
- 考试成绩统计
- 指针
- 结构体案例1
- 结构体案例2
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
//最重小猪
void pig_weight()
{
int a,b,c;
cout << "请输入第一只小猪的体重:" ;
cin >> a ;
cout << "请输入第二只小猪的体重:";
cin >> b;
cout << "请输入第三只小猪的体重:";
cin >> c;
int d=(a > b ? a : b)>c? (a > b ? a : b):c;
if (d == a) {
cout << "第一只小猪最重" <<d<< endl;
}else if (d == b) {
cout << "第二只小猪最重" <<d<< endl;
}else if (d == c) {
cout << "第三只小猪最重" <<d<< endl;
}
//system("pause");
//return 0;
}
//猜数字
void guess_math() {
srand((unsigned int)time(NULL));
int num = rand()%100+1;
int gm;
cin >>gm;
while (num != gm) {
num > gm ? cout << "小了" << endl : cout << "大了" << endl;
cin >> gm;
}
//system("pause");
//return 0;
}
//水仙花数
void narcissistic_number(){
int num = 100;
int sum = 0;
do {
int a, b, c;
a = num % 10;
b = (num / 10)%10;
c = num / 100;
sum = a * a * a + b * b * b + c * c * c;
if (num == sum) {
cout << num << "是水仙花数" << endl;
}
num++;
} while (num < 1000);
//system("pause");
//return 0;
}
//敲桌子
void konck_table() {
int i;
for (i = 1; i < 100; i++) {
if (i%7==0 || i/10==7 || i%10==7) {
continue;
}
else {
cout << i << endl;
}
}
//system("pause");
//return 0;
}
//乘法口诀表
void multiplication_table() {
int i, j;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
cout << i << "x" << j << "=" << i * j << "\t";
}
cout << endl;
}
//system("pause");
//return 0;
}
//五只小猪称体重
void five_pig_weight() {
int pig_weight[5]= { 300,350,500,400,250 };
int max_weight = pig_weight[0];
for (int i = 1; i < 5; i++) {
if (max_weight < pig_weight[i]) {
max_weight = pig_weight[i];
}
}
cout << "最重的小猪体重为:" << max_weight;
//system("pause");
//return 0;
}
//数组元素逆置
void array_element_inversion() {
int array[5] = {1,3,2,5,4};
int array1[5] = {};
for (int i = 0; i < 5; i++) {
array1[i] = array[4 - i];
cout << array1[i] << endl;
}
//system("pause");
//return 0;
}
//冒泡排序
void bubble_sorting() {
int arr[9] = { 4,2,8,0,5,7,1,3,9 };
cout << "重大到小排序:" << endl;
for (int i = 0; i < 9-1; i++) {
int a = 0 ;
for (int j = i + 1; j < 9; j++) {
if (arr[i] < arr[j]) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
for (int z = 0; z < 9; z++) {
cout << arr[z];
}
cout << endl;
}
for (int i = 0; i < 9; i++) {
cout << arr[i] << endl;
}
cout << "重小到大排序:" << endl;
for (int i = 0; i < 9-1; i++) {
int a = 0;
for (int j = i + 1; j < 9; j++) {
if (arr[i] > arr[j]) {
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
}
for (int z = 0; z < 9; z++) {
cout << arr[z];
}
cout << endl;
}
for (int i = 0; i < 9; i++) {
cout << arr[i] << endl;
}
cout << "冒泡排序" << endl;
for (int i = 0; i < 9 - 1; i++) {
int a ;
for (int j = 0; j < 9-1-i; j++) {
if (arr[j] > arr[j+1]) {
a = arr[j+1];
arr[j+1] = arr[j];
arr[j] = a;
}
}
for (int z = 0; z < 9; z++) {
cout << arr[z];
}
cout << endl;
}
for (int i = 0; i < 9; i++) {
cout << arr[i] << endl;
}
int arr3[][3] = { 1,2,3,4,5,6,7,8 };
cout << "总长度为"<<sizeof(arr3) / sizeof(arr3[0][0]) << endl;
for (int i = 0; i < sizeof(arr3) / sizeof(arr3[0]) ; i++) {
for (int j = 0; j < sizeof(arr3[0]) / sizeof(arr3[0][0]); j++) {
cout << arr3[i][j] << " ";
}
cout << endl;
}
}
//考试成绩统计
int exam_score_statistics() {
int score[3][3] = { {100,100,100},{90,50,100},{60,70,80} };
string name[3] = { "张三","李四","王五" };
cout << "\t语文\t" << "数学\t" << "英语\t" <<"总成绩\t" << endl;
for (int i = 0; i < 3; i++) {
int sum = 0;
cout << name[i] << "\t";
for (int j = 0; j < 3; j++) {
sum += score[i][j];
cout << score[i][j] << "\t";
}
cout <<sum<< endl;
}
//system("pause");
return 0;
}
//指针
void pointer() {
int a = 10;
int b = 20;
cout << "a的地址:" << &a << "\ta的值:" << a << endl;
cout << "b的地址:" << &b << "\tb的值:" << b << endl;
//常量指针指向的内存地址可以改变(值也会改成内存对应的值),但不能够对常量指针指向内存地址对应的值进行改变
//指针常量不可以修改指向的内存地址,但能够修改内存地址对应的值
//指针和变量都被const修饰的时候,内存地址和对应的值都不能被修改
const int * p = &a;
int * const p1 = &a;
const int* const p2 = &a;
p = &b;
cout << "p的地址:" << p << "\tp的值:" << *p << endl;
*p1 = b;
cout << "p的地址:" << p << "\tp的值:" << *p << endl;
cout << "p1的地址:" << p1 << "\tp1的值:" << *p1 << endl;
cout << "p2的地址:" << p2 << "\tp2的值:" << *p2 << endl;
cout << "a的地址:" << &a << "\ta的值:" << a << endl;
}
//老师结构体
struct Student {
string name;
int score=NULL;
};
//学生结构体
struct Teacher {
string name;
Student arr[5];
};
//打印信息
void print_information(struct Teacher t[]) {
for (int i = 0; i < 3; i++) {
cout << "老师姓名:" << t[i].name << "\t学生姓名" << "\t学生成绩" << endl;
for (int j = 0; j < 5; j++) {
cout << "\t\t" << t[i].arr[j].name << "\t" << t[i].arr[j].score << endl;
}
}
}
//结构体案例1--teacher
void Structure_Case1() {
Teacher t[3] ;
Student s[3][5];
string tea_name[3] = {"张三","李四","王五"};
string stu_name[3][5];
int stu_score[3][5];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
stu_name[i][j] = tea_name[i] +"学生"+ to_string(j+1);
stu_score[i][j] = (i + 2) * (j + 4) * 3;
}
}
for (int i = 0; i < 3; i++) {
t[i].name = tea_name[i];
for (int j = 0; j < 5; j++) {
s[i][j].name = stu_name[i][j];
s[i][j].score = stu_score[i][j];
t[i].arr[j] = s[i][j];
}
}
print_information(t);
}
//英雄结构体
struct Hero {
string name;
int age;
string gender;
};
void Structure_Case2() {
//Hero h1 = { "刘备", 23, "男" };
//Hero h2 = { "关羽", 22 ,"男" };
//Hero h3 = { "张飞", 20, "男" };
//Hero h4 = { "赵云", 21, "男" };
//Hero h5 = { "貂蝉", 19, "女" };
Hero h[5] = {
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"},
};
//int arr[5] = { h1.age ,h2.age ,h3.age ,h4.age ,h5.age };
for (int i = 0; i < sizeof(h) / sizeof(h[0])-1; i++) {
for (int j = 0; j < sizeof(h) / sizeof(h[0]) - 1 - i; j++) {
if (h[j].age > h[j + 1].age) {
Hero temp = h[j];
h[j] = h[j + 1];
h[j + 1] = temp;
}
}
}
for (int i = 0; i < sizeof(h) / sizeof(h[0]); i++) {
cout << h[i].name << "\t" << h[i].age << "\t" << h[i].gender << endl;
}
}