Title: ?16? ???
1?16? ??? ?????
- 16.3 ??????
- ?????
- ?????
- ???
- system??
2???????
- ?????????????????
- ????????!
- ????????????????
- ???????????????
- ????????????
316.3 ??????
????????????????????
?????? 16.3.2 ??
???
struct point double x double y void
center_point(struct point p1, struct point p2)
?2??double????????? point ??????????????????
?????
???????
???????????? center_point ?????????
p1, p2 ? point ??????????????
416.3 ??????
??????????
???????????????
516.3 ??????
??????????
???????????????
??????????
????????
616.3 ??????
????????????? ? ?????
???
??????16.3.3??
struct grade char name int subject1
int subject2 int subject3 double
average void average_of_scores(struct grade
h_p)
???????
????????? average_of_scores ?????????
h_p ? grade ????????????????????????????
716.3 ??????
3??????????????
?????? 16.3.3 ?
?????????????????? h_p ? grade ????????? ?????????
??????
include ltstdio.hgt struct grade char name
int subject1 int subject2 int subject3
double average void average_of_scores(struct
grade h_p) int main(void) struct grade
Smith "John Smith", 90, 80, 35, 0
average_of_scores(Smith) printf("name s\n",
Smith.name) printf("??1 d ??2 d ??3
d\n", Smith.subject1, Smith.subject2,
Smith.subject3) printf("?? f\n",
Smith.average) return 0
???? Smith ????????? ????(????)????
816.3 ??????
?????? 16.3.3 ?
3??????????????
???????????? h_p ? grade ????????? ???????????????
void average_of_scores(struct grade h_p) h_p
-gt average (h_p -gt subject1
h_p -gt subject2 h_p -gt
subject3) / 3.
??????? ????? "-gt" ??????????
h_p.average ? h_p -gt average
916.3 ??????
??????????? ??????1????
???????????????
?????????? p3 ???
???????????????????
1016.3 ??????
??????????? ??????1????
???????????????
????????????
??????????
?????????
11 ?????
p.183
- int rand(void)
- ???????
- ??0RAND_MAX(32767)
- ltstdlib.hgt????
- void srand(unsigned n)
- ????????(?)?????
- ltstdlib.hgt????
- time_t time(time_t timer)
- ??????????????
- ??? NULL (?????)???
- lttime.hgt????
12?????????(dice.c)
include ltstdio.hgt include ltstdlib.hgt include
lttime.hgt int main(void) int i /
??????????????????? ??????????????????????
/ srand((unsigned)time(NULL)) / ?????10???
/ for (i 0 i lt 10 i) printf("d ",
(rand() 6) 1) // 6????1 printf("\n")
return 0
13 ?????
- ??? p.180, ?A.2 ?????
- \a ????????
- \r ?????(?????????)
- \f ???????????(???????)
- \n ??(\r\f)
- \t ????
- ???
- putchar('\a')
- printf("?? d ??\r\a", i)
14?????????2(dice2.c)
include ltstdlib.hgt include ltstdio.hgt include
lttime.hgt int main(void) int i, n, m /
?????????????????????? / srand((unsigned)time(N
ULL)) printf(" ---\n") n rand() 6 m
n 8 rand() 10 for (i n i lt m i)
printf("\r d \a", i 6 1) printf("\n
---\n") return 0
n 0 5 m n8 n17
1?? alert ??200 ms ???
? ???????1.6 3.4?????
15 ???
- ????????????????????
- enum ?? ??????
- ?
- ??? 0, 1, ..., 6
enum DAYS SUNDAY, MONDAY, ..., SATURDAY
16 system??
- OS(?????????)?????????????????????
- system("cls") ??????
- include ltstdlib.hgt ???
17???????(undergnd.c)
???????
????????
g0.block 1, 0, 0, 1, 0, 0, 0, 1, 0, 1
g0.blockstr
______
2???????????????? \0 ? 21???
18???????(undergnd.c)
/ ????????? / void makeGround(struct ground
p) int i char wall " ", "" /
??? / strcpy(p-gtblockStr, wall1)
p-gtblock0 1 / ??????????? / for (i 1
i lt 8 i) p-gtblocki rand() 4 ? 0
1 // ??1/4??????? strcat(p-gtblockStr,
wallp-gtblocki) / ??? /
strcat(p-gtblockStr, wall1) p-gtblock9 1
3???????2????
""
4?????????1, ??????????0
??????????????
""
19???????(undergnd.c)
????????
int main(void) int i, score 0, x 4, key
1 struct ground g4 // ????????(4??) /
???????? / srand(time(NULL) 100) //
??????????100??? for (i 0 i lt 4 i)
makeGround(g i) / ?????? / while (key)
/ ???? / strncpy(g2.blockStr x 2,
"?", 2) for (i 0 i lt 4 i)
printf("s\n", gi.blockStr)
strncpy(g2.blockStr x 2, " ", 2)
?????????????
?????? ????????
?????? ?????
4????????
????????????????????2????????????????
20???????(undergnd.c)
????
????
????
1???????
????????
21????????(mineswpr.c)
include ltstdio.hgt include ltstdlib.hgt include
ltstring.hgt include lttime.hgt define TRUE
1 define BSIZE 5 // ?????????? define MINENUM
5 // ???? // ?????????? typedef struct int
x, y Mine enum MSTAT MISS, NEAR, HIT
static char mapBSIZEBSIZE
????????? Mine ??????
????????????MSTAT???
????????? static????????????
22????????(mineswpr.c)
// ????????? void Mine_set(Mine p) do
p-gtx rand() BSIZE p-gty rand()
BSIZE while (mapp-gtyp-gtx)
mapp-gtyp-gtx TRUE // ?????????????????? en
um MSTAT Mine_check(int cx, int cy, Mine p)
int distX, distY if (p-gtx cx p-gty
cy) return HIT distX p-gtx - cx distY
p-gty - cy if (-1 lt distX distX lt 1
-1 lt distY distY lt 1) return NEAR
return MISS
????????????????????????
????????
???????HIT???
??????NEAR???
??????MISS???
23????????(mineswpr.c)
// ???????????? int statusBSIZEBSIZE Mine
mMINENUM enum BSTAT ALREADY, OPEN, BOMB
// ???????????? void Board_init(void) int
x, y, i // ?????? for (y 0 y lt BSIZE y)
for (x 0 x lt BSIZE x) statusyx
-1 // ????? for (i 0 i lt MINENUM i)
Mine_set((mi))
?????????
????????
?????????? ???BSTAT???
?????? -1
??????????
???????
24????????(mineswpr.c)
// ???????????? enum BSTAT Board_open(int x, int
y) int i, nearCount 0 enum MSTAT ms
if (statusyx gt 0) return ALREADY for (i
0 i lt MINENUM i) ms Mine_check(x, y,
mi) if (ms HIT) return BOMB if
(ms NEAR) nearCount statusyx
nearCount return OPEN
????????
?????? 0 ?? ?ALREADY ???
?????????
???HIT??BOMB???
???NEAR???????1?????????????
NEAR?????????
??????????????? NEAR?????OPEN???
25????????(mineswpr.c)
// ???????????????? void Board_show(void) int
i, x, y printf(" ") for (i 0 i lt
BSIZE i) putchar('A' i) printf("\n")
for (y 0 y lt BSIZE y) printf("d ", y
1) for (x 0 x lt BSIZE x) if
(statusyx lt 0) printf("") else
printf("1d",statusyx)
printf("\n")
???? x ?? AE ???
y ???????
????? ???
?????? NEAR?????
26????????(mineswpr.c)
int main(void) int i, x, y, openCount 0
char charX, charY, key100 enum BSTAT bs //
?????? srand(time(NULL) 100)
Board_init() // ?????? while (TRUE)
Board_show() printf("???????????(? A1) ")
fgets(key, 100, stdin) if (strlen(key) lt
2) continue // ?????X????????? charX
key0 if (charX lt 'A' charX gt 'A'
BSIZE-1) continue x charX - 'A'
????????????
??????????
?????????
???????1???
2???????????
??????????????
27????????(mineswpr.c)
??????????????
// ?????Y????????? charY key1 if
(charY lt '1' charY gt '1' BSIZE-1) continue
y charY - '1' // ? ? if ((bs
Board_open(x, y)) BOMB)
printf("\a\a\a\a????????????!\n") break
if (bs ALREADY) continue if
(openCount BSIZEBSIZE - MINENUM)
printf("\a???????!\n") Board_show()
break
???????????
???????????
?????????-??? ?????
???????
28????????(mineswpr.c)
printf("\n") for (i0 iltMINENUM i)
printf(" cd",'A'mi.x,1mi.y) return 0
??(?????)???
status
map
A B C D E 1 -1-1-1-1-1 2 -1-1-1-1-1 3
-1-1-1-1-1 4 -1-1-1-1-1 5 -1-1-1-1-1
A B C D E 1 0 1 0 0 0 2 0 0 0 0 1 3 1 0 0 0 0 4
0 0 0 0 0 5 1 0 0 1 0
Mine m
m04,1 m11,0 m23,4 m30,4 m4
0,2
29?????????1
- ???????(undergnd.c)???????????????
- srand()????????????????????????
- ???????????????????????????????????(????????highs
cr.txt)
30?????????1????
int highscore0 FILE fp ... if ((fp
fopen("highscr.txt", "r")) ! NULL)
fscanf(fp, "d", highscore) fclose(fp)
if (score gt highscore)
printf("???????!\n") fp fopen("highscr.txt"
, "w") fprintf(fp, "d\n", score)
fclose(fp) else printf("????? d ????\n",
highscore)
31?????????2
- ????????(mineswpr.c)???????????????
- srand()????????????????????????
- ??????????????????????????????????????
- ?????????????????????