mirror of
https://github.com/PKUFlyingPig/cs-self-learning.git
synced 2026-06-23 09:58:12 +08:00
Pending changes exported from your codespace
This commit is contained in:
parent
441c308b07
commit
6673f06d6b
28 changed files with 376 additions and 0 deletions
14
Cprogramming/2dim_array.c
Normal file
14
Cprogramming/2dim_array.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
//in c programming language, 2dim arrary is in row
|
||||
// ini
|
||||
//int a[5][3] = {{},{},{}} 分段赋值
|
||||
//int a[5][3] = {} 按行连续赋值
|
||||
//如果对全部元素赋值,第一维的长度可以不给出 如下
|
||||
int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
int b[][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/2dim_array_pointer
Executable file
BIN
Cprogramming/2dim_array_pointer
Executable file
Binary file not shown.
16
Cprogramming/2dim_array_pointer.c
Normal file
16
Cprogramming/2dim_array_pointer.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int a[3][4] = {{0,1,2,3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
|
||||
int(*p)[4] = a;
|
||||
//一个指向数组的指针p,数组的类型为int[4]
|
||||
printf("%d\n", sizeof(*(p+1))); // 16 that is because 4 * 4 = 16
|
||||
|
||||
/**
|
||||
* *(p+1)单独使用表示第一行数据,放在表达式里就代表第一行数据的首地址
|
||||
* *(p+1)+1 代表第一行数据的第1个元素的地址
|
||||
* *(*(p+1)+1) 代表第一行数据的第一个元素
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/a.out
Executable file
BIN
Cprogramming/a.out
Executable file
Binary file not shown.
13
Cprogramming/array_ini.c
Normal file
13
Cprogramming/array_ini.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
int a[4] = {20, 1, 2, 3}; // directly
|
||||
int a[10] = {1, 2, 3, 4, 5}; //partly
|
||||
|
||||
int a[] = {1, 3, 4, 5, 6}; // == int a[5] = {1, 3, 4, 5, 6}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/ball
Executable file
BIN
Cprogramming/ball
Executable file
Binary file not shown.
BIN
Cprogramming/ballGame
Executable file
BIN
Cprogramming/ballGame
Executable file
Binary file not shown.
96
Cprogramming/ballGame.c
Normal file
96
Cprogramming/ballGame.c
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* 1. 尝试不用usleep函数 利用循环语句使小球变慢
|
||||
* 2. 尝试用printf("\a") 实现触边界响铃效果
|
||||
* 3. 尝试绘制边框
|
||||
*
|
||||
* 参考答案链接:https://blog.csdn.net/weixin_72939806/article/details/129221682
|
||||
*/
|
||||
|
||||
int main() {
|
||||
|
||||
|
||||
// 静止小球的位置
|
||||
int i, j;
|
||||
int x = 2; //坐标
|
||||
int y = 2;
|
||||
|
||||
int velocity_x = 1;
|
||||
int velocity_y = 1;
|
||||
int left = 0;
|
||||
int right = 20;
|
||||
int top = 0;
|
||||
int bottom = 10;
|
||||
//长10 宽20
|
||||
|
||||
|
||||
|
||||
//int height = 20;
|
||||
//int velocity = 1;
|
||||
// printf "\033c" 清屏
|
||||
// 小球下落: 让小球的x坐标增加
|
||||
// 上下弹跳的小球:小球新位置 = 旧位置x + 速度velocity,当判断小球到达上下边界时改变方向,即改变velocity符号
|
||||
// 斜着弹跳的小球:增加x,y两个方向的速度
|
||||
|
||||
|
||||
while(1){
|
||||
x+=velocity_x;
|
||||
y+=velocity_y;
|
||||
|
||||
system("clear");
|
||||
|
||||
for(j=0;j<=right;j++) printf("_"); printf("\n");
|
||||
for(i=1;i<bottom;i++){
|
||||
printf("|");
|
||||
for(j=0;j<right-1;j++) printf(" ");
|
||||
printf("|\n");
|
||||
}
|
||||
for(j=0;j<=right;j++) printf("=");
|
||||
|
||||
printf("\033[0;0H");
|
||||
|
||||
for(i=0;i<x;i++)
|
||||
printf("\n");
|
||||
for(j=0;j<y;j++){
|
||||
if(j==0)printf("|");
|
||||
else printf(" ");
|
||||
}
|
||||
printf("o");
|
||||
printf("\n");
|
||||
usleep(250000);
|
||||
|
||||
if(x==top+1||x==bottom-1){
|
||||
velocity_x=-velocity_x;
|
||||
printf("\a");
|
||||
}
|
||||
if(y==left+1||y==right-1){
|
||||
velocity_y=-velocity_y;
|
||||
printf("\a");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// for(x=1; x < 10; x++) {
|
||||
// system("clear");
|
||||
|
||||
// for (i=0; i < x; i++) {
|
||||
// printf("\n");
|
||||
// }
|
||||
|
||||
// for (j = 0; j < y; j++) {
|
||||
// printf(" ");
|
||||
// }
|
||||
|
||||
// printf("o");
|
||||
// printf("\n");
|
||||
// //sleep(1);
|
||||
// usleep(200000); // 500毫秒 * 1000 微秒/毫秒 = 500000 微秒
|
||||
// }
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
Cprogramming/char_array.c
Normal file
15
Cprogramming/char_array.c
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
//字符数组实际上就是一系列字符的集合也就是字符串
|
||||
char str[30] = {"asdasdasdasdasdasd"};
|
||||
char str[20] = "asdasdasasfs";
|
||||
|
||||
char str[] = "fefegfef";
|
||||
|
||||
//字符数组只有在定义的时候可以一整个字符串一次性赋值给他,一旦定义完了,就只能一个字符一个字符地赋值了
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/end_of_string
Executable file
BIN
Cprogramming/end_of_string
Executable file
Binary file not shown.
17
Cprogramming/end_of_string.c
Normal file
17
Cprogramming/end_of_string.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <stdio.h>
|
||||
//局部变量的初始值不一定全是0
|
||||
|
||||
int main() {
|
||||
char str[30];
|
||||
char c;
|
||||
int i;
|
||||
for (c = 65, i = 0; c <= 90; c++, i++) {
|
||||
str[i] = c;
|
||||
}
|
||||
str[i] = 0; //也可以写作str[i] = '\0'手动结束字符串
|
||||
//更专业的做法是 将str初始就全为0 str = {0};
|
||||
|
||||
|
||||
printf("%s\n", str);
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/func_pointer
Executable file
BIN
Cprogramming/func_pointer
Executable file
Binary file not shown.
20
Cprogramming/func_pointer.c
Normal file
20
Cprogramming/func_pointer.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int max(int a, int b) {
|
||||
return a>b ? a : b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int x, y, maxval;
|
||||
|
||||
int (*pmax)(int, int) = max;
|
||||
//函数指针
|
||||
|
||||
printf("input two numbers:");
|
||||
scanf("%d %d", &x, &y);
|
||||
maxval = (*pmax)(x,y);
|
||||
printf("Max value: %d\n", maxval);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
Cprogramming/gets_scanf.c
Normal file
10
Cprogramming/gets_scanf.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
//gets 可以接受带空格的字符串
|
||||
//scanf 不行,but 不是完全不行
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
Cprogramming/hello_world.c
Normal file
8
Cprogramming/hello_world.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
printf("Hello world");
|
||||
|
||||
return 0;
|
||||
}
|
||||
19
Cprogramming/last_skill_pointer.c
Normal file
19
Cprogramming/last_skill_pointer.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
/**
|
||||
* int *p1[6]; 指针数组
|
||||
* int *(p2[6]); 指针数组
|
||||
* int (*p3)[6]; 二维数组指针
|
||||
* int (*p4)(int, int); 函数指针
|
||||
*
|
||||
*/
|
||||
|
||||
//c语言标准规定,对于一个符号的定义,编译器总是从**它的名字**开始读,然后按照优先级顺序依次解析
|
||||
//优先级顺序: 定义中被括号()扩起的,
|
||||
// 后缀操作:()表示这是一个函数,[] 表示这是一个数组
|
||||
// 前缀操作: * 表示指向xxx的指针
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/main_advanced
Executable file
BIN
Cprogramming/main_advanced
Executable file
Binary file not shown.
14
Cprogramming/main_advanced.c
Normal file
14
Cprogramming/main_advanced.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
//argc表示传递的字符串的数目,argv是一个指针数组,每一个指针指向一个字符串
|
||||
|
||||
int i;
|
||||
printf("The program receives %d parameters:\n", argc);
|
||||
for (i=0; i<argc; i++) {
|
||||
printf("%s\n", argv[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
10
Cprogramming/memory_leak.c
Normal file
10
Cprogramming/memory_leak.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
while(1) {
|
||||
malloc(1024); // allocate 1024bytes
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/non-blockingListen
Executable file
BIN
Cprogramming/non-blockingListen
Executable file
Binary file not shown.
0
Cprogramming/non-blockingListen.c
Normal file
0
Cprogramming/non-blockingListen.c
Normal file
BIN
Cprogramming/passw
Executable file
BIN
Cprogramming/passw
Executable file
Binary file not shown.
48
Cprogramming/passw.c
Normal file
48
Cprogramming/passw.c
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#define PWDLEN 20
|
||||
|
||||
void getpwd(char *pwd, int pwdlen);
|
||||
|
||||
int main()
|
||||
{
|
||||
char pwd[PWDLEN + 1];
|
||||
printf("Input password: ");
|
||||
getpwd(pwd, PWDLEN);
|
||||
system("stty echo");
|
||||
printf("The password is: %s\n", pwd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* get the password by users
|
||||
* @param pwd char* 保存密码的内存的首地址
|
||||
* @param pwdlen int 密码的最大长度
|
||||
*/
|
||||
|
||||
void getpwd(char *pwd, int pwdlen) {
|
||||
char ch = 0;
|
||||
int i = 0;
|
||||
system("stty -echo");//取消回显
|
||||
while (i < pwdlen) { //循环输入密码
|
||||
ch = getchar();
|
||||
if (ch == '\n') { //unix敲入回车结束
|
||||
//printf("\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (ch == '\b' && i > 0) {//删除键回退之后覆盖
|
||||
i--;
|
||||
//printf("\b \b");
|
||||
} else if (isprint(ch)) {
|
||||
pwd[i] = ch;
|
||||
//printf("*");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
pwd[i] = 0; //最后结束
|
||||
printf("\n");
|
||||
//system("stty echo");
|
||||
}
|
||||
29
Cprogramming/pointer.c
Normal file
29
Cprogramming/pointer.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/**
|
||||
* Notes on Pointer Full
|
||||
* 1. 定义指针变量:
|
||||
* datatype *name 指针变量所指向的数据类型 指针变量的名字
|
||||
*
|
||||
* 2.关于*和&的谜题:
|
||||
* int a; pa是指向它的指针
|
||||
* 2.1 *&a是什么意思?
|
||||
* attempt:*(&a) == a
|
||||
* 2.2 &*pa是什么意思?
|
||||
* attempt: == pa
|
||||
*
|
||||
* 3.数组指针
|
||||
* 3.1 *p++ = *(p++)
|
||||
* 3.2 *++p = *(++p)
|
||||
*
|
||||
*
|
||||
* 4.数组和指针不等价
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Cprogramming/problem_pointer
Executable file
BIN
Cprogramming/problem_pointer
Executable file
Binary file not shown.
46
Cprogramming/problem_pointer.c
Normal file
46
Cprogramming/problem_pointer.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
char *lines[5]= {
|
||||
"COSC1283/1284",
|
||||
"Programming",
|
||||
"Techniques",
|
||||
"is",
|
||||
"great fun"
|
||||
};
|
||||
/**
|
||||
* 实际上上述的字符串数组等同于:
|
||||
* char *string0 = "C0SC1283/1284"
|
||||
* char *string1 = "Programming"
|
||||
* char *string2 = "Techniques"
|
||||
* char *string3 = "is"
|
||||
* char *string4 = "great fun"
|
||||
*
|
||||
* char *lines[5];
|
||||
* lines[0] = string0;
|
||||
* lines[1] = string1;
|
||||
* ....
|
||||
*
|
||||
* char* lines[5] 定义了一个指针数组
|
||||
*/
|
||||
|
||||
char *str1 = lines[1];
|
||||
//attempt: str1存放着"COSC1283/1284"的首地址
|
||||
char *str2 = *(lines + 3);
|
||||
//att: str2存放着“is”的首地址
|
||||
char c1 = *(*(lines + 4) + 6);
|
||||
//att: c1 = 'f'
|
||||
char c2 = (*lines + 5)[5];
|
||||
//att: c2 =
|
||||
char c3 = *lines[0] + 2;
|
||||
//att: c3 = 'C'
|
||||
|
||||
printf("str1 = %s\n", str1);
|
||||
printf("str2 = %s\n", str2);
|
||||
printf(" c1=%c\n",c1);
|
||||
printf(" c2=%c\n",c2);
|
||||
printf(" c3=%c\n",c3);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
0
Cprogramming/scanf_advanceUsage.c
Normal file
0
Cprogramming/scanf_advanceUsage.c
Normal file
1
conio.h
Submodule
1
conio.h
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 70935b61efce6da9042f3543add106e7dc52e3ca
|
||||
Loading…
Reference in a new issue