-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_fib.c
66 lines (58 loc) · 1.67 KB
/
main_fib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "bmp.h"
#include <stdio.h>
#include <stdlib.h>
int fib(int n, int x, int y, int step, RGB b, RGB f, int w, int h, RGB* image);
double getTime(int);
int main(int argc, char** argv)
{
if (argc != 8) {
printf("Usage: ./fib n x y step width height filename\n");
return -1;
}
int n, x, y, step, width, height;
n = atoi(argv[1]);
x = atoi(argv[2]);
y = atoi(argv[3]);
step = atoi(argv[4]);
width = atoi(argv[5]);
height = atoi(argv[6]);
if (n < 3) {
printf("n must be >= 3\n");
return -2;
}
if (step < 2) {
printf("step must be >= 2\n");
return -3;
}
if (x < 0 || x > width) {
printf("x must be in [0,%d]\n", width - 1);
return -4;
}
if (y < 0 || y > height) {
printf("y must be in [0,%d]\n", height - 1);
return -5;
}
char* file = argv[7];
printf("n=%d, x,y=%d,%d, step=%d, %dX%d -> %s\n",
n, x, y, step, width, height, file);
RGB* image = malloc(sizeof(RGB) * width * height);
if (!image) {
printf("malloc failed to allocate image memory\n");
return -3;
}
RGB bcolor = {255,255,255}; /* background color */
RGB fcolor = {0,0, 0} ; /* foreground color */
double time = getTime(2);
int steps = fib(n, x, y, step, bcolor, fcolor, width, height, image);
time = getTime(2)-time;
if (time < 0.01)
printf("fib(%d) done in < 0.01 seconds\n", n);
else
printf("fib(%d) done in %.1f s, %d steps/sec\n", n, time, (int)(steps/time));
if (!steps)
printf("fib failed\n");
else
saveBMP(file, width, height, image);
free(image);
return !steps;
}