Add files via upload

This commit is contained in:
Nuño Sempere 2018-04-07 15:44:56 +02:00 committed by GitHub
parent ffd90ab058
commit 74badf524e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 155 additions and 0 deletions

9
divisor 1.1/const.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __CONST_H_
#define __CONST_H_
#define REJECT -1
#define ACCEPT -2
#endif

BIN
divisor 1.1/main Normal file

Binary file not shown.

47
divisor 1.1/main.c Normal file
View File

@ -0,0 +1,47 @@
#include <stdlib.h>
#include <stdio.h>
#include "turing.h"
#include "states.h"
#include "const.h"
#define REJECT -1
#define ACCEPT -2
// gcc -g main.c turing.c states.c turing.h states.h const.h -o main
int main(){
int List[] = {0,5,1,1,8,2,2,2,2,2,9};
int longitud=length(List);
fprintf(stdout,"\nEsta función acepta cuando n no divide a m, o cuando n=m");
int state=0;
int position=0;
int symbol= List[position];
int movement =0; // undefined.
int placeholder;
while(state!=ACCEPT && state!=REJECT){
print_linea();
print_variables(state,position,symbol, movement);
print_lista(List, longitud, position);
fscanf(stdin,"\n%d", &placeholder);
carry_out_step(&symbol,&state,&movement);
List[position]=symbol;
position = position +movement;
symbol=List[position];
}
print_linea();
print_variables(state,position,symbol, movement);
print_lista(List, longitud, position);
}

6
divisor 1.1/states.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __STATES_H_
#define __STATES_H_
void carry_out_step(int *symbol, int *state, int *movement); // This is the only function I'm going to need to use outside of states.c
#endif

BIN
divisor 1.1/states.o Normal file

Binary file not shown.

85
divisor 1.1/turing.c Normal file
View File

@ -0,0 +1,85 @@
#include <stdlib.h>
#include <stdio.h>
#include "turing.h"
#include "const.h"
// Turing print defines functions that take a list, take a position in that list and print the list with a pointer to that position.
// It prints this vertically
int length(int *List){
int i =0;
while (List[i]<9){ // Por cómo construimos nuestras listas.
i=i+1;
}
/*Anecdóticamente, el siguiente código da error.
i=0;
while (List[i]<=9){
i=i+1;
}
Y lo hace porque al asignar un trozo de memoria,
si la lista tiene, p.ej., longitud 6,
List[0] hasta Lista[5] son la lista de verdad,
pero Lista[6] <9, y la sigue contando.
*/
return i+1;
}
void print_lista(int*L, int length, int position){
if(L==NULL || length <= position){
fprintf(stdout, "\nError en prettyprint");
}
else{
int i=0;
char tabs[]="\t\t\t\t\t\t\t\0";
for(i=0; i<length; i++){
if(i==position){
fprintf(stdout, "\n%s{",tabs);
}
else{
fprintf(stdout, "\n%s",tabs);
}
if( L[i]==0){
fprintf(stdout, "T");
}
else if(L[i]==8){
fprintf(stdout, "X");
}
else if(L[i]==9){
fprintf(stdout, "W");
}
else{
fprintf(stdout, "%d",L[i]);
}
if(i==position){
fprintf(stdout, "}\n");
}
else{
fprintf(stdout, "\n");
}
}
}
}
void print_variables(int state, int position, int symbol, int movement){
char tabs[]="\t\t\t\t\t\t\t\0";
fprintf(stdout,"\n%sState =%d\n",tabs,state);
fprintf(stdout,"\n%sPosition =%d\n",tabs,position);
fprintf(stdout,"\n%sSymbol =%d\n",tabs,symbol);
fprintf(stdout,"\n%sMovement =%d\n\n",tabs,movement);
}
void print_linea(){
char tabs[]="\t\t\0";
fprintf(stdout,"\n%s__________________________________________________________________________________________\n",tabs);
}

8
divisor 1.1/turing.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef __CONST_H_
#define __CONST_H_
void print_lista(int*L, int length, int position);
void print_variables(int state, int position, int symbol, int movement);
int length(int *List);
void print_linea();
#endif