第14课。
This commit is contained in:
88
14/Exercise01.c
Normal file
88
14/Exercise01.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#define MALE false
|
||||
#define FEMALE true
|
||||
|
||||
typedef struct {
|
||||
int year;
|
||||
int month;
|
||||
int date;
|
||||
} DATE;
|
||||
|
||||
typedef struct {
|
||||
int ID;
|
||||
char name[21];
|
||||
bool sex;
|
||||
DATE birthday;
|
||||
double score;
|
||||
} STUDENT;
|
||||
|
||||
void saveStu(STUDENT students[], int count) {
|
||||
int i;
|
||||
FILE* fileObj;
|
||||
for (i = 0; i < count; i++) {
|
||||
printf("ID: ");
|
||||
scanf("%d", &students[i].ID);
|
||||
printf("Name(max 20 char): ");
|
||||
scanf("%20s", students[i].name);
|
||||
printf("Sex: (MALE-0/FEMLE-1)");
|
||||
scanf("%d", &students[i].sex);
|
||||
printf("Birthday (Year/Month/Date):");
|
||||
scanf("%d/%d/%d", &students[i].birthday.year, &students[i].birthday.month, &students[i].birthday.date);
|
||||
printf("Scores:\n");
|
||||
scanf("%lf", &students[i].score);
|
||||
}
|
||||
fileObj = fopen("studentInfos", "wb");
|
||||
if (fileObj == NULL) {
|
||||
printf("Cannot open file.\n");
|
||||
return;
|
||||
}
|
||||
if (fwrite(students, sizeof(STUDENT), count, fileObj) == 0) {
|
||||
printf("Print file error.\n");
|
||||
}
|
||||
fclose(fileObj);
|
||||
}
|
||||
|
||||
void loadStu(STUDENT students[], int count) {
|
||||
int i;
|
||||
FILE* fileObj;
|
||||
int targetID;
|
||||
fileObj = fopen("studentInfos", "rb");
|
||||
if (fileObj == NULL) {
|
||||
printf("Cannot open file.\n");
|
||||
return;
|
||||
}
|
||||
if (fread(students, sizeof(STUDENT), count, fileObj) == 0) {
|
||||
printf("Read error.\n");
|
||||
return;
|
||||
}
|
||||
fclose(fileObj);
|
||||
printf("Loaded.\n");
|
||||
printf("ID to be checked: ");
|
||||
scanf("%d", &targetID);
|
||||
for (i = 0; i < count; i++) {
|
||||
if (targetID == students[i].ID) {
|
||||
printf("ID:%d; Name: %s; Sex:%s; Birthday:%d/%d/%d; Score:%lf\n", students[i].ID, students[i].name, students[i].sex?"FEMALE":"MALE", students[i].birthday.year, students[i].birthday.month, students[i].birthday.date, students[i].score);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
STUDENT students[10];
|
||||
int i = 0;
|
||||
int count = 3;
|
||||
char mode;
|
||||
// freopen("fakedata","r",stdin);
|
||||
printf("Select: (S)ave/(L)oad ");
|
||||
scanf("%c", &mode);
|
||||
if (mode == 's' || mode == 'S') {
|
||||
saveStu(students, count);
|
||||
}
|
||||
else if (mode == 'l' || mode == 'L') {
|
||||
loadStu(students, count);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
31
14/Exercise02.c
Normal file
31
14/Exercise02.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
char toUpper(char input) {
|
||||
if (input >= 'a') {
|
||||
return input - 'a' + 'A';
|
||||
}
|
||||
else {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
char ch;
|
||||
FILE* fl;
|
||||
fl = fopen("upper.txt", "w+");
|
||||
while (1) {
|
||||
ch = getchar();
|
||||
if (ch == '#') {
|
||||
break;
|
||||
}
|
||||
fprintf(fl, "%c", toUpper(ch));
|
||||
}
|
||||
fflush(fl);
|
||||
// fclose(fl);
|
||||
// fl = fopen("upper.txt", "r");
|
||||
rewind(fl);
|
||||
while(!feof(fl)) {
|
||||
putchar(fgetc(fl));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
35
14/Exercise03.c
Normal file
35
14/Exercise03.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
char filename[81];
|
||||
FILE* fl;
|
||||
char string[81];
|
||||
|
||||
if (argc < 2) {
|
||||
printf("Insufficient argument\n");
|
||||
return 0;
|
||||
}
|
||||
else if (argc > 2) {
|
||||
printf("Too many arguemtns\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
strncpy(filename, argv[1], 80);
|
||||
|
||||
fl = fopen(filename, "r");
|
||||
|
||||
if (fl == NULL) {
|
||||
printf("Failed to open file.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fscanf(fl, "%80s", string);
|
||||
|
||||
fclose(fl);
|
||||
printf("Content: %s\nStringLength: %d", string, strlen(string));
|
||||
|
||||
return 0;
|
||||
}
|
||||
60
14/fakedata
Normal file
60
14/fakedata
Normal file
@@ -0,0 +1,60 @@
|
||||
s
|
||||
345
|
||||
aaa
|
||||
0
|
||||
2020/3/4
|
||||
3.5
|
||||
|
||||
465
|
||||
bbb
|
||||
1
|
||||
2020/3/5
|
||||
4.5
|
||||
|
||||
933
|
||||
ddd
|
||||
0
|
||||
2020/4/6
|
||||
5
|
||||
|
||||
9688
|
||||
dadg
|
||||
0
|
||||
2020/12/6
|
||||
6.8
|
||||
|
||||
33
|
||||
adfidadff
|
||||
0
|
||||
2020/4/6
|
||||
7.9
|
||||
|
||||
34555
|
||||
aaa4wt
|
||||
0
|
||||
2020/3/4
|
||||
2.5
|
||||
|
||||
463
|
||||
bbberr
|
||||
1
|
||||
2020/3/5
|
||||
6.5
|
||||
|
||||
93364
|
||||
dddade
|
||||
0
|
||||
2020/4/6
|
||||
8
|
||||
|
||||
9688243
|
||||
dadgjj
|
||||
0
|
||||
2020/12/6
|
||||
10
|
||||
|
||||
33345
|
||||
adfidafd
|
||||
0
|
||||
2020/4/6
|
||||
9
|
||||
BIN
14/studentInfos
Normal file
BIN
14/studentInfos
Normal file
Binary file not shown.
1
14/upper.txt
Normal file
1
14/upper.txt
Normal file
@@ -0,0 +1 @@
|
||||
ADEIKKDKEI
|
||||
17
test.c
Normal file
17
test.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
int a = 4;
|
||||
|
||||
int main() {
|
||||
int s = a, i = 0;
|
||||
printf("%d\n", s);
|
||||
for (;i < 2; i++) {
|
||||
s += f(i);
|
||||
}
|
||||
printf("%d\n%d\n%d", s, f(0), f(1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// printf 从右往左压栈
|
||||
// (..., ..., ...)括号里的叫逗号表达式,优先级比赋值还低
|
||||
// 挨个运算所有的表达式,整个逗号表达式的值是最后一个语句的值
|
||||
// (*p)[5] 5个指针的数组 *p[5]一个行长为5的行指针
|
||||
Reference in New Issue
Block a user