-
-
Save anonymous/e6b33a31a0b76b7a759a to your computer and use it in GitHub Desktop.
solution for http://www.reddit.com/r/dailyprogrammer/comments/2z68di/20150316_challenge_206_easy_recurrence_relations/ by erxyi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#include <stdbool.h> | |
#include <string.h> | |
size_t bufsize = 128; | |
typedef enum {NOTHING, ADD, SUBSTRACT, DIVIDE, MULTIPLY} | |
Operation; | |
struct Atom | |
{ | |
struct Atom* next; | |
Operation mode; | |
double value; | |
}; | |
struct Atom* parse(char *buffer) | |
{ | |
struct Atom* head, *next; | |
bool valueSet = false,negative=false; | |
char *tempbuf = malloc(bufsize+1); | |
head = malloc(sizeof(struct Atom)); | |
next = head; | |
head->next = NULL; | |
head->mode = NOTHING; | |
int i = 0; | |
int start = 0; | |
while(buffer[i]>=' ') | |
{ | |
if(buffer[i]==' ') continue; | |
switch(buffer[i]) | |
{ | |
case '+': | |
next->mode = ADD; | |
break; | |
case '-': | |
if(next->mode==NOTHING) | |
next->mode = SUBSTRACT; | |
else | |
negative = true ^ negative; | |
puts("minus"); | |
break; | |
case '*': | |
next->mode = MULTIPLY; | |
break; | |
case '/': | |
next->mode = DIVIDE; | |
break; | |
//let's look for a real number | |
default: | |
start = i; | |
while(isdigit(buffer[i]) || buffer[i]=='.') i++; | |
memmove(tempbuf, buffer+start, i-start); | |
next->value = atof(tempbuf); | |
if(negative) next->value*=-1; | |
memset(tempbuf, 0, bufsize+1); | |
valueSet = true; | |
negative = false; | |
} | |
i++; | |
if(next->mode != NOTHING && valueSet) | |
{ | |
next->next = malloc(sizeof(struct Atom)); | |
next = next->next; | |
next->value = 0; | |
next->next = NULL; | |
next->mode = NOTHING; | |
valueSet = false; | |
} | |
} | |
free(tempbuf); | |
return head; | |
} | |
double travel(struct Atom *where, double value) | |
{ | |
if(where->mode == NOTHING || where->next == NULL) return value; | |
switch(where->mode) | |
{ | |
case ADD: | |
return travel(where->next, value+where->value); | |
case SUBSTRACT: | |
return travel(where->next, value-where->value); | |
case MULTIPLY: | |
return travel(where->next, value*(where->value)); | |
case DIVIDE: | |
return travel(where->next, value/(where->value)); | |
} | |
} | |
int main() | |
{ | |
char *buffer =(char*) malloc(bufsize + 1); | |
float value = 0.0; | |
int steps = 0, i = 0; | |
getline(&buffer, &bufsize, stdin); | |
scanf("%f", &value); | |
scanf("%d", &steps); | |
struct Atom *recipe = parse(buffer), *cleaner; | |
for(i = 1; i <= steps; i++) | |
printf("%d Term: %f\n", i,value = travel(recipe, value)); | |
free(buffer); | |
while(recipe!=NULL) | |
{ | |
cleaner = recipe; | |
recipe = recipe->next; | |
free(cleaner); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment