mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-11-02 22:23:48 +08:00
5-16
This commit is contained in:
285
Coding/C_Mooc/01 Examples/常见字符串函数/Test/main.c
Normal file
285
Coding/C_Mooc/01 Examples/常见字符串函数/Test/main.c
Normal file
@@ -0,0 +1,285 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
char ch = -1;
|
||||
|
||||
int a = 0;
|
||||
|
||||
a = putchar(ch);
|
||||
|
||||
printf("\n%d", a);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
int ch;
|
||||
|
||||
while ((ch = getchar()) != EOF) {
|
||||
putchar(ch);
|
||||
}
|
||||
|
||||
printf("EOF\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str[] = { 'H', 'E', 'L', 'L', 'O', };
|
||||
|
||||
printf("%d\n", strlen(str));
|
||||
|
||||
printf("%d\n", strnlen_s(str, (size_t)sizeof(str)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test(const char* str) {
|
||||
|
||||
str++;
|
||||
*str = '1';
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str[] = { 'H', 'I', '!' };
|
||||
|
||||
str++;
|
||||
|
||||
printf("%d\n", test(str));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mystrlen(const char* str) {
|
||||
|
||||
char* end = str;
|
||||
|
||||
while (*end++);
|
||||
|
||||
//退出while循环时,多加了一次 1
|
||||
return end - str - 1;
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char* str = "Hello World!";
|
||||
|
||||
printf("%d\n", mystrlen(str));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mystrcmp(char* str1, char* str2) {
|
||||
|
||||
int ret = 0;
|
||||
|
||||
while ((ret = ((unsigned char)*str1 - (unsigned char)*str2)) == 0 && *str1) {
|
||||
++str1, ++str2;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
UINT_MAX
|
||||
|
||||
int main() {
|
||||
|
||||
char* str1 = "abc ";
|
||||
char* str2 = "abc";
|
||||
|
||||
printf("%d\n", mystrcmp(str1, str2));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char* str1 = NULL;
|
||||
char* str2 = "Hello";
|
||||
|
||||
printf("%d\n", strcmp(str1, str2));
|
||||
printf("%d\n", strncmp(str1, str2, 1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mystrcmp(char* str1, char* str2) {
|
||||
|
||||
while (*str1 == *str2 && *str1)
|
||||
++str1, ++str2;
|
||||
|
||||
return (*str1 - *str2);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char* str1 = "Hello ";
|
||||
char* str2 = "Hello";
|
||||
|
||||
printf("%d\n", mystrcmp(str1, str2));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char* str1 = "Hello";
|
||||
char* str2 = "Hello";
|
||||
|
||||
//strcpy(str1, str2);
|
||||
strncpy(str1, str2, 5);
|
||||
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str1[] = "Hello";
|
||||
char str2[] = "Hello";
|
||||
|
||||
//strcpy(str1, str2);
|
||||
strncpy(str1, str2, 6);
|
||||
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str1[] = "hello";
|
||||
char str2[] = "hello world";
|
||||
|
||||
strcpy(str1, str2);
|
||||
//strncpy(str1, str2, 5);
|
||||
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
char* str1 = "Hello";
|
||||
char* str2 = "Hello World";
|
||||
|
||||
strcpy(str1, str2);
|
||||
strncpy(str1, str2, 12);
|
||||
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
int main() {
|
||||
|
||||
char str1[] = "Hello World";
|
||||
char str2[] = { 'H', 'e', 'l','l', 'o' };
|
||||
|
||||
strncpy(str1, str2, 10);
|
||||
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str1[] = "Hello World";
|
||||
char str2[] = "World";
|
||||
|
||||
//strncpy(str1, str2, 10);
|
||||
strcpy(str1, str2);
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include<assert.h>
|
||||
|
||||
char* mystrcpy(char* str1, char* str2) {
|
||||
|
||||
assert(str1 != NULL && str2 != NULL);
|
||||
assert(strlen(str1) >= strlen(str2));
|
||||
|
||||
//核心代码从这里开始, 上面的不懂可以加QQ群问我(群在我公众号关注回复的消息里)
|
||||
char* after = str1;
|
||||
|
||||
while (*str1++ = *str2++);
|
||||
|
||||
return after;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char* str1 = "HI!!!";
|
||||
char* str2 = "Hello";
|
||||
|
||||
mystrcpy(str1, str2);
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str1[11] = "Hello";
|
||||
char str2[3] = { 'J', 'I', 'M' };
|
||||
|
||||
strcat(str1, str2);
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str2[3] = { 'J', 'I', 'M' };
|
||||
char str1[3] = "JIM";
|
||||
|
||||
|
||||
printf("%d\n", strcmp(str1, str2));
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str1[8] = "hi";
|
||||
char str2[4] = "you";
|
||||
|
||||
strncat(str1, str2, 5);
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* mystrcat(char* str1, char* str2) {
|
||||
|
||||
while (str1[strlen(str1)] = *str2++);
|
||||
|
||||
return str1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
char str1[8] = "Hi";
|
||||
char str2[4] = "YOU";
|
||||
|
||||
mystrcat(str1, str2);
|
||||
puts(str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
Coding/C_Mooc/01 Examples/常见字符串函数/Test/readme.md
Normal file
1
Coding/C_Mooc/01 Examples/常见字符串函数/Test/readme.md
Normal file
@@ -0,0 +1 @@
|
||||
写文章中的测试代码
|
||||
29
Coding/C_Mooc/01 Examples/常见字符串函数/mystrcmp/VSstrcmp.c
Normal file
29
Coding/C_Mooc/01 Examples/常见字符串函数/mystrcmp/VSstrcmp.c
Normal file
@@ -0,0 +1,29 @@
|
||||
//这是我能理解的格式
|
||||
int mystrcmp(char* str1, char* str2) {
|
||||
|
||||
int ret = 0;
|
||||
|
||||
while ((ret = ((unsigned char)*str1 - (unsigned char)*str2)) == 0 && *str1) {
|
||||
++str1, ++str2;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//原版在这里
|
||||
int __cdecl strcmp(
|
||||
const char* src,
|
||||
const char* dst
|
||||
)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
while ((ret = *(unsigned char*)src - *(unsigned char*)dst) == 0 && *dst)
|
||||
{
|
||||
++src, ++dst;
|
||||
}
|
||||
|
||||
//这里不太理解,那个大神理解了可以给我讲一下,谢谢
|
||||
return ((-ret) < 0) - (ret < 0); // (if positive) - (if negative) generates branchless code
|
||||
}
|
||||
11
Coding/C_Mooc/01 Examples/常见字符串函数/mystrcmp/main.c
Normal file
11
Coding/C_Mooc/01 Examples/常见字符串函数/mystrcmp/main.c
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
char* str1 = "Hello ";
|
||||
char* str2 = "Hello";
|
||||
|
||||
printf("%d\n", mystrcmp(str1, str2));
|
||||
|
||||
return 0;
|
||||
}
|
||||
7
Coding/C_Mooc/01 Examples/常见字符串函数/mystrcmp/mystrcmp.c
Normal file
7
Coding/C_Mooc/01 Examples/常见字符串函数/mystrcmp/mystrcmp.c
Normal file
@@ -0,0 +1,7 @@
|
||||
int mystrcmp(char* str1, char* str2) {
|
||||
|
||||
while (*str1 == *str2 && *str1)
|
||||
++str1, ++str2;
|
||||
|
||||
return (*str1 - *str2);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
#include<stdbool.h>
|
||||
|
||||
struct date {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
//月 - 日 - 年 咱得国际化!
|
||||
};
|
||||
|
||||
bool isleap(struct date d);//判断是否为闰年
|
||||
int NumberOfDays(struct date d);//判断天数
|
||||
bool ErrorInputCheck(struct date d);//检查是否是合法输入
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct date today, tomorrow;
|
||||
int flag = 0;
|
||||
|
||||
printf("Input today's date:(format: month day year)\n");
|
||||
scanf("%d %d %d", &today.month, &today.day, &today.year);
|
||||
|
||||
if (ErrorInputCheck(today))
|
||||
return 0;
|
||||
|
||||
if (today.day != NumberOfDays(today)) {
|
||||
|
||||
tomorrow = today;
|
||||
tomorrow.day++;
|
||||
|
||||
}
|
||||
else if (today.month != 12) {
|
||||
|
||||
tomorrow.day = 1;
|
||||
tomorrow.month = today.month + 1;
|
||||
tomorrow.year = today.year;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
tomorrow.day = 1;
|
||||
tomorrow.month = 1;
|
||||
tomorrow.year = today.year + 1;
|
||||
|
||||
}
|
||||
|
||||
printf("%d - %d - %d\n", tomorrow.month, tomorrow.day, tomorrow.year);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ErrorInputCheck(struct date d) {
|
||||
|
||||
bool flag = false;
|
||||
|
||||
if (d.day <= 0 || d.day > 31) {
|
||||
|
||||
printf("illegal input of day!\n");
|
||||
flag = true;
|
||||
|
||||
}
|
||||
if (d.month <= 0 || d.month > 12) {
|
||||
|
||||
printf("illegal input of month!\n");
|
||||
flag = true;
|
||||
|
||||
}
|
||||
if (d.year < 0) {
|
||||
|
||||
printf("illegal year!\n");
|
||||
flag = true;
|
||||
|
||||
}
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
int NumberOfDays(struct date d) {
|
||||
|
||||
int day = 0;
|
||||
int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
|
||||
if (d.month == 2 && isleap)
|
||||
day = 29;
|
||||
else
|
||||
day = days[d.month - 1];
|
||||
|
||||
return day;
|
||||
}
|
||||
|
||||
bool isleap(struct date d) {
|
||||
|
||||
bool leap = false;
|
||||
int year = d.year;
|
||||
|
||||
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
|
||||
leap = true;
|
||||
|
||||
return leap;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
## 结构体作为函数参数
|
||||
**明天的日期**
|
||||
*写一个程序:输入今天的日期,求明天的日期*
|
||||
|
||||
看着这个问题简单,实际上稍微有点技巧.
|
||||
|
||||
给出下面四个特殊日期,大家可以思考一下:
|
||||
|
||||
>2020 - 1 -31
|
||||
>
|
||||
>2020 - 11 - 31
|
||||
>
|
||||
>2020 - 12 - 31
|
||||
>
|
||||
>2000 - 2 - 28
|
||||
|
||||
## Use sturcture itself as a function's parameter
|
||||
it's not an efficient way
|
||||
|
||||
>K & R said (p.131 )
|
||||
>"if a large structure is to be passed to a function,
|
||||
>in is generally more efficent to pass a pointer than to copy the whole structure"
|
||||
542
Coding/C_Mooc/01 Examples/结构/test/test.c
Normal file
542
Coding/C_Mooc/01 Examples/结构/test/test.c
Normal file
@@ -0,0 +1,542 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
enum color {
|
||||
red, yellow, blue
|
||||
};
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
enum color {
|
||||
red, yellow, blue
|
||||
};
|
||||
|
||||
void f(enum color x) {
|
||||
printf("%d\n", x);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
// 变量 t 的类型是 enum color
|
||||
enum color t = yellow;
|
||||
|
||||
int y = blue;
|
||||
|
||||
printf("%d\n", y);
|
||||
|
||||
scanf("%d", &t);//可以当作整数输入输出
|
||||
|
||||
f(t);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum color {
|
||||
red, yellow, blue, NumColors
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
char* ColorNames[NumColors] = {
|
||||
"red", "yellow", "blue",
|
||||
};
|
||||
|
||||
char* colorName = NULL;
|
||||
int color = 0;
|
||||
|
||||
printf("输入你喜欢的颜色所对应的代码:\n");
|
||||
scanf("%d", &color);
|
||||
|
||||
if (color >= 0 && color < NumColors)
|
||||
colorName = ColorNames[color];
|
||||
else
|
||||
colorName = "Unknowe";
|
||||
|
||||
printf("%s\n", colorName);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum COLOR { RED = 1, YELLOW, GREEN = 5, NumColors, };
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%d\n%d", YELLOW, NumColors);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum color {
|
||||
red = 1, greem, yellow
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
|
||||
enum color t = 0;
|
||||
|
||||
printf("%d\n", t);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct date {
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct date today;
|
||||
|
||||
today.year = 2020;
|
||||
today.month = 2;
|
||||
today.day = 9;
|
||||
|
||||
printf("%d年 - %d月 - %d日\n", today.year, today.month, today.day);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct point {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct point p1, p2;
|
||||
|
||||
struct {
|
||||
int x;
|
||||
int y;
|
||||
}; p1
|
||||
|
||||
struct date {
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct date today = { 2020, 2, 9 };
|
||||
struct date tomorrow = { .month = 2, .day = 9 };
|
||||
|
||||
printf("%d - %d - %d\n", today.year, today.month, today.day);
|
||||
printf("%d - %d - %d\n", tomorrow.year, tomorrow.month, tomorrow.day);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct date {
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
}p1, p2;
|
||||
|
||||
int main(void) {
|
||||
|
||||
//struct date p1;
|
||||
p1 = (struct date){ 2020, 2, 9 };
|
||||
p2 = (struct date){ 1010, 1, 5 };
|
||||
|
||||
p2 = p1;
|
||||
|
||||
printf("%d - %d - %d\n", p2.year, p2.month, p2.day);
|
||||
|
||||
return 0;
|
||||
}
|
||||
int main(void) {
|
||||
|
||||
printf("%d\n", sizeof(char*));
|
||||
printf("%d\n", sizeof(short));
|
||||
printf("%d\n", sizeof(long));
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include<stdbool.h>
|
||||
|
||||
struct date {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
//月 - 日 - 年 咱得国际化!
|
||||
};
|
||||
|
||||
bool isleap(struct date d);//判断是否为闰年
|
||||
int NumberOfDays(struct date d);//判断天数
|
||||
bool ErrorInputCheck(struct date d);//检查是否是合法输入
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct date today, tomorrow;
|
||||
int flag = 0;
|
||||
|
||||
printf("Input today's date:(format: month day year)\n");
|
||||
scanf("%d %d %d", &today.month, &today.day, &today.year);
|
||||
|
||||
if (ErrorInputCheck(today))
|
||||
return 0;
|
||||
|
||||
if (today.day != NumberOfDays(today)) {
|
||||
|
||||
tomorrow = today;
|
||||
tomorrow.day++;
|
||||
|
||||
}
|
||||
else if (today.month != 12) {
|
||||
|
||||
tomorrow.day = 1;
|
||||
tomorrow.month = today.month + 1;
|
||||
tomorrow.year = today.year;
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
tomorrow.day = 1;
|
||||
tomorrow.month = 1;
|
||||
tomorrow.year = today.year + 1;
|
||||
|
||||
}
|
||||
|
||||
printf("%d - %d - %d\n", tomorrow.month, tomorrow.day, tomorrow.year);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ErrorInputCheck(struct date d) {
|
||||
|
||||
bool flag = false;
|
||||
|
||||
if (d.day <= 0 || d.day > 31) {
|
||||
|
||||
printf("illegal input of day!\n");
|
||||
flag = true;
|
||||
|
||||
}
|
||||
if (d.month <= 0 || d.month > 12) {
|
||||
|
||||
printf("illegal input of month!\n");
|
||||
flag = true;
|
||||
|
||||
}
|
||||
if (d.year < 0) {
|
||||
|
||||
printf("illegal year!\n");
|
||||
flag = true;
|
||||
|
||||
}
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
int NumberOfDays(struct date d) {
|
||||
|
||||
int day = 0;
|
||||
int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
||||
|
||||
if (d.month == 2 && isleap)
|
||||
day = 29;
|
||||
else
|
||||
day = days[d.month - 1];
|
||||
|
||||
return day;
|
||||
}
|
||||
|
||||
bool isleap(struct date d) {
|
||||
|
||||
bool leap = false;
|
||||
int year = d.year;
|
||||
|
||||
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
|
||||
leap = true;
|
||||
|
||||
return leap;
|
||||
}
|
||||
|
||||
struct date {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
}myday;
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct date* p = &myday;
|
||||
|
||||
//两种访问指针结构体变量成员方式
|
||||
(*p).month = 12;
|
||||
|
||||
p->month = 12;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct point {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
};
|
||||
|
||||
struct point* getStruct(struct point* p);//输入结构体函数
|
||||
|
||||
void outputStruct(struct point p);//输出结构体函数
|
||||
|
||||
void printStruct(const struct point* p);//输出结构体函数
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct point y = { 0, 0, 0 };
|
||||
|
||||
*getStruct(&y);
|
||||
// *getStruct(&y) = (struct point){ 0, 0, 0 };
|
||||
|
||||
outputStruct(y);
|
||||
outputStruct(*getStruct(&y));
|
||||
|
||||
printStruct(&y);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct point* getStruct(struct point* p) {
|
||||
|
||||
printf("Input a date\n");
|
||||
scanf("%d", &p->month);
|
||||
scanf("%d", &p->day);
|
||||
scanf("%d", &p->year);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void outputStruct(struct point p) {
|
||||
|
||||
printf("outputStruct:\n");
|
||||
printf("%d - %d - %d\n", p.month, p.day, p.year);
|
||||
}
|
||||
|
||||
void printStruct(const struct point* p) {
|
||||
|
||||
printf("printStruct:\n");
|
||||
printf("%d - %d - %d\n", p->month, p->day, p->year);
|
||||
}
|
||||
|
||||
struct date {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
};
|
||||
|
||||
struct date dates[100];
|
||||
struct date datess[2] = { {2, 9, 2020}, {2, 10, 2020} };
|
||||
|
||||
struct time {
|
||||
int hour;
|
||||
int minute;
|
||||
int second;
|
||||
};
|
||||
|
||||
struct time* timeUpdate(struct time* p) {
|
||||
|
||||
if (p->second != 59) {
|
||||
p->second++;
|
||||
}
|
||||
else if (p->minute != 59) {
|
||||
p->second = 0;
|
||||
p->minute++;
|
||||
}
|
||||
else if (p->hour != 23) {
|
||||
p->second = 0;
|
||||
p->minute = 0;
|
||||
p->hour++;
|
||||
}
|
||||
else {
|
||||
p->second = 0;
|
||||
p->minute = 0;
|
||||
p->hour = 0;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct time testTimes[5] = {
|
||||
{11, 59, 59}, {12, 0, 0}, {1, 29, 59}, {23, 59, 59}, {19, 12, 27}
|
||||
};
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
|
||||
printf("Now the time is:%d - %d - %d\n",
|
||||
testTimes[i].hour, testTimes[i].minute, testTimes[i].second);
|
||||
|
||||
testTimes[i] = *timeUpdate(&testTimes[i]);
|
||||
|
||||
printf("one second letter:%d - %d - %d\n",
|
||||
testTimes[i].hour, testTimes[i].minute, testTimes[i].second);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//这个结构体表示 二维坐标系中点的坐标
|
||||
struct point {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
//这个结构体表示一个矩形 (两点确定一个矩形)
|
||||
struct rectangle {
|
||||
struct point p1;
|
||||
struct point p2;
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct rectangle r;
|
||||
r.p1.x;
|
||||
r.p2.y;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct point {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct rectangle {
|
||||
struct point p1;
|
||||
struct point p2;
|
||||
struct point* p3;
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct rectangle r, * pr;
|
||||
|
||||
r.p1.x;
|
||||
(r.p1).x;
|
||||
|
||||
pr->p1.x;
|
||||
(pr->p1).x;
|
||||
//为什么访问 point 不需要用 -> 呢?
|
||||
//因为在 rectangle 中 point 并不是指针结构体
|
||||
|
||||
pr->p3->x;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct point {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct rectangle {
|
||||
struct point p1;
|
||||
struct point p2;
|
||||
|
||||
};
|
||||
|
||||
void printStruct(struct rectangle* p, int len) {
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
|
||||
printf("(%d, %d) (%d, %d)\n", p[i].p1.x, p[i].p1.y, p[i].p2.x, p[i].p2.y);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
struct rectangle rects[] = {
|
||||
{{2, 2}, {4, 4} },
|
||||
{{5, 6}, {7, 8} }
|
||||
};
|
||||
int len = sizeof(rects) / sizeof(rects[0]);
|
||||
|
||||
printStruct(rects, len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct {
|
||||
int x, y;
|
||||
} s[2] = {
|
||||
{1, 3},
|
||||
{2, 7}
|
||||
};
|
||||
|
||||
printf("%d\n", s[0].y / s[1].x);
|
||||
|
||||
struct sk {
|
||||
int a;
|
||||
float b;
|
||||
}data, * p = &data;
|
||||
|
||||
A: (*p).data.a;
|
||||
B: (*p).a;
|
||||
C: p->data.a;
|
||||
D: p.data.a;
|
||||
|
||||
struct { int x; int y; }x;
|
||||
struct { int x; int y; }y;
|
||||
|
||||
A: √
|
||||
B : ×
|
||||
|
||||
typedef long int64_t;
|
||||
|
||||
typedef struct Adate {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
}date;
|
||||
|
||||
int main(void) {
|
||||
|
||||
int64_t i = (int64_t)1e+10;
|
||||
date d = { 2, 10, 2020 };
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int month;
|
||||
int day;
|
||||
int year;
|
||||
}Date;
|
||||
|
||||
union AnEit {
|
||||
int i;
|
||||
char c;
|
||||
}elt1, elt2;
|
||||
|
||||
int main(void) {
|
||||
|
||||
elt1.i = 4;
|
||||
elt2.c = 'a';
|
||||
elt2.i = 0xDEADBEEF;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef union {
|
||||
int i;
|
||||
char ch[sizeof(int)];
|
||||
}CHI;
|
||||
|
||||
int main(void) {
|
||||
|
||||
CHI chi;
|
||||
int i;
|
||||
chi.i = 1234;
|
||||
for (i = 0; i < sizeof(int); i++) {
|
||||
printf("%02hhx", chi.ch[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
234
Coding/C_Mooc/01 Examples/编写大型程序/test.c
Normal file
234
Coding/C_Mooc/01 Examples/编写大型程序/test.c
Normal file
@@ -0,0 +1,234 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
//全局变量
|
||||
|
||||
int f(void);
|
||||
|
||||
int gAll = 12;
|
||||
|
||||
int main(void){
|
||||
|
||||
//__func__ 可以打印出当前函数的函数名,下划线一边是两个
|
||||
printf("in %s gAll = %d\n", __func__, gAll);
|
||||
f();
|
||||
printf("again in %s gAll = %d\n", __func__, gAll);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(void) {
|
||||
|
||||
printf("in %s gAll = %d\n", __func__, gAll);
|
||||
gAll += 2;
|
||||
printf("again in %s gAll = %d\n", __func__, gAll);
|
||||
|
||||
return gAll;
|
||||
}
|
||||
|
||||
int gAll = 12;
|
||||
int g = gAll;
|
||||
|
||||
int main(void) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const int gAll = 12;
|
||||
int g = gAll;
|
||||
|
||||
int main(void) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(void);
|
||||
|
||||
int gAll = 12;
|
||||
|
||||
int main(void) {
|
||||
|
||||
int gAll = 2;
|
||||
|
||||
printf("in %s gAll = %d\n", __func__, gAll);
|
||||
|
||||
f();
|
||||
|
||||
printf("again in %s gAll = %d\n", __func__, gAll);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(void) {
|
||||
|
||||
printf("in %s gAll = %d\n", __func__, gAll);
|
||||
gAll += 2;
|
||||
printf("again in %s gAll = %d\n", __func__, gAll);
|
||||
|
||||
return gAll;
|
||||
}
|
||||
|
||||
int f(void);
|
||||
|
||||
int main(void) {
|
||||
|
||||
f();
|
||||
f();
|
||||
f();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(void) {
|
||||
|
||||
static int All = 1;
|
||||
printf("in %s All = %d\n", __func__, All);
|
||||
All += 2;
|
||||
printf("again in %s All = %d\n", __func__, All);
|
||||
|
||||
return All;
|
||||
}
|
||||
|
||||
int f(void);
|
||||
|
||||
int gAll = 12;
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("1 st\n");
|
||||
f();
|
||||
printf("2 nd\n");
|
||||
f();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f(void) {
|
||||
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
static int All = 1;
|
||||
|
||||
printf("&All : %p\n", &All);
|
||||
printf("&gAll: %p\n", &gAll);
|
||||
printf("&a : %p\n", &a);
|
||||
printf("&b : %p\n", &b);
|
||||
|
||||
return All;
|
||||
}
|
||||
|
||||
int* f(void);
|
||||
void g(void);
|
||||
|
||||
int main(void) {
|
||||
|
||||
int* p = f();
|
||||
printf("*p = %d\n", *p);
|
||||
g();
|
||||
printf("*p = %d\n", *p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int* f(void) {
|
||||
|
||||
int i = 12;
|
||||
|
||||
return &i;
|
||||
}
|
||||
void g(void) {
|
||||
|
||||
int k = 24;
|
||||
|
||||
printf("k = %d\n", k);
|
||||
|
||||
return k;
|
||||
}
|
||||
|
||||
编译预处理 与 宏
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
const double PI = 3.14159;
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%f\n", 2 * PI * 3.0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
#define PI 3.14159
|
||||
//注意:不写分号 不写等于号
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%f\n", 2 * PI * 3.0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%s : %d\n", __FILE__, __LINE__);
|
||||
printf("%s %s\n", __DATE__, __TIME__);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define cube(x) ((x) * (x) * (x))
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%d\n", cube(5));
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define ERROR1(x) (x * 57)
|
||||
#define ERROR2(x) (x) * 57
|
||||
|
||||
int main(void) {
|
||||
|
||||
int i = 1;
|
||||
printf("%d\n", ERROR1(i + 2));
|
||||
printf("%d\n", 300 / ERROR2(1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define PRETTY_PRINT(msg) printf(msg);
|
||||
|
||||
int main(void) {
|
||||
|
||||
int n = 0;
|
||||
|
||||
printf("Input an number\n");
|
||||
scanf("%d", &n);
|
||||
|
||||
if (n < 10)
|
||||
PRETTY_PRINT("less than 10\n");
|
||||
else
|
||||
PRETTY_PRINT("more than 10\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
|
||||
#define TOUPPER(c) ('a' <= (c) && (c) <= 'z' ? (c) - 'a' + 'A' : (c))
|
||||
|
||||
int main(void) {
|
||||
|
||||
int i = 0;
|
||||
char s[1000];
|
||||
strcpy(s, "abcd");
|
||||
putchar(TOUPPER(s[++i]));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
大程序结构
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#include<stdio.h>
|
||||
#include"student.h"
|
||||
|
||||
void read(FILE* fp, int index);
|
||||
|
||||
int main(void) {
|
||||
|
||||
FILE* fp = fopen("student.data", "r");
|
||||
if (fp) {
|
||||
fseek(fp, 0L, SEEK_END);//将文件读/写开始的位置定到结尾
|
||||
long size = ftell(fp);//获得从文件开始到文件结尾的字节数
|
||||
int index = 0;
|
||||
int number = size / sizeof(Student);
|
||||
printf("有 %d 个学生数据,你要看第几个\n", number);
|
||||
scanf("%d", &index);
|
||||
read(fp, index - 1);
|
||||
}
|
||||
else
|
||||
printf("文件打开失败!\n");
|
||||
}
|
||||
|
||||
void read(FILE* fp, int index) {
|
||||
|
||||
fseek(fp, index * sizeof(Student), SEEK_SET);
|
||||
|
||||
Student stu;
|
||||
|
||||
if (fread(&stu, sizeof(Student), 1, fp) == 1) {
|
||||
printf("第 %d 个学生:\n", index + 1);
|
||||
printf("\t姓名:%s\n", stu.name);
|
||||
printf("\t性别:");
|
||||
switch (stu.gender) {
|
||||
case 0:
|
||||
printf("男\n");
|
||||
break;
|
||||
case 1:
|
||||
printf("女\n");
|
||||
break;
|
||||
default:
|
||||
printf("其他\n");
|
||||
break;
|
||||
}
|
||||
printf("\t年龄:%d\n", stu.age);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
程序中向文件中写入/读取2进制文本
|
||||
|
||||
linux 下可以用 `od` 命令打开文件
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef _STUDENT_H_
|
||||
#define _STUDENT_H_
|
||||
|
||||
#define STR_LEN 20
|
||||
|
||||
typedef struct _student {
|
||||
char name[STR_LEN];
|
||||
char gender;
|
||||
char age;
|
||||
}Student;
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include "student.h"
|
||||
|
||||
void getList(Student* Stu, int num);
|
||||
int save(Student* Stu, int num);
|
||||
|
||||
int main(void) {
|
||||
|
||||
int num = 0;
|
||||
|
||||
printf("请输入学生数量:\n");
|
||||
scanf("%d", &num);
|
||||
Student* Stu = (Student*)malloc(sizeof(Student) * num);
|
||||
|
||||
getList(Stu, num);
|
||||
|
||||
if (save(Stu, num)) {
|
||||
printf("保存成功\n");
|
||||
}
|
||||
else
|
||||
printf("保存失败\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void getList(Student* Stu, int num) {
|
||||
|
||||
char format[STR_LEN];
|
||||
sprintf(format, "%%%ds", STR_LEN - 1);
|
||||
//向 format 中写入 %19s
|
||||
//%%%ds 前两个%的意义是写入%
|
||||
//%d 输出后面的参数,即:19
|
||||
//最终写入的就是 %19s
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
|
||||
printf("第 %d 个学生 \n", i + 1);
|
||||
printf("\t姓名:");
|
||||
scanf(format, Stu[i].name);
|
||||
printf("\t性别(0-男 1-女 2-其他):");
|
||||
scanf("%d", &Stu[i].gender);
|
||||
printf("\t年龄:");
|
||||
scanf("%d", &Stu[i].age);
|
||||
//记得用scanf时,何时用 &
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int save(Student* Stu, int num) {
|
||||
|
||||
int ret = -1;
|
||||
FILE* fp = fopen("student.data", "wx");//如果要向student.data反复写入数据,将 wx 改为 x
|
||||
if (fp) {
|
||||
ret = fwrite(Stu, sizeof(Student), num, fp);
|
||||
//记录 fwrite 的返回值:写入文件的学生个数
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
printf("文件打开失败!");
|
||||
|
||||
return (ret == num);
|
||||
//如果写入的个数不等于总人数,返回0
|
||||
}
|
||||
195
Coding/C_Mooc/01 Examples/输入&输出/test code/test.c
Normal file
195
Coding/C_Mooc/01 Examples/输入&输出/test code/test.c
Normal file
@@ -0,0 +1,195 @@
|
||||
#include<stdio.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%+09d\n", 123);
|
||||
printf("%-+09d\n", -123);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%+09.2f\n", 12.3);
|
||||
printf("%-8.3f\n", -12.3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
int len = 5;
|
||||
int dec = 2;
|
||||
printf("%*.*f\n",5, 2, 12.3);
|
||||
printf("%*.*f\n",len, dec, 12.3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("%hhd\n", (char)12345);//当作 1 个字节输出
|
||||
printf("%hd\n", 12345);//当作 short 输出
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int num;
|
||||
printf("%d%n\n", 123, &num);
|
||||
printf("%d\n", num);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int num;
|
||||
scanf("%*d %d", &num);
|
||||
printf("%d\n", num);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
float x;
|
||||
scanf("%g", &x);
|
||||
printf("%F", x);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
int num = 0;
|
||||
int return_scanf = 0;
|
||||
int return_printf = 0;
|
||||
|
||||
return_scanf = scanf("%d", &num);
|
||||
return_printf = printf("%d\n", num);
|
||||
|
||||
printf("%d:%d\n", return_scanf, return_printf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
FILE* fp = fopen("file", "r");
|
||||
|
||||
if (fp) {
|
||||
fscanf(fp, ...);
|
||||
fclose(fp);
|
||||
}
|
||||
else {
|
||||
...
|
||||
}
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include "student.h"
|
||||
|
||||
void getList(Student* Stu, int num);
|
||||
int save(Student* Stu, int num);
|
||||
|
||||
int main(void) {
|
||||
|
||||
int num = 0;
|
||||
|
||||
printf("请输入学生数量:\n");
|
||||
scanf("%d", &num);
|
||||
Student* Stu = (Student*)malloc(sizeof(Student) * num);
|
||||
|
||||
getList(Stu, num);
|
||||
|
||||
if (save(Stu, num)) {
|
||||
printf("保存成功\n");
|
||||
}
|
||||
else
|
||||
printf("保存失败\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void getList(Student* Stu, int num) {
|
||||
|
||||
char format[STR_LEN];
|
||||
sprintf(format, "%%%ds", STR_LEN - 1);
|
||||
//向 format 中写入 %19s
|
||||
//%%%ds 前两个%的意义是写入%
|
||||
//%d 输出后面的参数,即:19
|
||||
//最终写入的就是 %19s
|
||||
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
|
||||
printf("第 %d 个学生 \n", i + 1);
|
||||
printf("\t姓名:");
|
||||
scanf(format, Stu[i].name);
|
||||
printf("\t性别(0-男 1-女 2-其他):");
|
||||
scanf("%d", &Stu[i].gender);
|
||||
printf("\t年龄:");
|
||||
scanf("%d", &Stu[i].age);
|
||||
//记得用scanf时,何时用 &
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int save(Student* Stu, int num) {
|
||||
|
||||
int ret = -1;
|
||||
FILE* fp = fopen("student.data", "w");
|
||||
if (fp) {
|
||||
ret = fwrite(Stu, sizeof(Student), num, fp);
|
||||
//记录 fwrite 的返回值:写入文件的学生个数
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
printf("文件打开失败!");
|
||||
|
||||
return (ret == num);
|
||||
//如果写入的个数不等于总人数,返回0
|
||||
}
|
||||
|
||||
#include<stdio.h>
|
||||
#include"student.h"
|
||||
|
||||
void read(FILE* fp, int index);
|
||||
|
||||
int main(void) {
|
||||
|
||||
FILE* fp = fopen("student.data", "r");
|
||||
if (fp) {
|
||||
fseek(fp, 0L, SEEK_END);//将文件读/写开始的位置定到结尾
|
||||
long size = ftell(fp);//获得从文件开始到文件结尾的字节数
|
||||
int index = 0;
|
||||
int number = size / sizeof(Student);
|
||||
printf("有 %d 个学生数据,你要看第几个\n", number);
|
||||
scanf("%d", &index);
|
||||
read(fp, index - 1);
|
||||
}
|
||||
else
|
||||
printf("文件打开失败!\n");
|
||||
}
|
||||
|
||||
void read(FILE* fp, int index) {
|
||||
|
||||
fseek(fp, index * sizeof(Student), SEEK_SET);
|
||||
|
||||
Student stu;
|
||||
|
||||
if (fread(&stu, sizeof(Student), 1, fp) == 1) {
|
||||
printf("第 %d 个学生:\n", index + 1);
|
||||
printf("\t姓名:%s\n", stu.name);
|
||||
printf("\t性别:");
|
||||
switch (stu.gender) {
|
||||
case 0:
|
||||
printf("男\n");
|
||||
break;
|
||||
case 1:
|
||||
printf("女\n");
|
||||
break;
|
||||
default:
|
||||
printf("其他\n");
|
||||
break;
|
||||
}
|
||||
printf("\t年龄:%d\n", stu.age);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user