Skip to content

C++ 基础语法部分课堂代码

C++ 基础语法部分课堂代码

20200307-验证哥德巴赫猜想

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//验证哥德巴赫猜想
#include<iostream>
#include<cmath>
using namespace std;

bool isPrime(int num)
{
if (num <= 1) {
return false;
}
int cnt = 2;
int qnum = int(sqrt(num)) + 1; //空间换时间
while (cnt < qnum) {
if (num % cnt == 0) {
return false;
}
cnt++;
}
return true;
}

int main()
{
int num;
cout << "请输入一个大于2的偶数:";
cin >> num;
if (num <= 2 || num % 2 != 0) {
cout << "输入有误" << endl;
return 0;
}
for (int cnt = 2; cnt < num/2; cnt++) {
if (!isPrime(cnt)) {
continue;
}
if (isPrime(num - cnt)) {
cout << num << "可以分解为" << cnt << "和" << num - cnt << endl;
break;
}
}
return 0;
}

20200307-验证角谷猜想

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//角谷猜想
#include<iostream>
using namespace std;

int main()
{
int num = 0, cnt = 0;
cout << "请输入一个正整数:";
cin >> num;
while (num != 1) {
cnt++;
int last = num;
switch (num % 2) {
case 0: //偶数
num /= 2;
cout << last << "/2=" << num << endl;
break;
case 1: //奇数
num = 3 * last + 1;
cout << "3*" << last << "+1=" << num << endl;
break;
}
}
cout << "共运算了" << cnt << "步" << endl;
return 0;
}

20200309-猜数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//猜数字
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

int main()
{
//产生随机数
srand((unsigned)time(NULL));
int num = rand() % 50 + 50;
int guess, count = 0;
//cout << num << endl;
do {
cout << "猜一个1到10000的数字:";
cin >> guess;
count++;
if (guess > num) {
cout << "您猜的数字偏大,再试一次吧!" << endl;
}
else if (guess < num) {
cout << "您猜的数字偏小,再试一次吧!" << endl;
}
}while (guess != num);
cout << "您猜对了!正确的数字是" << num << ",您一共猜了" << count << "次。" << endl;
return 0;
}

20200309-常胜将军

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//常胜将军
#include<iostream>
using namespace std;

int main()
{
int coins = 21;
do {
int PersonGet, ComputerGet;
while (1) {
cout << "现在有" << coins << "枚硬币。";
cout << "请输入你要取的硬币的数量(1-4枚):";
cin >> PersonGet;
if (PersonGet >= 1 && PersonGet <= 4 && PersonGet <= coins)
break;
cout << "你取走的数量是不合法的,请重新输入。" << endl;
}
ComputerGet = 5 - PersonGet;
coins -= ComputerGet+ PersonGet;
cout << "电脑取走了" << ComputerGet << "枚硬币。" << endl;
} while (coins != 1);
cout << "你输了!你只能取走这一枚硬币。" << endl;
return 0;
}

20200309-输出所有的勾股数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//输出所有的勾股数
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
int num1, num2, num3;
for (num1 = 1; num1 <= 100; num1++) {
for (num2 = num1 + 1; num2 <= 100; num2++) {
num3 = sqrt(num1 * num1 + num2 * num2);
if (num1 * num1 + num2 * num2 == num3 * num3) {
cout << num1 << ' ' << num2 << ' ' << num3 << endl;
}
}
}
return 0;
}

20200309-36块砖头36人搬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//36块砖头36人搬, 男人:4块, 女人:2块, 小孩:2人搬1块
#include<iostream>
using namespace std;

int main()
{
int male, female, child;
for (male = 0; male <= 9; male++) {
for (female = 0; female <= 18; female++) {
child = 36 - male - female;
if (4 * male + 2 * female + child*0.5 ==36) {
cout << male << "男人," << female << "女人," << child << "小孩。" << endl;
}
}
}
return 0;
}

20200310-数组初始化

1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;

int main() {
int array1[10] = { 0 };
for (int i = 0; i <= 30000; i++) {
cout << array1[i] << '\t';
}
return 0;
}

