1.输入三个数判断大小
方法1:使用选择语句实现
#include<iostream>
using namespace std;
int main() {
int a = 0;
int b = 0;
int c = 0;
cout << "输入第1只猪的重量:" << endl;
cin >> a;
cout << "输入第2只猪的重量:" << endl;
cin >> b;
cout << "输入第3只猪的重量:" << endl;
cin >> c;
if (a > b)
{
if (a > c)
{
cout << "第1只猪最重" << endl;
}
else
{
cout << "第3只猪最重" << endl;
}
}
else
{
if (b > c)
{
cout << "第2只猪最重" << endl;
}
else
{
cout << "第3只猪最重" << endl;
}
}
}
方法2:使用max函数实现
#include<iostream>
using namespace std;
int main() {
int a, b, c;
cout << "请输入三个数:" << endl;
cin >> a >> b >> c;
int maxnum = 0;
maxnum = max(max(a, b), c); //采用max函数计算最大值
cout << "最大的数是:" << maxnum << endl;
}
2.猜数字
#include<iostream>
#include<ctime>
using namespace std;
int main() {
srand((unsigned int)time(NULL)); //每次生成的随机数不同
int n = rand() % 100 + 1; //生成1-100之间的一个随机数
int i = 0;
while (i != n) {
cin >> i;
if (i > n) {
cout << "数字大了" << endl;
}
else if(i < n){
cout << "数字小了" << endl;
}
else {
cout << "猜对了" << endl;
break;
}
}
}
3.判断水仙花数
#include<iostream>
using namespace std;
int main() {
int i = 100;
int sum = 0;
while (i < 1000) {
int a = i % 10; //个位
int b = i / 10 % 10; //十位
int c = i / 100; // 百位
if (i == pow(a,3) + pow(b,3) + pow(c,3)) {
cout << i << endl;
}
i++;
}
}
4.输出100以内和7有关的数字
#include<iostream>
using namespace std;
int main() {
int i = 1;
for (; i < 101; i++) {
if (i % 7 == 0) { //7的倍数
cout << "敲桌子" << endl;
}
else if (i % 10 == 7) { //个位数字是7的数
cout << "敲桌子" << endl;
}
else if (i / 10 == 7) { //十位数字是7的数
cout << "敲桌子" << endl;
}
else {
cout << i << endl;
}
}
}
5.打印星阵
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 11; j++) {
cout << "* ";
}
cout << endl; //换行
}
}
6.打印乘法口诀表
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i < 10; i++) {
for (int j = 1; j <=i; j++) {
cout << j << "*" << i << "=" << j * i << " ";
}
cout << endl;
}
}
7.寻找数组中最大的数
#include<iostream>
using namespace std;
int main() {
int a[] = {900,200,700,450,500};
int temp = a[0];
for (int i = 1; i < 5; i++) {
if (a[i] > temp) {
temp = a[i];
}
}
cout << "MAX = " << temp << endl;
}
8.数组元素逆置
#include<iostream>
using namespace std;
int main() {
int a[] = {1,3,2,5,4,9};
int start = 0; //第一个数组元素的下标
int end = sizeof(a) / sizeof(a[0]); //数组长度
int temp = 0; //用来交换元素值
for (int i = 0; i < end/2; i++) {
temp = a[i];
a[i] = a[end-i-1];
a[end - i - 1] = temp;
}
for (int j = 0; j < end; j++) { //循环输出逆序后的数组
cout << a[j] << endl;
}
}
9.冒泡排序
#include<iostream>
using namespace std;
int main() {
int a[] = { 4,2,8,0,5,7,1,3,9 };
cout << "排序前:" << endl;
for (int i = 0; i < 9; i++) {
cout << a[i] << " ";
}
cout << endl;
//开始冒泡排序
for (int j = 0; j < 8; j++) { //对比的轮数
for (int k = 0; k < 8 - j; k++) { //每轮对比的次数
if (a[k] > a[k+1]) {
int temp = a[k+1]; //交换元素值
a[k+1] = a[k];
a[k] = temp;
}
}
}
cout << "排序后:" << endl;
for (int i = 0; i < 9; i++) {
cout << a[i] << " ";
}
cout << endl;
}
10.封装冒泡排序函数
#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 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void print(int* arr, int len) {
for (int i = 0; i < len; i++) {
cout << arr[i] << " ";
}
}
int main() {
int arr[] = { 2,5,7,9,1,4,3,8,6 };
int len = sizeof(arr) / sizeof(arr[0]);
bubblesort(arr, len); //调用bubblesort函数
print(arr, len); //调用print函数
cout << endl;
}
11.使用指针遍历数组元素
#include<iostream>
using namespace std;
int main() {
int arr[] = { 10,20,30,40,50,60,70,80,90,100 };
int* p = arr;
for (int i = 0; i < 10; i++) { //使用指针遍历数组
cout << *p << " ";
p++;
}
cout << endl;
}
12.使用指针访问函数
#include<iostream>
using namespace std;
void swap(int* p, int* q) {
int temp = *p;
*p = *q;
*q = temp;
}
int main() {
int a = 10;
int b = 20;
swap(&a, &b); //地址传递时,形参会影响实参
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
13.使用指针访问结构体
#include<iostream>
using namespace std;
#include<string>
struct student { //创建一个名为student的结构体数据类型
string name;
int age;
int score;
};
int main() {
student s = { "ty",24,18000 }; //创建结构体变量并赋值
student* p = &s;
// 直接访问结构体变量
cout << "姓名:" << s.name << " "
<< "年龄:" << s.age << " "
<< "分数:" << s.score << " " << endl;
// 指针访问结构体变量
cout << "姓名:" << p->name << " "
<< "年龄:" << p->age << " "
<< "分数:" << p->score << " " << endl;
}
14.结构体案例1
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
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() % 60 + 40; //1~99之间随机生成数
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);
}
Teacher_A所带的学生信息
Student_A 60
Student_B 50
Student_C 66
Student_D 90
Student_E 87
Teacher_B所带的学生信息
Student_A 91
Student_B 89
Student_C 87
Student_D 95
Student_E 41
Teacher_C所带的学生信息
Student_A 47
Student_B 56
Student_C 50
Student_D 68
Student_E 66
15.结构体案例2
案例描述:
设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排序,最终打印排序后的结果。
#include<iostream>
using namespace std;
struct hero { //创建结构体
string name;
int age;
string sex;
};
void bubblesort(struct hero array[], int len) { //冒泡排序函数
for (int i = 0; i < len-1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (array[j].age > array[j + 1].age) {
int temp = array[j].age;
array[j].age = array[j + 1].age;
array[j + 1].age = temp;
}
}
}
}
void printinfo(struct hero array[], int len) { //打印函数
for (int i = 0; i < len; i++) {
cout << "\t" << array[i].name << " " << "\t" << array[i].age << " " << "\t" << array[i].sex << " " << endl;
}
}
int main() {
struct hero array[5] = { //创建结构体数组
{"马云",59,"男"},
{"马化腾",50,"男"},
{"雷军",48,"男"},
{"马斯克",55,"男"},
{"董明珠",57,"女"},
};
int len;
len = sizeof(array) / sizeof(array[0]);
cout << "排序前人物信息:" << endl;
printinfo(array, len);
cout << "排序后人物信息:" << endl;
bubblesort(array, len);
printinfo(array, len);
}
排序前人物信息:
马云 59 男
马化腾 50 男
雷军 48 男
马斯克 55 男
董明珠 57 女
排序后人物信息:
马云 48 男
马化腾 50 男
雷军 55 男
马斯克 57 男
董明珠 59 女
16.封装案例1
#include<iostream>
#include<string>
using namespace std;
class cube {
public:
//行为
//设置获取长宽高
void setL(int L) {
cube_L = L;
}
int getL() {
return cube_L;
}
void setW(int W) {
cube_W = W;
}
int getW() {
return cube_W;
}
void setH(int H) {
cube_H = H;
}
int getH() {
return cube_H;
}
//获取立方体面积
int getS() {
area = 2 * ((cube_H * cube_L) + (cube_H * cube_W) + (cube_L * cube_W));
return area;
}
//获取立方体体积
int getV() {
size = cube_H * cube_L * cube_W;
return size;
}
//利用成员函数判断立方体是否相等
bool isSameByClass(cube &c) {
if (cube_L == c.getL() && cube_W == c.getW() && cube_H == c.getH()) {
return true;
}
return false;
}
private:
//属性
int cube_L;
int cube_W;
int cube_H;
int area;
int size;
};
//利用全局函数判断两个立方体是否相等
bool isSame(cube& c1, cube& c2) {
if (c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH()) {
return true;
}
return false;
}
int main() {
//创建立方体对象1
cube c1;
c1.setL(10);
c1.setW(8);
c1.setH(10);
cout << "c1立方体面积是:" << c1.getS() << endl;
cout << "c1立方体体积是:" << c1.getV() << endl;
//创建立方体对象2
cube c2;
c2.setL(10);
c2.setW(8);
c2.setH(10);
cout << "c2立方体面积是:" << c2.getS() << endl;
cout << "c2立方体体积是:" << c2.getV() << endl;
//利用全局函数判断立方体是否相等
bool ret = isSame(c1,c2);
if (ret) {
cout << "c1 and c2 is same " << endl;
}
else {
cout << "c1 and c2 is different " << endl;
}
//利用成员函数判断立方体是否相等
ret = c1.isSameByClass(c2);
if (ret) {
cout << "c1 and c2 is same " << endl;
}
else {
cout << "c1 and c2 is different " << endl;
}
}
17.封装案例2
总代码实现
#include<iostream>
#include"point.h"
#include"circle.h"
using namespace std;
//点类
class point {
public:
//设置x
void setx(int a) {
x = a;
}
//获取x
int getx() {
return x;
}
//设置y
void sety(int a) {
y = a;
}
//获取y
int gety() {
return y;
}
private:
int x;
int y;
};
//圆类
class circle {
public:
//设置半径
void setR(int a) {
R = a;
}
//获取半径
int getR() {
return R;
}
//设置圆心
void setCenter(point center) {
Center = center;
}
//获取圆心
point getCenter() {
return Center;
}
private:
//半径
int R;
//在类中可以让另一个类成为本类中的成员
point Center;
};
//判断点和圆的关系
void isInCircle(circle &c,point &p) {
//计算两点之间距离的平方
int dis =
(c.getCenter().getx() - p.getx()) * (c.getCenter().getx() - p.getx()) +
(c.getCenter().gety() - p.gety()) * (c.getCenter().gety() - p.gety());
//计算半径的平方
int Rdis = c.getR() * c.getR();
//判断关系
if (dis == Rdis) {
cout << "点在圆上" << endl;
}
else if (dis > Rdis) {
cout << "点在圆外" << endl;
}
else {
cout << "点在圆内" << endl;
}
}
int main() {
//创建圆
circle c1;
c1.setR(10);
point center;
center.setx(10);
center.sety(0);
c1.setCenter(center);
//创建点
point p;
p.setx(10);
p.sety(10);
//创建关系
isInCircle(c1, p);
}
创建头文件实现
point.h(头文件)
#pragma once
#include<iostream>
using namespace std;
//点类
class point {
public:
//设置x
void setx(int a);
//获取x
int getx();
//设置y
void sety(int a);
//获取y
int gety();
private:
int x;
int y;
};
point.cpp(源文件)
#include"point.h"
//点类
//设置x
void point::setx(int a) {
x = a;
}
//获取x
int point::getx() {
return x;
}
//设置y
void point::sety(int a) {
y = a;
}
//获取y
int point::gety() {
return y;
}
circle.h(头文件)
#pragma once
#include<iostream>
#include"point.h"
using namespace std;
//圆类
class circle {
public:
//设置半径
void setR(int a);
//获取半径
int getR();
//设置圆心
void setCenter(point center);
//获取圆心
point getCenter();
private:
//半径
int R;
//在类中可以让另一个类成为本类中的成员
point Center;
};
circle.cpp(源文件)
#include"circle.h"
//圆类
//设置半径
void circle::setR(int a) {
R = a;
}
//获取半径
int circle::getR() {
return R;
}
//设置圆心
void circle::setCenter(point center) {
Center = center;
}
//获取圆心
point circle::getCenter() {
return Center;
}
主程序入口
#include<iostream>
#include"point.h"
#include"circle.h"
using namespace std;
//判断点和圆的关系
void isInCircle(circle &c,point &p) {
//计算两点之间距离的平方
int dis =
(c.getCenter().getx() - p.getx()) * (c.getCenter().getx() - p.getx()) +
(c.getCenter().gety() - p.gety()) * (c.getCenter().gety() - p.gety());
//计算半径的平方
int Rdis = c.getR() * c.getR();
//判断关系
if (dis == Rdis) {
cout << "点在圆上" << endl;
}
else if (dis > Rdis) {
cout << "点在圆外" << endl;
}
else {
cout << "点在圆内" << endl;
}
}
int main() {
//创建圆
circle c1;
c1.setR(10);
point center;
center.setx(10);
center.sety(0);
c1.setCenter(center);
//创建点
point p;
p.setx(10);
p.sety(10);
//创建关系
isInCircle(c1, p);
}
点在圆上
D:\C++代码(VS)\Project1\x64\Debug\Project1.exe (进程 20252)已退出,代码为 0。
按任意键关闭此窗口. . .