问题
1
模拟一个自动寻路机器人。
假设有一个10*10的网格地图,最左上角的坐标为(0,0),最右下角为(9,9),在左上角(0,0)坐标处生成机器人,机器人只有两个动作:向下走down()和向右走right()。每次随机生成一个终点(h,w),打印机器人从(0,0)走到(h,w)的路线(自己决定机器人的行走路线)。
例如:
随机生成的终点是(2,3)
输出(0,0),(0,1),(0,2),(0,3),(1,3),(2,3)
要求:
1)向右走写成一个函数放在right.c文件里,向下走写成一个函数放在down.c文件里,决定机器人下一步怎么走写成一个函数放在step.c文件里,打印最后结果写成main函数放在main.c文件里。
2)写一个makefile文件编译上面的程序(不写makefile为0分)。
解
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "step.h"
#include "types.h"
const int MAX_COORD = 10;
void walk(const struct coord _Src, const struct coord _Dest) {
struct coord c = _Src;
do {
printf("(%d, %d)\n", c.x, c.y);
} while (nextstep(&c, &_Dest) == FALSE);
}
int main() {
srand(time(NULL));
struct coord s = {
0, 0 };
struct coord d = {
rand() % MAX_COORD, rand() % MAX_COORD };
printf("Source: (%d, %d)\n", s.x, s.y);
printf("Destination: (%d, %d)\n", d.x, d.y);
walk(s, d);
return 0;
}
types.h
#pragma once
#define FALSE 0
#define TRUE 1
struct coord {
int x, y;
};
right.h
right.c
#pragma once
#include "types.h"
void right(struct coord* const CurrentPos);
#include "right.h"
void right(struct coord* const CurrentPos) {
++CurrentPos->x;
}
down.h
down.c
#pragma once
#include "types.h"
void down(struct coord* const CurrentPos);
#include "down.h"
void down(struct coord* const CurrentPos) {
++CurrentPos->y;
}
step.h
step.c
#pragma once
#include "types.h"
int nextstep(struct coord* const CurrentPos, const struct coord* const Dest);
#include "down.h"
#include "right.h"
#include "step.h"
int nextstep(struct coord* const CurrentPos, const struct coord* const Dest) {
if (CurrentPos->x < Dest->x) {
right(CurrentPos); return FALSE; }
else if (CurrentPos->y < Dest->y) {
down(CurrentPos); return FALSE; }
else return TRUE;
}
Makefile
main: main.o right.o down.o step.o
main.o: main.c step.h types.h
cc -c main.c
right.o: right.c right.h
cc -c right.c
down.o: down.c down.h
cc -c down.c
step.o: step.c down.h right.h
cc -c step.c
clean:
rm main main.o right.o down.o step.o
运行结果
问题
2
修改第一题,现在机器人随机生成在(x,y)坐标处(与终点可以重合),并且该机器人增加了两个动作:向左走left()和向上走up()。
能否写一个makefile,不修改就可以编译第1题和第2题?
要求:
1)向左走写成一个函数放在left.c文件里,向上走写成一个函数放在up.c文件里
解
以下仅列出需要修改的文件。
main.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "step.h"
#include "types.h"
const int MAX_COORD = 10;
void walk(const struct coord _Src, const struct coord _Dest) {
struct coord c = _Src;
do {
printf("(%d, %d)\n", c.x, c.y);
} while (nextstep(&c, &_Dest) == FALSE);
}
int main() {
srand(time(NULL));
struct coord s = {
rand() % MAX_COORD, rand() % MAX_COORD };
struct coord d = {
rand() % MAX_COORD, rand() % MAX_COORD };
printf("Source: (%d, %d)\n", s.x, s.y);
printf("Destination: (%d, %d)\n", d.x, d.y);
walk(s, d);
return 0;
}
step.c
#include "up.h"
#include "left.h"
#include "down.h"
#include "right.h"
#include "step.h"
int nextstep(struct coord* const CurrentPos, const struct coord* const Dest) {
if (CurrentPos->x > Dest->x) {
left(CurrentPos); return FALSE; }
else if (CurrentPos->x < Dest->x) {
right(CurrentPos); return FALSE; }
else if (CurrentPos->y > Dest->y) {
up(CurrentPos); return FALSE; }
else if (CurrentPos->y < Dest->y) {
down(CurrentPos); return FALSE; }
else return TRUE;
}
left.h
left.c
#pragma once
#include "types.h"
void left(struct coord* const CurrentPos);
#include "left.h"
void left(struct coord* const CurrentPos) {
--CurrentPos->x;
}
up.h
up.c
#pragma once
#include "types.h"
void up(struct coord* const CurrentPos);
#include "up.h"
void up(struct coord* const CurrentPos) {
--CurrentPos->y;
}
若要求用单一的makefile编译第1题和第2题,则要将如下的makefile分别放入第1题和第2题的各个头文件与源文件所在的目录下,然后传入不同的target参数编译并链接指定的文件。
exp2-2: main.o right.o down.o up.o left.o step.o
cc -o main $(intermediates-exp2-2)
exp2-1: main.o right.o down.o step.o
cc -o main $(intermediates-exp2-1)
intermediates-exp2-2 = main.o right.o down.o up.o left.o step.o
intermediates-exp2-1 = main.o right.o down.o step.o
main.o: main.c step.h types.h
cc -c main.c
right.o: right.c right.h
cc -c right.c
down.o: down.c down.h
cc -c down.c
up.o: up.c up.h
cc -c up.c
left.o: left.c left.h
cc -c left.c
step.o: step.c down.h right.h
cc -c step.c
clean-exp2-2:
rm main $(intermediates-exp2-2)
clean-exp2-1:
rm main $(intermediates-exp2-1)
运行结果
参考资料
https://www.gnu.org/software/make/manual/html_node/Introduction.html