From cce889aeac3c99b17099c927430d70cf7d039f76 Mon Sep 17 00:00:00 2001 From: shubham7298 Date: Fri, 14 Dec 2018 13:56:57 +0530 Subject: [PATCH 1/3] Added scanf --- libs/sudocode_c.py | 27 +++++++++++++++++++++++++++ testfiles/code0.c | 37 +++++++++++++++++++++++++++++++++++++ testfiles/code0.txt | 24 ++++++++++++++++++++++++ testfiles/code0_c | Bin 0 -> 8480 bytes testfiles/sudocode3.c | 6 ++++-- testfiles/sudocode3.txt | 4 +++- 6 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 testfiles/code0.c create mode 100644 testfiles/code0.txt create mode 100755 testfiles/code0_c diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 688babe..2fdab34 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -86,7 +86,34 @@ def get_code(filename): break line_of_code += line_elem[i] + " " #spacing need for each word line_of_code += ");" + #Enigma goes here + elif("input" in line_elem): + + for i in range(1,len(line_elem)): #getting each word to be input + if(line_elem[i] in variables): #checking if input statement is referring to variables being scanned + line_of_code += "scanf(\"" + index_var = variables.index(line_elem[i]) #get index of that particular variable + print(index_var,"**********") + line_of_code += "%" + if(variables[index_var-1]=="int"): #checking one index before the var name to check var type in order to get right format specifier + line_of_code += "d\",&" + variables[index_var] + if(variables[index_var-1]=="float"): + line_of_code += "f\",&" + variables[index_var] + break + else: # since the variable is not initialised + line_of_code += "float " + line_elem[1] + ";" #default type is float + variables.append("float") #storing var type in stack + line_elem[1] = (line_elem[1].split("="))[0] #need to store var name so tokenising at = in order to get name of var + variables.append(line_elem[1]) #pushing var name into variables stack + + line_of_code += "\nscanf(\"" + index_var = variables.index(line_elem[i]) #get index of that particular variable + line_of_code += "%" + line_of_code += "f\",&" + variables[index_var] # if a variable is not initialized then by default it is set to float + break + line_of_code += ");" + elif("function" in line_elem): #check for functions part funcs.append(line_elem[1]) #adding func name to funcs stack return_list.append(line_elem) #storing the line_elem list vals in return_list to get return type diff --git a/testfiles/code0.c b/testfiles/code0.c new file mode 100644 index 0000000..6db0a63 --- /dev/null +++ b/testfiles/code0.c @@ -0,0 +1,37 @@ +#include +#include + + +int test_3(int a,int temp,float test) +{ + + float b=10; + test=10; + a = b/test; + return a; + +} + +int main() +{ + + float hi=20; + int a; + scanf("%d",&a); + float b; + scanf("%f",&b); + printf("%f\n",b); + printf("%f\n",hi); + int var = test_3(0,10,10); + for(int i=0; i <= 10; i++) + { + if(i%2==0) + { + printf("%d\n",i); + } + else + { + printf("not even\n"); + } + } +} \ No newline at end of file diff --git a/testfiles/code0.txt b/testfiles/code0.txt new file mode 100644 index 0000000..1c6a930 --- /dev/null +++ b/testfiles/code0.txt @@ -0,0 +1,24 @@ + +function test_3 returns int with args int a, int temp, float test + +initialise b=10 +test=10 +a = b/test +return a +endfunction + +start +initialise hi=20 +initialise int a +input a +input b +print b +print hi +call test_3 with values 0,10,10 +for int i=0 to 10 +if i%2==0 +print i +else +print not even +fi +endfor diff --git a/testfiles/code0_c b/testfiles/code0_c new file mode 100755 index 0000000000000000000000000000000000000000..d6cf44a3cf962e3f3ab6692ac77f1cfdffa44092 GIT binary patch literal 8480 zcmeHMeQaCR6~B&?Hf>YKE&T`|TRqmG!WK8_XFAjoCvo~RrKKwg3~2k}*iPf&*qQyz zl4?Mu0K$~Ait$Hn~2QBu6>%Zj-E1vb*gf~l*y0@9{h)gt_U zMqQ^!ty*KYyv-(%+T|j<7Fx@=7nJN;t8BncuVX!-5+TtVmQ^AXi)~b>QUMK+9mRTl zsb)a~x*~Lh;}~TscEpn1Nwz!5c0y-4Frm^v>Ki@1?9WX*A)_LcbgArq+lpzsMK<5- zqM|QU?7j*+er2}d6@L9J-^1EH(4Z}LyNJ9hQymgs%U&{E7R@DN7wr=R~$+wb2B{yb6tLUZdcmpy&Q^3C^?tvsmy z$7K|nfePB!09#%GUo#KhJP+PA5B?yjzrM zCbH@1wryrM8c7YRk(`wUmbD_$d(7z2J?3B}kyIm@M9LZj9vzLCgNalmnK&2+OKwO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z>Tw978a$8&YsE+LOLG%SY5nek@gbg#LoqlJ&Nj8DrEMW~v=A9iL=h9c`XGi_1+gTC zG0jU$@xQ9kc>zlq-&VUwJ||Sp3oO>UQYZR(JharsUh+Pn0mz(1_C>*3P;uHzekgRU zC^B{1cLM(iIl8njd8AN@U(PjhE)ci7P@yx7*q`Ex&{RGmzW+eX* zy0)lkSKRhLbJ@#zLTKj|?Crd>G=JpWA#|NKbMrCVQkp?`pF zy~AGe_Y-_kB6?8KMR=iq0ZLOv+L!!U{vRy=_m%l6b~1k`-={8${qgz$IEC|(CaT{i z#b2=(`VJ&c`(hr-4M;oUK #include + int test_3(int a,int temp,float test) { @@ -14,10 +15,11 @@ int test_3(int a,int temp,float test) int main() { - float a=10; float hi=20; + int a; + scanf("%d",&a); printf("%f\n",hi); - test_3(0,10,10); + int var = test_3(0,10,10); for(int i=0; i <= 10; i++) { if(i%2==0) diff --git a/testfiles/sudocode3.txt b/testfiles/sudocode3.txt index 8ff8e41..47005cd 100644 --- a/testfiles/sudocode3.txt +++ b/testfiles/sudocode3.txt @@ -1,3 +1,4 @@ + function test_3 returns int with args int a, int temp, float test initialise b=10 @@ -7,8 +8,9 @@ return a endfunction start -initialise a=10 initialise hi=20 +initialise int a +input a print hi call test_3 with values 0,10,10 for int i=0 to 10 From 4e77b5b6be59db35fae7e5a9bea6941618894561 Mon Sep 17 00:00:00 2001 From: shubham7298 Date: Fri, 14 Dec 2018 14:05:41 +0530 Subject: [PATCH 2/3] Updated readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 8a4110d..87efcb9 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ But for now, it's going to be just some code written with some common sense to h - While loop - Function definition - Function calling +- Inputting a variable - Printing of statements and variables - Initialising variables - Basic increment and decrement statements @@ -60,6 +61,21 @@ Follow the following format: Both the examples above are valid.
Mentioning the type is optional. Incase you don't mention the type, the default type of the variable is float. +-

