Add files via upload

This commit is contained in:
Nuño Sempere 2018-04-07 15:47:23 +02:00 committed by GitHub
parent d7aae039e3
commit f7265e5c8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 159 additions and 0 deletions

9
is_prime/const.h Normal file
View File

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

BIN
is_prime/main Normal file

Binary file not shown.

51
is_prime/main.c Normal file
View File

@ -0,0 +1,51 @@
#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,8,7,7,7,2,2,2,2,9};
// Enough sevens that we can later add more 1s.
// We could have between (sqrt(m)-2) and (m-2) 7s
// But Nuño, if you put sqrt(m) -2 sevens, aren't you offloading the calculation to yourself instead making the machine calculate it?
// Yes, I am.
int longitud=length(List);
fprintf(stdout,"\nEsta función acepta cuando n es primo");
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
is_prime/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
is_prime/states.o Normal file

Binary file not shown.

85
is_prime/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
is_prime/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