~rabbits/fractran

8680f6d04c28d8689ace30323de75b01bf6fd941 — neauoire 9 months ago 5821f89
Fixed typo
2 files changed, 11 insertions(+), 11 deletions(-)

M README.md
M fractran.c
M README.md => README.md +3 -3
@@ 10,11 10,11 @@ cc fractran.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -o fractran

## HowTo

A valid input begins with a state, followed by a series of positive fractions. If the program state is 1008(2⁴ × 3² × 7), r2 has the value 4, r3 has the value 2, r7 has the value 1, and all other registers are unassigned. 
A valid input begins with the accumulator state, followed by a series of positive fractions. If the accumulator value is 1008(2⁴ × 3² × 7), r2 has the value 4, r3 has the value 2, r7 has the value 1, and all other registers are unassigned. 

## Adder Program

Adding the values store in the registers 2 and 3, and storing the result in the register 3.
Adding the values stored in the registers 2 and 3, and storing the result in the register 3.

```
3888 2/3


@@ 41,7 41,7 @@ r02=09

## Multiplication Program

Multiplying the values store in the registers 2 and 3, and storing the result in the register 5.
Multiplying the values stored in the registers 2 and 3, and storing the result in the register 5.

```
36 385/13 13/21 1/7 3/11 7/2 1/3

M fractran.c => fractran.c +8 -8
@@ 17,7 17,7 @@ typedef struct Fraction {

typedef struct Machine {
	int len;
	Fraction state, program[256];
	Fraction acc, program[256];
} Machine;

int


@@ 41,7 41,7 @@ Frac(unsigned int num, unsigned int den)
void
printstate(Machine *m)
{
	unsigned int fac = 2, num = m->state.num;
	unsigned int fac = 2, num = m->acc.num;
	printf("[%d] ", num);
	while(num > 1) {
		if(num % fac == 0) {


@@ 67,15 67,15 @@ run(Machine *m)
	int i = 0;
	while(i < m->len) {
		Fraction res, *f = &m->program[i++];
		res = Frac(m->state.num * f->num, m->state.den * f->den);
		res = Frac(m->acc.num * f->num, m->acc.den * f->den);
		printf("%u × %u/%u = %u/%u \n",
			m->state.num,
			m->acc.num,
			f->num,
			f->den,
			res.num,
			res.den);
		if(res.den == 1) {
			m->state = res;
			m->acc = res;
			printstate(m);
			i = 0;
		}


@@ 86,9 86,9 @@ void
push(Machine *m, char *w)
{
	Fraction f;
	if(!m->state.den) {
		if(sscanf(w, "%u", &m->state.num) > 0)
			m->state.den = 1;
	if(!m->acc.den) {
		if(sscanf(w, "%u", &m->acc.num) > 0)
			m->acc.den = 1;
		return;
	}
	if(sscanf(w, "%u/%u", &f.num, &f.den) > 0)