-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
79 lines (68 loc) · 1.74 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "seq.h"
#include "fib.h"
#include "sieve.h"
#include "rot.h"
#include "foxsays.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void error(char * msg){printf("%s\n", msg); exit(-1);}
int main (int argc, char ** argv){
char seqName[30];
seq * s = NULL;
int n;
while(scanf("%s", seqName) > 0){
s = NULL;
if(strcmp(seqName, "Fib") == 0){
int A, B;
if(scanf("%d %d %d", &A, &B, &n) == 3)
s = newFibSeq(A,B);
else
error("Could not read three integers for Fib sequence.");
} else if(strcmp(seqName, "Sieve") == 0){
int P;
if(scanf("%d %d", &P, &n) == 2)
s = newSieveSeq(P);
else
error("Could not read two integers for Sieve sequence.");
}
/* EXTEND MAIN BELOW HERE */
else if (strcmp(seqName, "Rot") == 0){
int A, B;
if(scanf("%d %d %d", &A, &B, &n) == 3){
s = newRotSeq(A,B);
}else{
error("Could not read three integers for Rot Sequence.");
}
}
else if (strcmp(seqName, "FoxSays") == 0){
token A;
token B;
if(scanf("%s %s %d", A.text, B.text, &n) == 3){
s = newFoxSaysSeq(A, B);
}else{
error("Could not read the three integers for FoxSays Sequence");
}
}
/* EXTEND MAIN ABOVE HERE */
else {
char errormsg[200];
snprintf(errormsg, 200, "No sequence defined for %s.", seqName);
error(errormsg);
}
//print first n entries in sequence.
int i;
for(i = 0; i < n; i++){
token t = s->getCurrent(s);
printf("%s", t.text);
if(i < n-1){
printf(", ");
s->gotoNext(s);
}else
printf("\n");
}
s->destroy(s);
s= NULL;
}
return 0;
}