From 9339c5faae5749d53444fb19e137a8dec7f777c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C3=B1o=20Sempere?= Date: Fri, 6 Apr 2018 21:52:54 +0200 Subject: [PATCH] Add files via upload --- divisor 0.1/main | Bin 0 -> 19472 bytes divisor 0.1/main.c | 41 +++++++++ divisor 0.1/states.c | 206 +++++++++++++++++++++++++++++++++++++++++++ divisor 0.1/states.h | 6 ++ divisor 0.1/states.o | Bin 0 -> 2576 bytes divisor 0.1/turing.c | 59 +++++++++++++ divisor 0.1/turing.h | 6 ++ 7 files changed, 318 insertions(+) create mode 100644 divisor 0.1/main create mode 100644 divisor 0.1/main.c create mode 100644 divisor 0.1/states.c create mode 100644 divisor 0.1/states.h create mode 100644 divisor 0.1/states.o create mode 100644 divisor 0.1/turing.c create mode 100644 divisor 0.1/turing.h diff --git a/divisor 0.1/main b/divisor 0.1/main new file mode 100644 index 0000000000000000000000000000000000000000..2a42483500a9df1afd0c39bbf4e20de0fc7381e7 GIT binary patch literal 19472 zcmeHP4Rlo1oxg7;A8!auzCZ(Lc`7U)nn@x7p$JSM0~0oU6cS}o`ZAfBB%{epoDT#& z6l%mWCsM=iw$d&VZK<_Yc4<$$%c=`uaS5J!w&@;i>DIQit8OB67nRbqwPb()`!Sh$ znb?oh^>p`ebLRc;|MULu|K9uFz4yJFU7q??HcewH+1N5hSW%vvlf)JHN{&j}BIaOj zHj~}J@<7SO&&@dmMNYwCX<9*D%Gm*P@LOaPghe(t&nS4EATtG(hXkp-(d3p{0cTDY zsDg|coIrZyYtctI3fROm3VNj9q)SDyq6daI_DX8j4b)#Q{wAWyB10&o` zfYtKrC?r`*8{UiuDDN)jD4?5X6ns>WnSx61W6&c%f7etb1vkp_#*52ESw96O$|~AJ z%?qn5+5_e7p>Se*`SwK%%NJI=ViDJ^yxwG=WLK|imln>mR+Ywb7@l0ZL}709uks;#k2r z^tt2E*NsD8H4eQAbOS$YS`I=se{KZ5m`!6%MiW<2{fGKJ(K{`4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N~Qu z2Ng;Ek_k#uKvk}ye$k?@*3mPiphF2tD8rhfensl5_EYn`N~u4|(&4Q2`I<#v{ofXM zu_)rvR7oOZE&qf?U#;H_<|9@vd#$R@aRR2-^;DDukwGS~Ym-JSD z6|UfaTl7==6$ThcUI9{9rKjL9IO+9Q>kzLYRFt2}fYMX=6=aB}>fdBEan@#o115n$ zaJAZ_)9+M%WlBP1lzz2^PO_8~oGA4Lb@ckEA@V(=B%V`xiadQB{p)TJ5HGwxJ#7dmMU`AoP|PSuk0{40VR^^KkE5O_qS~2A6+K&$Maus8T%h)kq`X< literal 0 HcmV?d00001 diff --git a/divisor 0.1/main.c b/divisor 0.1/main.c new file mode 100644 index 0000000..802ede1 --- /dev/null +++ b/divisor 0.1/main.c @@ -0,0 +1,41 @@ +#include +#include +#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,1,1,8,2,2,9}; + int longitud=7; + int state=0; + int position=0; + int symbol= List[position]; + int movement =0; // undefined. + int placeholder; + + + while(state!=ACCEPT && state!=REJECT){ + + print_lista(List, longitud, position); + print_variables(state,position,symbol, movement); + fscanf(stdin,"\n%d", &placeholder); + + carry_out_step(&symbol,&state,&movement); + + if(state!=ACCEPT && state!=REJECT){ + List[position]=symbol; + position = position +movement; + symbol=List[position]; + } + + } + + + +} diff --git a/divisor 0.1/states.c b/divisor 0.1/states.c new file mode 100644 index 0000000..4eda922 --- /dev/null +++ b/divisor 0.1/states.c @@ -0,0 +1,206 @@ +#include +#include +#include "const.h" +#include "states.h" + +// States of a Turing Machine that checks whether a number DOESN'T divide another number +// NTS: Check that the first number is smaller? Actually not necessary: see state 2. + +/* +Initially, I defined a triplet, which was what the different states, which are functions returned. + +typedef struct { + + int state; // Finite list of states: initial (0), state 1, state 2, state 3, ..., state 7. + int symbol; // Finite list of symbols: 0,1,2,3,4,5,6,7,8,9 + int movement; // Left = -1, Right =1; + +} ssm; + +But I thought that modifying pointers was easier and more elegant. +*/ + +void state0(int *symbol, int *state, int *movement); +void state1(int *symbol, int *state, int *movement); +void state2(int *symbol, int *state, int *movement); +void state3(int *symbol, int *state, int *movement); +void state4(int *symbol, int *state, int *movement); +void state5(int *symbol, int *state, int *movement); +void state6(int *symbol, int *state, int *movement); +void state7(int *symbol, int *state, int *movement); + +// This function is the delta of which Kozen speaks of in p.210 &ss in Automata and Computability. +void carry_out_step(int *symbol, int *state, int *movement){ // I could make this a bigger if/else tree, but I don't want to. +// Initially, I thought to use https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work, but this proved unnecessary. + + if(*state==0){ + state0(symbol,state, movement); + } + else if(*state==1){ + // Nótese que los else ifs son necesarios, porque después de state0, *state es =1, pero queremos que se de cuenta en la siguiente vuelta + state1(symbol,state, movement); + } + else if(*state==2){ + state2(symbol,state, movement); + } + else if(*state==3){ + state3(symbol,state, movement); + } + else if(*state==4){ + state4(symbol,state, movement); + } + else if(*state==5){ + state5(symbol,state, movement); + } + else if(*state==6){ + state6(symbol,state, movement); + } + else if(*state==7){ + state7(symbol,state, movement); + } + +} + +void state0(int *symbol, int *state, int *movement){ + if(*symbol ==0){ + *symbol=0; // This is the left endmarker. It isn't changed. This line could be ommited. + *movement= 1; // Move to the right (0 would be to the left). + *state=1; // Change to state 1. + } + + else{ + fprintf(stdout,"\nEn state0, REJECT"); + *state = REJECT; // This is defined as a constant in const.h + // The program ends, because it has unspecified behaviour + } + +} + +void state1(int *symbol, int *state, int *movement){ + if(*symbol ==1){ + *symbol=3; // Another symbol. + *movement= 1; // Move to the right (-1 would be to the left). + *state=2; // Change to state 2. + } + + else{ + fprintf(stdout,"\nEn state1, REJECT"); + *state = REJECT; // This is defined as a constant in const.h + // The program ends, because it has unspecified behaviour + } +} + +void state2(int *symbol, int *state, int *movement){ + + if(*symbol ==2){ + *symbol=4; // You get the idea. + *movement= -1; + *state=3; + } + + else if(*symbol ==9){ + *state=ACCEPT; + fprintf(stdout,"\nIt accepts because the first number doesn't divide the second"); + } + + else{ + *symbol=*symbol; // redundant + *movement= 1; + *state=2; + } +} + +void state3(int *symbol, int *state, int *movement){ + + if(*symbol ==0){ + *symbol=0; + *movement= 1; + *state=4; + } + + else { + *symbol=*symbol; + *movement = -1; + *state= 3; + } +} + +void state4(int *symbol, int *state, int *movement){ + + if(*symbol ==3){ + *symbol=3; + *movement= 1; + *state=4; + } + + else if(*symbol ==1){ + *symbol=1; + *movement= -1; + *state=5; + } + + else if(*symbol ==8){ + *symbol=8; + *movement= 1; + *state=6; + } + + else{ + *state = REJECT; // This is defined as a constant in const.h + // The program ends, because it has unspecified behaviour + } +} + +void state5(int *symbol, int *state, int *movement){ +// This state comes to be because at the end of option symbol ==1 in state 4, I want to stay on the same place, but have to move. + *symbol =*symbol; + *movement = 1; + *state = 1; +} + +void state6(int *symbol, int *state, int *movement){ + + if(*symbol ==2){ + *symbol=4; + *movement=-1; + *state=7; + } + + else if(*symbol ==9){ + *symbol=9; + *movement=-1; + *state = REJECT; + fprintf(stdout,"\nIt rejects because the first number DOES divide the second\n");// The number does divide. + } + + else{ + *symbol = *symbol; + *movement =1; + *state=6; + } + +} + +void state7(int *symbol, int *state, int *movement){ + + if(*symbol ==3){ + *symbol=1; + *movement=-1; + *state=7; + } + + else if(*symbol ==0){ + *symbol=0; + *movement= 1; + *state = 1; + } + + else{ + *symbol = *symbol; + *movement =-1; + *state=7; + } + +} + + diff --git a/divisor 0.1/states.h b/divisor 0.1/states.h new file mode 100644 index 0000000..625b918 --- /dev/null +++ b/divisor 0.1/states.h @@ -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 diff --git a/divisor 0.1/states.o b/divisor 0.1/states.o new file mode 100644 index 0000000000000000000000000000000000000000..1e33186fbb83ecb042c3b8d3801d7260980bfe15 GIT binary patch literal 2576 zcmbtV&ubG=5T0$UHfgI>R0I`+TB)E*VzpRBiI%mCh)6NiTi7-mTQrGm-eUa?7KIoK zf|nk>dG)9sv?^M}gFW=ne?YLIZvtE@fA*%~bvAYIZ7b*Os++AjPg)^XUnD^x6!F zrkXW<(3-ls&8{_)w_SblbRCY*DAsdAK2kU9;Cj-Uqxq!uIgVDCw7$mUZ>(<`pG_mD zX3e3Tzgf$TWj{Y7C%ZcNm`16FBzxQ*TYF1BsaZ3&F;#4G<05|C)GTuD%WTJ~pT-!J?2d;*}ir{_SjBRk-igrD4J?ln`pyL`Dy zUr%?Lsl8_R&HzvDNu|3|U8!z5jgE_m4+0Sn&r&Kn$Yy8w2(+DNoEK ztYQ0xx)Bd=MKZU&i#;%&hrDt=Q^($-ik4G{*x*`<$>BSGb@*h!WjUwBD`Q6 z(lgIJ@0h|J5n$#W;ea_%setK~gT-LESak6sGD~IQn!P8pyJZz3f!y4oT)}gS?yori zIZ2{Fy;}GS%^*gmzsq11aP^$Vjo9e+$YA4^QCCWvN($9J#nCs!21VYX@bJyl{97^= zS#cLv0K0HOn*ZvbR+`~(I{rQ$OmR6xmpi>zii0@MD8YLEs+{1c@(;O#AKs86Xg|&G zSN>u8hywO&q8fGnHQa}jsPj9e3PPU~ogevXe~qt3ES!6UU`Y8pv|twAKdr-iC;B4= ao>C6 +#include +#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 + +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"; + for(i=0; i