mirror of
https://github.com/hairrrrr/C-CrashCourse.git
synced 2026-04-09 22:08:18 +08:00
2-13
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#ifndef _ARRAY_H_
|
||||
#define _ARRAY_H_
|
||||
|
||||
#define BLOCK_SIZE 20
|
||||
|
||||
typedef struct {
|
||||
int* array;
|
||||
int size;
|
||||
}Array;
|
||||
|
||||
Array array_creat(int init_size);//初始化数组
|
||||
void array_free(Array* a);//释放堆上空间
|
||||
int array_size(const Array* a);//查看数组的大小
|
||||
int* array_at(Array* a, int index);
|
||||
void array_inflate(Array* a, int more_size);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,108 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include"Array.h"
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
//typedef struct {
|
||||
// int* array;
|
||||
// int size;
|
||||
//}Array;
|
||||
|
||||
Array array_creat(int init_size) {
|
||||
|
||||
Array a;
|
||||
|
||||
a.array = (int*)malloc(sizeof(int) * init_size);
|
||||
a.size = init_size;
|
||||
|
||||
return a;
|
||||
}
|
||||
//为何不传入指针?
|
||||
//比如:Array array_creat(Array* a, int init_size)
|
||||
//问题如下:
|
||||
//1.如果a == NULL?
|
||||
//2.如果a指向的是已经malloc过的空间,那样还要再free一次
|
||||
|
||||
void array_free(Array* a) {
|
||||
|
||||
free(a->array);
|
||||
a->array = NULL;//防止此函数被调用两次。free(NULL)是可以的
|
||||
a->size = 0;
|
||||
}
|
||||
|
||||
int array_size(const Array* a) {
|
||||
|
||||
return a->size;
|
||||
}
|
||||
|
||||
int* array_at(Array* a, int index) {
|
||||
|
||||
if (index >= a->size)
|
||||
array_inflate(&a, (index / BLOCK_SIZE + 1) * BLOCK_SIZE - a->size);
|
||||
//为什么不直接加上一个 BLOCK_SIZE?而是用 index / BLOCK_SIZE + 1) * BLOCK_SIZE - a->size) 这么复杂的公式?
|
||||
//这样加强了程序的可变性,如果某一次数组增加的大小你不清楚,比如从 100 增加到 150
|
||||
//直接加 BLOCL_SIZE 可能并不够用,用着个公式增加的大小一定 大于等于 需要增加的大小。
|
||||
//为什么引入 BLOCKJ_SIZE ?
|
||||
//一方面 增加了程序的扩展性(可以方便更改)和可读性,另一方面
|
||||
//如果你设置的 BLICK_SIZE 过大,数组大小这时就增加 1,这样太浪费内存
|
||||
//如果设置的过小就不能数组满足大幅的更改
|
||||
//拿本题 BLOCK_SIZE = 20 为例,一次最多多增加 19 个 int 的大小,这也就意味着
|
||||
//这种方法浪费的内存最多只有 19*sizeof(int) 个字节
|
||||
|
||||
return &(a->array[index]);
|
||||
}
|
||||
|
||||
////输出数组元素
|
||||
//int array_get(const Array a, int index) {
|
||||
//
|
||||
// return a.array[index];
|
||||
//}
|
||||
////修改数组元素
|
||||
//int array_set(Array* a, int index, int value) {
|
||||
//
|
||||
// return (a->array[index] = value);
|
||||
//}
|
||||
|
||||
void array_inflate(Array* a, int more_size) {
|
||||
|
||||
int* p = (int*)malloc(sizeof(int) * (a->size + more_size));
|
||||
int i = 0;
|
||||
|
||||
//可换成memcpy
|
||||
for (i = 0; i < a->size; i++) {
|
||||
p[i] = a->array[i];
|
||||
}
|
||||
free(a->array);
|
||||
|
||||
a->array = p;
|
||||
a->size += more_size;
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
|
||||
Array a;
|
||||
int size = 0;
|
||||
|
||||
printf("初始化数组大小,请输入:\n");
|
||||
scanf("%d", &size);
|
||||
|
||||
a = array_creat(size);
|
||||
|
||||
printf("%d members in array\n", array_size(&a));
|
||||
//为什么不直接用 a.size 输出数组的大小?
|
||||
//这样用函数来返回数组大小的方式就叫做 封装
|
||||
//日后如果有版本更新,算法优化等等,可以用函数来实现,将实现的代码保护起来
|
||||
|
||||
//array_at(&a, 1) 返回 &(a.array[1]) 解引用即使这个变量 array[1]
|
||||
//当然这个函数可以拆开,实现在array_at 后
|
||||
*array_at(&a, 1) = 10;
|
||||
printf("a.array[1] = %d\n", *array_at(&a, 1));
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef _LINK_LIST_
|
||||
#define _LINK_LIST_
|
||||
|
||||
typedef struct _node {
|
||||
|
||||
char Name[20];
|
||||
char Gender[5];
|
||||
int Age;
|
||||
long long ID;
|
||||
struct _node* next;
|
||||
}node;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include"Array.h"
|
||||
|
||||
//typedef struct _node {
|
||||
//
|
||||
// char name[20];
|
||||
// char gender[5];
|
||||
// int age;
|
||||
// long long ID;
|
||||
//}node;
|
||||
|
||||
int main(void) {
|
||||
|
||||
node* head = NULL;
|
||||
int count = 1;
|
||||
int Exit = 0;
|
||||
|
||||
do {
|
||||
printf("是否继续?输入 0 退出\n");
|
||||
scanf("%d", &Exit);
|
||||
|
||||
if (Exit) {
|
||||
printf("请输入第 %d 个学生信息\n", count++);
|
||||
node* p = (node*)malloc(sizeof(node));
|
||||
printf("\t\t姓名: ");
|
||||
scanf("%s", p->Name);
|
||||
printf("\t\t性别: ");
|
||||
scanf("%s", p->Gender);
|
||||
printf("\t\t年龄: ");
|
||||
scanf("%d", &(p->Age));
|
||||
printf("\t\t学号: ");
|
||||
scanf("%lld", &(p->ID));
|
||||
|
||||
p->next = NULL;
|
||||
|
||||
node* last = head;//创建新的节点需要寻找最后一个放 NULL 的节点
|
||||
|
||||
//如果表头是空指针
|
||||
if (last) {
|
||||
//节点 指针域为NULL 找到了
|
||||
while (last->next)
|
||||
last = last->next;
|
||||
last->next = p;
|
||||
}
|
||||
else
|
||||
head = p;
|
||||
}
|
||||
} while (Exit);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
翁恺MOOC
|
||||
|
||||
main 函数中的链表创建
|
||||
@@ -0,0 +1,2 @@
|
||||
翁恺老师 MOOC
|
||||
|
||||
195
C Crash Course/21 Input&Output/prelesson/test code/test.c
Normal file
195
C Crash Course/21 Input&Output/prelesson/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