Input a variable

+`Input` helps you input variable on console. It can only be a variable.
+The best part about this is that it automatically initialises an uninitialised variable to float and then takes the input.
+The only drawback here being that, you can input only one particular variable in one input statement for now.
+Let's see how it works! + + input + + EXAMPLE: (1) initialise x + input x + (2) input b + +
+ + -

Print

`Print` helps you print things on console. It could be statements or variables.
The best part about this application is that it understands whether you want to print a string or a variable.
From a74552fb48ad9ceaf875f8fda958e5702fd3eea3 Mon Sep 17 00:00:00 2001 From: shubham7298 Date: Wed, 19 Dec 2018 18:22:42 +0530 Subject: [PATCH 3/3] Enter message while taking input --- libs/sudocode_c.py | 11 +++++++---- testfiles/code0.c | 2 ++ testfiles/code0_c | Bin 8480 -> 0 bytes 3 files changed, 9 insertions(+), 4 deletions(-) delete mode 100755 testfiles/code0_c diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 2fdab34..bf7c936 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -86,14 +86,17 @@ def get_code(filename): break line_of_code += line_elem[i] + " " #spacing need for each word line_of_code += ");" - #Enigma goes here + elif("input" in line_elem): for i in range(1,len(line_elem)): #getting each word to be input if(line_elem[i] in variables): #checking if input statement is referring to variables being scanned - line_of_code += "scanf(\"" index_var = variables.index(line_elem[i]) #get index of that particular variable - print(index_var,"**********") + if(variables[index_var-1]=="int"): + line_of_code += "printf(\"Enter a integer: \");\nscanf(\"" + if(variables[index_var-1]=="float"): + line_of_code += "printf(\"Enter a float value: \");\nscanf(\"" + line_of_code += "%" if(variables[index_var-1]=="int"): #checking one index before the var name to check var type in order to get right format specifier line_of_code += "d\",&" + variables[index_var] @@ -106,7 +109,7 @@ def get_code(filename): line_elem[1] = (line_elem[1].split("="))[0] #need to store var name so tokenising at = in order to get name of var variables.append(line_elem[1]) #pushing var name into variables stack - line_of_code += "\nscanf(\"" + line_of_code += "\nprintf(\"Enter a float value: \");\nscanf(\"" index_var = variables.index(line_elem[i]) #get index of that particular variable line_of_code += "%" line_of_code += "f\",&" + variables[index_var] # if a variable is not initialized then by default it is set to float diff --git a/testfiles/code0.c b/testfiles/code0.c index 6db0a63..f1bf421 100644 --- a/testfiles/code0.c +++ b/testfiles/code0.c @@ -17,8 +17,10 @@ int main() float hi=20; int a; + printf("Enter a integer: "); scanf("%d",&a); float b; + printf("Enter a float value: "); scanf("%f",&b); printf("%f\n",b); printf("%f\n",hi); diff --git a/testfiles/code0_c b/testfiles/code0_c deleted file mode 100755 index d6cf44a3cf962e3f3ab6692ac77f1cfdffa44092..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8480 zcmeHMeQaCR6~B&?Hf>YKE&T`|TRqmG!WK8_XFAjoCvo~RrKKwg3~2k}*iPf&*qQyz zl4?Mu0K$~Ait$Hn~2QBu6>%Zj-E1vb*gf~l*y0@9{h)gt_U zMqQ^!ty*KYyv-(%+T|j<7Fx@=7nJN;t8BncuVX!-5+TtVmQ^AXi)~b>QUMK+9mRTl zsb)a~x*~Lh;}~TscEpn1Nwz!5c0y-4Frm^v>Ki@1?9WX*A)_LcbgArq+lpzsMK<5- zqM|QU?7j*+er2}d6@L9J-^1EH(4Z}LyNJ9hQymgs%U&{E7R@DN7wr=R~$+wb2B{yb6tLUZdcmpy&Q^3C^?tvsmy z$7K|nfePB!09#%GUo#KhJP+PA5B?yjzrM zCbH@1wryrM8c7YRk(`wUmbD_$d(7z2J?3B}kyIm@M9LZj9vzLCgNalmnK&2+OKwOv zW0}K|L`v=G>2B{ZHw3p9YnzI-4Z+RI?C#xZ#^Rayfkf7dXL@&bB-5#QZ)6}z-VO|> zQ|!&ORVym${#4<8SLr!G?_cJE2PnN~nUAXz39Phg^)<#;Xx^&^n9p64z>26+N$^x; z{mQ!QUufeec%2F!V7%5J&({iQM*I-T&rcOl8lNJbhUENN%|Agrg?fHM^N$iwA)Y^}d9oio3SO6% z_qy}1?Kkp&HO9})?(Xe5QuAZj8b_9H)MR@jJQr5p4x10^*L0F(`xux$<4DanK#gq| zjr`lz)#$=a#G(sJ_Z6lFgY|13)c*HPQq(0L_S;DD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