20200312-输出30个不重复的1~50的随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//输出30个不重复的1~50的随机数
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
int nums[30] = { 0 };
int flags[50] = { 0 };
int i = 1;
nums[0] = rand() % 50 + 1;
flags[nums[0]-1] = 1;
while (i < 30) {
//srand((unsigned)time(NULL));
int temp = rand() % 50 + 1;
if (flags[temp-1] == 0) {
nums[i] = temp;
i++;
}
}
for (int j = 0; j < 30; j++) {
cout << nums[j] << '\t';
}
return 0;
}

20200312-字符数组拷贝初探

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//字符数组拷贝初探
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
char buff1[10] = "hello";
char buff2[10];
int i = 0;
while (buff2[i] = buff1[i++]);
cout << buff2;
return 0;
}

20200316-函数1:输出所有的水仙花数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//输出所有的水仙花数
#include<iostream>
#include<cmath>
using namespace std;

//function declare
bool isNarcissistic(int num);

int main() {
for (int i = 100; i < 1000; ++i) {
if (isNarcissistic(i)) {
cout << i << ' ';
}
}
cout << endl;
return 0;
}

//function define
/*===============================
本函数用于判断一个正整数是否是水仙花数
参数说明:
int num:待判断的正整数
返回值说明:
true-是水仙花数;false-不是水仙花数
===============================*/
bool isNarcissistic(int num) {
if (num >= 1000 || num < 100) {
return false;
}
int sum = 0;
sum += (int)pow(num / 100, 3);
sum += (int)pow(num % 100 / 10, 3);
sum += (int)pow(num % 10, 3);
return (sum == num);
}

20200316-函数2:值传递与数组作为函数参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//值传递与数组作为函数参数
#include<iostream>
using namespace std;

//值传递:单向传递
void exchange(int num1, int num2);
//数组作为函数参数(数组名即地址,按地址传递):双向传递
void change(int nums[], int size);

int main() {
/*
int m = 3, n = 5;
cout << m << ' ' << n << endl;
exchange(m, n);
cout << m << ' ' << n << endl;
*/

int arr1[5] = { 0 };
change(arr1, 5);
for (int i = 0; i <= 4; i++) {
cout << arr1[i] << ' ';
}
cout << endl;
return 0;
}

void change(int nums[], int size) {
for (int i = 0; i < size; i++) {
nums[i] = i;
}
}

void exchange(int num1, int num2) {
int temp = num1;
num1 = num2;
num2 = temp;
cout << num1 << " " << num2 << endl;
}

20200319-函数默认值参数,指针的sizeof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

//函数默认值参数,指针的sizeof
#include <cstdlib>
#include <iostream>
using namespace std;

void delay(int x = 1000)
{
for (int i = 0; i < x; i++) {
for (int i = 0; i < x; i++);
}

}

int main()
{
// for (int i = 0; i < 1000; i++) {
// cout << '\r' << rand() % 9000 + 1000;
// delay(1000);
// }
int num = 0;
char ch = 'a';
cout << sizeof(num) << endl;
cout << sizeof(ch) << endl;
int* p1;
char* p2;
cout << sizeof(p1) << endl;
cout << sizeof(p2) << endl;

return 0;
}

20200321-指针与数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//2020-3-21 10:49:29
//指针与数组
#include<iostream>
//#include<cstring>
using namespace std;

#define SIZE 10
int myStrlen(char buff[]);

int main()
{
int array1[SIZE];
//常规初始化
for (int i = 0; i < SIZE; i++) {
array1[i] = i;
}
for (int i = 0; i < SIZE; i++) {
cout << i[array1] << '\t'; //这样也是可以的 相当于i+array1
}
cout << endl;

//使用指针
int* p;
//cout << *p; 指针不知道指向哪里,不要去它的内容
p = array1; //数组名即首元素地址

//如果一个指针指向了数组的首地址,该指针就可以当数组名用
for (int i = 0; i < SIZE; i++) {
cout << p[i] << '\t'; //指针名当数组名用
}
cout << endl;

//指针+-常量=指针;指针-指针=距离
//版本1
for (int i = 0; i < SIZE; i++) {
*(p+i) = i;
}
for (int i = 0; i < SIZE; i++) {
cout << *(p + i) << '\t';
}
cout << endl;
//版本2
for (int i = 0; i < SIZE; i++) {
*p = i;
p++;
}
p = array1; //指针回到首地址,防止指针越界
for (int i = 0; i < SIZE; i++) {
cout << *p << '\t';
p++;
}
cout << endl;

//char buff1[128] = "hello";
//strcpy(buff1, buff1 + 1); 删掉buff1的第一个字符

return 0;
}

