mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-05-08 15:02:50 +08:00
5-16
This commit is contained in:
276
Coding/Advanced_C/01 Examples/Address_Book/AddressBook.c
Normal file
276
Coding/Advanced_C/01 Examples/Address_Book/AddressBook.c
Normal file
@@ -0,0 +1,276 @@
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
|
||||
#define Max_SIZE 50 //定义通讯录的大小
|
||||
|
||||
typedef struct PersonInfo {
|
||||
char name[100];
|
||||
char phone[100];
|
||||
}PersonInfo;
|
||||
|
||||
typedef struct AddressBook {
|
||||
PersonInfo all_address[Max_SIZE];
|
||||
int size;
|
||||
}AddressBook;
|
||||
//size 的含义是:
|
||||
//数组 all_address 下标范围在 [0,size) 内的元素是有意义的
|
||||
// [size, 200) 是我们没有用到的
|
||||
|
||||
//初始化
|
||||
void init(AddressBook* address_book) {
|
||||
|
||||
address_book->size = 0;
|
||||
//尽量少用 magic number(不明含义的数字)
|
||||
for (int i = 0; i < Max_SIZE; i++) {
|
||||
strcpy(address_book->all_address->name, " ");
|
||||
strcpy(address_book->all_address->phone, " ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int Menu(void) {
|
||||
|
||||
printf("======================\n");
|
||||
printf("*** 0.退出 ***\n");
|
||||
printf("*** 1.新增联系人 ***\n");
|
||||
printf("*** 2.删除联系人 ***\n");
|
||||
printf("*** 3.查找联系人 ***\n");
|
||||
printf("*** 4.修改联系人 ***\n");
|
||||
printf("*** 5.打印联系人 ***\n");
|
||||
printf("*** 6.清除联系人 ***\n");
|
||||
printf("*** 7.排序联系人 ***\n");
|
||||
printf("======================\n");
|
||||
|
||||
printf("请输入你的选择:");
|
||||
int choice;
|
||||
scanf("%d", &choice);
|
||||
|
||||
return choice;
|
||||
}
|
||||
|
||||
void AddPersonInfo(AddressBook* address_book) {
|
||||
|
||||
printf("新增联系人\n");
|
||||
|
||||
if (address_book->size >= Max_SIZE) {
|
||||
printf("通讯录已满,请先清除!\n");
|
||||
return;
|
||||
//虽然函数类型是 void 但是也是可以用 return 滴
|
||||
}
|
||||
PersonInfo* info = &address_book->all_address[address_book->size];
|
||||
|
||||
printf("请输入联系人姓名:");
|
||||
scanf("%s", info->name);
|
||||
printf("请输入联系人电话:");
|
||||
scanf("%s", info->phone);
|
||||
|
||||
address_book->size++;
|
||||
}
|
||||
|
||||
void DelPersonInfo(AddressBook* address_book) {
|
||||
|
||||
//删除的方法很多,可以根据姓名,电话,序号等等来删除,
|
||||
//这里我们就用我个人比较常用的 搜索名字的删除方法
|
||||
|
||||
char search_name[100] = { 0 };
|
||||
|
||||
printf("删除联系人\n");
|
||||
printf("请输入联系人姓名:");
|
||||
scanf("%s", search_name);
|
||||
|
||||
int name_exist = FindName(address_book, search_name);
|
||||
|
||||
//这里注意 FindName 返回值设定,要被删除的元素下标可能是 0,这种情况下会返回 0
|
||||
if (name_exist == -1) {
|
||||
printf("该联系人不存在!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//删除了相同姓名的第一个后,继续寻找改名字,如果找到了,重复上面的操作,如果没找到,退出循环
|
||||
//相同的姓名的情况比较复杂,在修改,查找,排序等等场景都会带来麻烦,
|
||||
//而且平时你的通讯录中难道会将两个相同姓名的人的备注写成一样的吗?
|
||||
//所以,我仅仅在删除功能中实现一种针对相同姓名的情况的设计思路,后面的其他功能默认没有重复姓名的情况。
|
||||
while (name_exist != -1) {
|
||||
|
||||
//将 all_address 数组的最后一个元素赋值给要删除的元素,完成删除
|
||||
//结构体类型是可以直接赋值的
|
||||
address_book->all_address[name_exist] = address_book->all_address[address_book->size - 1];
|
||||
address_book->size--;
|
||||
name_exist = FindName(address_book, search_name);
|
||||
}
|
||||
|
||||
printf("删除成功!\n");
|
||||
|
||||
}
|
||||
|
||||
int FindName(AddressBook* address_book, char search_name[100]) {
|
||||
|
||||
for (int i = 0; i < address_book->size; i++) {
|
||||
//找到返回数组下标
|
||||
if (strcmp(address_book->all_address->name, search_name) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
//没有找到,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
void FindPersonInfo(AddressBook* address_book) {
|
||||
|
||||
char search_name[100] = { 0 };
|
||||
|
||||
//搜索人的方式也很多,我们这里用搜索名字的方法
|
||||
printf("更新联系人\n");
|
||||
printf("请输入人名:");
|
||||
scanf("%s", search_name);
|
||||
|
||||
for (int i = 0; i < address_book->size; i++) {
|
||||
PersonInfo* info = &address_book->all_address[i];// 创建一个 PersonInfo 类型的变量简化程序,不然下面的姓名访问就太长了
|
||||
if (strcmp(info->name, search_name) == 0) {
|
||||
printf("[%d] %s %s\n", i, info->name, info->phone);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void ModifyPersonInfo(AddressBook* address_book) {
|
||||
|
||||
char search_name[100] = { 0 };
|
||||
int isjump = 1;
|
||||
|
||||
printf("删除联系人\n");
|
||||
printf("请输入联系人姓名:");
|
||||
scanf("%s", search_name);
|
||||
|
||||
int name_exist = FindName(address_book, search_name);
|
||||
|
||||
if (name_exist == -1) {
|
||||
printf("该联系人不存在!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//优化以下用户的体验
|
||||
printf("请输入新的姓名,输入 0 跳过:");
|
||||
scanf("%d", &isjump);
|
||||
if (isjump) {
|
||||
scanf("%s", address_book->all_address[name_exist].name);
|
||||
}
|
||||
printf("请输入新的电话,输入 0 跳过:");
|
||||
scanf("%d", &isjump);
|
||||
if (isjump) {
|
||||
scanf("%s", address_book->all_address[name_exist].phone);
|
||||
}
|
||||
|
||||
printf("更新成功!\n");
|
||||
|
||||
}
|
||||
|
||||
void PrintPersonInfo(AddressBook* address_book) {
|
||||
|
||||
PersonInfo* info;
|
||||
|
||||
if (address_book->size == 0) {
|
||||
printf("当前没有联系人!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("所有联系人信息如下:\n");
|
||||
|
||||
for (int i = 0; i < address_book->size; i++) {
|
||||
info = &address_book->all_address[i];
|
||||
printf("[%2d]%4s %s\n", i, info->name, info->phone);
|
||||
}
|
||||
}
|
||||
|
||||
void ClearPersonInfo(AddressBook* address_book) {
|
||||
|
||||
//清除所有信息是一种 危险的行为,我们最好让用户确认一次
|
||||
//相比你应该在自己的手机上回复过出厂设置,系统应该会让你确认不止一次!
|
||||
int is_continue = 0;
|
||||
|
||||
printf("清除所有联系人,你确定吗?输入 0 继续: ");
|
||||
scanf("%d", &is_continue);
|
||||
if(is_continue == 0) {
|
||||
//将 size 置为 0 即可,不过你也可以将数组的每个元素都进行重置
|
||||
address_book->size = 0;
|
||||
}
|
||||
printf("清除完成!\n");
|
||||
}
|
||||
|
||||
void SortPersonInfo(AddressBook* address_book) {
|
||||
|
||||
printf("排序通讯录\n");
|
||||
|
||||
for (int i = 0; i < address_book->size - 1; i++) {
|
||||
for (int j = 0; j < address_book->size - 1 - i; j++) {
|
||||
PersonInfo* info = &address_book->all_address[j];
|
||||
PersonInfo* info_next = &address_book->all_address[j + 1];
|
||||
//简单的用 strcmp 进行排序,不过排序的行为感觉是“未定义”的,
|
||||
//看着有规律,但再多试试会发现很多情况并没有规律。
|
||||
//也还有很多可以排血的函数。比如:strcoll,wcsscoll,wcscmp 这些只要你能弄懂,我想你也可以用。
|
||||
//或者有其他更好的实现办法,但这并不是我们在这里的重点。
|
||||
if (strcmp(info->name, info_next) > 0) {
|
||||
PersonInfo tmp;
|
||||
tmp = *info;
|
||||
*info = *info_next;
|
||||
*info_next = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("排序成功!\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
AddressBook address_book;
|
||||
|
||||
//声明一个函数指针类型
|
||||
typedef void (*Func)(AddressBook*);
|
||||
Func func_table[] = {
|
||||
NULL,
|
||||
AddPersonInfo,
|
||||
DelPersonInfo,
|
||||
FindPersonInfo,
|
||||
ModifyPersonInfo,
|
||||
PrintPersonInfo,
|
||||
ClearPersonInfo,
|
||||
SortPersonInfo,
|
||||
};
|
||||
//或者你也可以这么做:
|
||||
//声明一个函数类型:
|
||||
//typedef void (Func)(AddressBook*);
|
||||
//我们用的是指针数组,数组类型必须是指针类型,所以应该加上 *
|
||||
//Func* func_table[] = {
|
||||
//NULL,
|
||||
//AddPersonInfo,
|
||||
//DelPersonInfo,
|
||||
//FindPersonInfo,
|
||||
//ModifyPersonInfo,
|
||||
//PrintPersonInfo,
|
||||
//ClearPersonInfo,
|
||||
//SortPersonInfo,
|
||||
//};
|
||||
|
||||
init(&address_book);
|
||||
|
||||
|
||||
while (1) {
|
||||
int choice = Menu();
|
||||
|
||||
if (choice < 0 || choice > 7) {
|
||||
printf("输入错误!\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (choice == 0) {
|
||||
printf("再见!\n");
|
||||
break;
|
||||
}
|
||||
|
||||
func_table[choice](&address_book);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
5
Coding/Advanced_C/01 Examples/Address_Book/readme.md
Normal file
5
Coding/Advanced_C/01 Examples/Address_Book/readme.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## 简易版通讯录
|
||||
|
||||
第一版通讯录的实现是定义一个结构体里面包含一个数组。技巧不高,其实就是结构体的一个应用
|
||||
|
||||
后面可能会优化,可能不会。。
|
||||
|
Before Width: | Height: | Size: 65 KiB After Width: | Height: | Size: 65 KiB |
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
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user