Skip to content

Commit b0d036a

Browse files
committed
21-03-31 backup
1 parent ef37dff commit b0d036a

File tree

202 files changed

+54550
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

202 files changed

+54550
-0
lines changed

cpp程序设计/12.sort.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,11 @@ int main() {
8282
cout << arr2[i] << " ";
8383
}
8484
cout << endl;
85+
86+
haizei::sort(arr2, 0, 4, [](int a, int b) -> bool {return a < b;});
87+
for (int i = 0; i < 5; i++) {
88+
cout << arr2[i] << " ";
89+
}
90+
cout << endl;
8591
return 0;
8692
}

cpp程序设计/a.out

34.2 KB
Binary file not shown.

c语言课程/10.11_main.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*************************************************************************
2+
> File Name: 10.11_main.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年10月11日 星期日 18时39分12秒
6+
************************************************************************/
7+
8+
#include <stdio.h>
9+
#include <string.h>
10+
#include <stdlib.h>
11+
#define ppchar char *
12+
typedef char * pchar;
13+
14+
void output(int argc, char *argv[], char **env) {
15+
printf("argc = %d\n", argc);
16+
for (int i = 0; i < argc; ++i) {
17+
printf("%s\n", argv[i]);
18+
}
19+
for (int i = 0; env[i]; ++i) {
20+
if (!strncmp(env[i], "USER=", 5)) {
21+
if (!strcmp(env[i] + 5, "cgc")) {
22+
printf("welcome cui\n");
23+
} else {
24+
printf("you are not the user! please gun!\n");
25+
exit(0);
26+
}
27+
}
28+
}
29+
return ;
30+
}
31+
32+
int main(int argc, char *argv[], char **env) {
33+
pchar p1, p2;
34+
ppchar p3, p4;//简单的代码替换会出bug
35+
printf("p1 = %lu, p2 = %lu\n", sizeof(p1), sizeof(p2));
36+
printf("p3 = %lu, p4 = %lu\n", sizeof(p3), sizeof(p4));
37+
output(argc, argv, env);
38+
return 0;
39+
}

c语言课程/10.11_point.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*************************************************************************
2+
> File Name: 10.11_point.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年10月11日 星期日 15时25分39秒
6+
************************************************************************/
7+
8+
#include <stdio.h>
9+
#define P(a) {\
10+
printf("%s = %d\n", #a, a);\
11+
}
12+
13+
struct Data {
14+
int x, y;
15+
};
16+
17+
int main() {
18+
struct Data a[2], *p = a;
19+
a[0].x = 1, a[0].y = 1;
20+
a[1].x = 2, a[1].y = 2;
21+
P(a[1].x);
22+
P((a + 1) -> x);
23+
P((p + 1) -> x);
24+
P(p[1].x);
25+
P((*(p + 1)).x);
26+
P((*(a + 1)).x);
27+
P((&a[1])->x);
28+
P((&p[1])->x);
29+
P((&a[0] + 1)->x);
30+
P((*(&a[0] + 1)).x);
31+
P(*((int *)p + 2));
32+
return 0;
33+
}

c语言课程/10.11_pointer.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*************************************************************************
2+
> File Name: 10.11_pointer.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年10月11日 星期日 16时34分18秒
6+
************************************************************************/
7+
//类型作为参数进行传递用宏
8+
#include <stdio.h>
9+
/*#define offset(T, a) ({\
10+
T temp;\
11+
(char *)&temp.a - (char *)&temp;\
12+
})**/
13+
#define offset(T, a) (long)(&(((T*)(NULL))->a))
14+
15+
struct Data {
16+
int a;
17+
double b;
18+
char c;
19+
};
20+
21+
int main() {
22+
int num = 0x616263;
23+
printf("%s\n", (char *)(&num));
24+
int num1 = 0x616263;
25+
int num2 = 0x61626364;
26+
//printf("%s\n", (char *)(&num2));
27+
printf("%s\n", (char *)(&num2 + 1));
28+
printf("%ld\n", offset(struct Data, a));
29+
printf("%ld\n", offset(struct Data, b));
30+
printf("%ld\n", offset(struct Data, c));
31+
return 0;
32+
}

c语言课程/10.9_ip.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*************************************************************************
2+
> File Name: 10.9_ip.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年10月09日 星期五 20时55分20秒
6+
************************************************************************/
7+
//Ip 地址转整数
8+
#include <stdio.h>
9+
10+
union IP {
11+
struct {
12+
unsigned char a1;
13+
unsigned char a2;
14+
unsigned char a3;
15+
unsigned char a4;
16+
} ip;
17+
unsigned int num;
18+
};
19+
20+
int is_little() {
21+
static int num = 1;
22+
return ((char *)(&num))[0];
23+
}
24+
25+
int main() {
26+
printf("%d\n", is_little());
27+
union IP p;
28+
char str[100];
29+
int arr[4];
30+
while (~scanf("%s", str)) {
31+
sscanf(str, "%d.%d.%d.%d", arr + 0, arr + 1, arr + 2, arr + 3);
32+
p.ip.a1 = arr[3];
33+
p.ip.a2 = arr[2];
34+
p.ip.a3 = arr[1];
35+
p.ip.a4 = arr[0];
36+
printf("%u\n", p.num);
37+
}
38+
//printf("%u\n", p.num);
39+
return 0;
40+
}