//用指针计算字符串长度
int myStrlen(char buff[])
{
char* p = buff;
while (*p) p++;
return p - buff;
}

20200323-动态分配内存与指针

1
2
3
4
5
6
7
8
9
10
11
12
13
//动态分配内存与指针
#include<iostream>
using namespace std;

int main()
{
int count;
cin >> count;
int* p = new int[count]; //动态分配内存,p轻易不要修改
delete p; //new出来内存的一定要delete(程序退出时也会回收)
//否则程序运行时间长后,程序会有bug
return 0;
}

20200323-指针作为函数参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//指针作为函数参数
#include<iostream>
using namespace std;

void exchange1(int, int);
void exchange2(int*, int*);
void exchange3(int*, int*);
void exchange4(int*, int*);
void exchange5(int&, int&);

int main()
{
int m = 3, n = 5;
cout << m << " " << n << endl;
//exchange1(m, n);
//exchange2(&m, &n);
//exchange3(&m, &n);
//exchange4(&m, &n); 报错
exchange5(m, n);
cout << m << " " << n << endl;
return 0;
}

//按值传递:单向传递
void exchange1(int num1, int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
cout << num1 << " " << num2 << endl;
}
//按地址传递:双向传递
void exchange2(int* pnum1, int* pnum2)
{
int temp = *pnum1;
*pnum1 = *pnum2;
*pnum2 = temp;
cout << *pnum1 << " " << *pnum2 << endl;
}
//!指针之间的赋值:单向传递(不是用了指针就一定能双相传递)
void exchange3(int* pnum1, int* pnum2)
{
int *ptemp = pnum1;
pnum1 = pnum2; //这里的几条语句仅仅使得pnum1和pnum2交换了指向的变量
pnum2 = ptemp; //并没有真正的修改了两者指向的数据
cout << *pnum1 << " " << *pnum2 << endl;
}
//不要使用未初始化的指针
void exchange4(int* pnum1, int* pnum2)
{
int* ptemp;
*ptemp = *pnum1; //ptemp指针没有指向的变量->错误 使用了未初始化的局部变量“ptemp”
//正确做法:int temp; int* ptemp = &temp; *ptemp = *pnum1 实际上和exchange2一致
*pnum1 = *pnum2;
*pnum2 = *ptemp;
cout << *pnum1 << " " << *pnum2 << endl;
}
//引用传递:双向传递(比指针高效)
void exchange5(int& num1, int& num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
cout << num1 << " " << num2 << endl;
}


//用指针处理数组
char* myStrcpy(char* des, char* src)
{
char* p = des;
char* q = src;
while (*p++ = *q++);
return des;
}
char* revert(char* src)
{
char* p = src;
char* q = src;
while (*q != '\0') q++;
q--;
while (p < q) {
char temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
return src;
}

20200326-二维指针:指向指针的指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//二维指针:指向指针的指针
#include<iostream>
using namespace std;

int main02()
{
char* months[12] = { //指针数组,12个字符数组不必连续,但多了指针数组的空间
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
int num;
cout << "请输入一个数字:"; cin >> num;
if (num >= 1 && num <= 12) {
cout << months[num - 1] << endl;
}
else {
cout << "输入错误" << endl;
}
return 0;
}

int main01()
{
char months[12][16] = { //是连续的,内存浪费:12*16
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
int num;
cout << "请输入一个数字:"; cin >> num;
if (num >= 1 && num <= 12) {
cout << months[num - 1] << endl;
}
else {
cout << "输入错误" << endl;
}
return 0;
}

20200326-冒泡排序和函数指针(不要求)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//冒泡排序:对字符串排序
#include<iostream>
#include<cstring>
using namespace std;

void mySort(char**, int, int(*)(const char*, const char*) = &strcmp);
int lengthcmp(const char*, const char*);

int main()
{
char* months[12] = { //待排序指针数组
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};

mySort(months, 12); //默认字典序排序
for (int i = 0; i < 12; i++) {
cout.width(16); cout << left;
cout << months[i];
if ((i + 1) % 6 == 0) {
cout << endl;
}
}
cout << endl;

mySort(months, 12, &lengthcmp); //根据字符串的长度排序
for (int i = 0; i < 12; i++) {
cout.width(16); cout << left;
cout << months[i];
if ((i + 1) % 6 == 0) {
cout << endl;
}
}
cout << endl;
return 0;
}

//根据字符串的长度来比较大小
int lengthcmp(const char* str1, const char* str2)
{
return (strlen(str1) - strlen(str2));
}
//利用 函数指针 和 指向指针的指针 排序
void mySort(char** arr, int size, int(*op)(const char* ,const char*))
{
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (op(arr[j], arr[j + 1]) > 0) {
//swap
char* temp;
temp = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = temp;
}
}
}
}

应当掌握的方法:

20200326

20200330-函数重载

1
2
3
4
5
6
7
8
9
10
11
12
int myabs(int num)
{
return ((num > 0) ? num : -num);
}
double myabs(double num)
{
return ((num > 0) ? num : -num);
}
long myabs(long num)
{
return ((num > 0) ? num : -num);
}

20200330-函数模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//用模板写一个通用的可以对任意个元素的数值类型的数组排序的函数
#include<iostream>
using namespace std;

/**
对任意个元素的数值类型的数组原地排序(选择排序)
模板参数说明:
T: 数值类型的数据类型,例如int、long、double等等
形参说明:
arr: 数组首地址
size: 数组大小
reverse: 是否从大到小排序,默认从小到大(false)
返回值说明:
排序后该数组的首地址
*/
template <typename T>
T* sort(T* const arr, int size, bool reverse = false)
{
for (T* forwardPtr = arr; forwardPtr < arr+size-1; forwardPtr++) {
T* assume = forwardPtr; //假设forePtr指向元素最大/小
for (T* backPtr = forwardPtr+1; backPtr < arr+size; backPtr++) {
//找出指向未排序区域的最大/小元素的指针,用assume保存
if (reverse ? (*backPtr > *assume) : (*backPtr < *assume)) {
assume = backPtr;
}
}
if (assume != forwardPtr) {
//最大/小不是forePtr指向的元素,交换两指针指向的内容
T temp = *assume;
*assume = *forwardPtr;
*forwardPtr = temp;
}
}
return arr;
}

/**
输出任意个元素的数值类型的数组
模板参数说明:
T: 数值类型的数据类型,例如int、long、double等等
形参说明:
arr: 数组首地址
size: 数组大小
返回值说明:
无返回值
*/
template <typename T>
void show(const T* arr, int size)
{
const T* end = arr + size;
for (; arr < end; arr++) {
cout << *arr << '\t';
}
cout << endl;
}

int main()
{
int arr[5] = { 10,4,9,2,1 }; //int数组
show(sort(arr, 5), 5);
show(sort(arr, 5, true), 5);
double darr[7] = { 10.3,-4.4,8.9,0.2,10.0,5.5,-6.7 }; //double数组
show(sort(darr, 7), 7);
show(sort(darr, 7, true), 7);
char carr[8] = { 'c','f','q','z','.','E','B','(' }; //char数组
show(sort(carr, 8), 7);
show(sort(carr, 8, true), 8);
return 0;
}

20200402-文件流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//文件流
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
ofstream ofile("d:\\input.txt");
ifstream inFile("d:\\output.txt"); //物理路径用\\, 虚拟路径用/
if (!inFile) {
cout << "打开文件失败";
return 0;
}
char ch;
while (!inFile.eof()) {
ch = inFile.get();
ch = ch ^ 0x80; //简单的加密:a^b=c, c^b=a
ofile << ch;
}
inFile.close();
ofile.close();
return 0;
}

20200402-二进制文件流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//用二进制方式打开文件,进行读写加密
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
ifstream inFile("d:\\input.txt", ios::binary | ios::in);
if (!inFile) {
cout << "File Not Found!" << endl;
return 0;
}
ofstream outFile("d:\\output.txt", ios::binary | ios::out);
char ch;
while (!inFile.eof()) {
inFile.read(&ch, sizeof(ch));
ch ^= 0x80;
outFile.write(&ch, sizeof(ch));
}
inFile.close();
outFile.close();
return 0;
}

20200413-静态局部变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;

void test()
{
static int num = 0;
num++;
cout << num << endl;
}

int main()
{
for (int i = 0; i <= 2; i++)
test();
return 0;
}

About this Post

This post is written by Holger, licensed under CC BY-NC 4.0.

#考试不挂科 #C++ #课堂 #学习笔记