c语言课程/10.9_log.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*************************************************************************
2+
> File Name: 10.9_log.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年10月09日 星期五 18时15分56秒
6+
************************************************************************/
7+
//变参宏, argc相当于...的别名, #将参数字符串化,##连接,确保参数为空的时候不出问题
8+
//g++ -DEBUG .c 可调用log宏
9+
#include <stdio.h>
10+
//#ifdef DEBUG
11+
#define log(frm, argc...) {\
12+
printf("[%s : %d] ", __func__, __LINE__);\
13+
printf(frm, ##argc);\
14+
printf("\n");\
15+
}
16+
//#else
17+
//#define log(frm, argc...)
18+
//#endif
19+
20+
#define contact(a, b) a##b
21+
22+
void func(int a) {
23+
a += 1;
24+
log("%d", a);
25+
return;
26+
}
27+
28+
int main() {
29+
int a = 123, abcdef = 0;
30+
//printf("[%s : %d]%d\n", __func__, __LINE__, a);
31+
func(a);
32+
log("%d %d", a, abcdef);
33+
printf("hello world\n");
34+
log("hello workd");
35+
contact(abc, def) = 24;
36+
log("%d", abcdef);
37+
return 0;
38+
}

c语言课程/10.9_string_10to16.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*************************************************************************
2+
> File Name: 10.9_string_10to16.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年10月09日 星期五 20时03分55秒
6+
************************************************************************/
7+
//使用字符串相关操作方法,计算一个整型16进制表示的位数。
8+
#include <stdio.h>
9+
#include <string.h>
10+
11+
int main() {
12+
int n;
13+
char str[12];
14+
while (~scanf("%d", &n)); {
15+
sprintf(str,"%X", n);
16+
printf("%s has %lu digits!\n", str, strlen(str));
17+
}
18+
return 0;
19+
}

c语言课程/9.23func.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*************************************************************************
2+
> File Name: 9.23func.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年09月23日 星期三 20时34分32秒
6+
************************************************************************/
7+
//函数声明与函数定义
8+
#include <stdio.h>
9+
10+
/*int funcB(int n) {
11+
if (n == 0) return 0;
12+
printf("funcB : %d\n", n);
13+
return 0;
14+
}*/
15+
16+
int funcB(int n);
17+
18+
int funcA(int n) {
19+
if (n==0) return 0;
20+
printf("funcA : %d\n", n);
21+
funcB(n--);
22+
return 0;
23+
}
24+
25+
int main() {
26+
funcA(5);
27+
return 0;
28+
}

c语言课程/9.25_my_printf.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*************************************************************************
2+
> File Name: 9.25_my_printf.c
3+
> Author: Cui Guochong
4+
5+
> Created Time: 2020年09月25日 星期五 18时13分41秒
6+
************************************************************************/
7+
//putchar();向屏幕打印一个字符
8+
#include <stdio.h>
9+
#include <stdarg.h>
10+
#include <inttypes.h>
11+
12+
int reverse_num(int num, int *temp) {
13+
int digit = 0;
14+
do {
15+
*temp = *temp * 10 + num % 10;
16+
num /= 10;
17+
++digit;
18+
} while (num);
19+
return digit;
20+
}
21+
22+
int output_num(int num, int digit) {
23+
int cnt = 0;
24+
while (digit--) {
25+
putchar(num % 10 + '0');
26+
num /= 10;
27+
cnt++;
28+
}
29+
return cnt;
30+
}
31+
32+
int my_printf(const char *frm, ...) {
33+
//字符串字面量不可被修改,所以用const, char *const frm:指针常量
34+
va_list arg;
35+
va_start(arg, frm);
36+
int cnt = 0;
37+
#define PUTC(a) putchar(a), ++cnt//局部宏,工程中很少全局宏
38+
for (int i = 0; frm[i]; ++i) {
39+
switch (frm[i]) {
40+
case '%': {
41+
switch (frm[++i]) {
42+
case '%': {
43+
PUTC(frm[i]);
44+
} break;
45+
case 'd': {
46+
int xx = va_arg(arg, int);//取int类型的值
47+
uint32_t x;
48+
if (xx < 0) x = -xx, PUTC('-');//输出负数
49+
else x = xx;
50+
int num1 = x / 100000, num2 = x % 100000;
51+
int temp1 = 0, temp2 = 0;
52+
int digit1 = reverse_num(num1, &temp1);
53+
int digit2 = reverse_num(num2, &temp2);
54+
if (num1) digit2 = 5;
55+
else digit1 = 0;
56+
cnt += output_num(temp1, digit1);
57+
cnt += output_num(temp2, digit2);
58+
} break;
59+
case 's': {
60+
const char *str = va_arg(arg, const char*);
61+
for (int i = 0; str[i]; i++) {
62+
PUTC(str[i]);
63+
}
64+
} break;
65+
}
66+
} break;
67+
default: PUTC(frm[i]); break;
68+
}
69+
}
70+
#undef PUTC
71+
va_end(arg);
72+
return cnt;
73+
}
74+
75+
int main() {
76+
int a = 123;
77+
printf("hello world\n");
78+
my_printf("hello world\n");
79+
printf("int a = %d\n",a);
80+
my_printf("int a = %d\n",a);
81+
printf("int a = %d\n",1000);
82+
my_printf("int a = %d\n",1000);
83+
printf("int a = %d\n",0);
84+
my_printf("int a = %d\n",0);
85+
printf("int a = %d\n",-123);
86+
my_printf("int a = %d\n",-123);
87+
printf("int a = %d\n",INT32_MAX);
88+
my_printf("int a = %d\n",INT32_MAX);
89+
printf("int a = %d\n",INT32_MIN);
90+
my_printf("int a = %d\n",INT32_MIN);
91+
char *str = "I love china\n";
92+
printf("%s", str);
93+
my_printf("%s", str);
94+
int n;
95+
while (~scanf("%d", &n)) {
96+
printf(" has %d digits!\n", printf("%d", n));
97+
my_printf(" has %d digits!\n", my_printf("%d", n));
98+
}
99+
return 0;
100+
}

0 commit comments

Comments
 (0)