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.
diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 688babe..bf7c936 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -86,7 +86,37 @@ def get_code(filename): break line_of_code += line_elem[i] + " " #spacing need for each word line_of_code += ");" + + 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 + index_var = variables.index(line_elem[i]) #get index of that particular variable + 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] + 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 += "\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 + 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..f1bf421 --- /dev/null +++ b/testfiles/code0.c @@ -0,0 +1,39 @@ +#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; + 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); + 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/sudocode3.c b/testfiles/sudocode3.c index 22e6c8f..29b1c0e 100644 --- a/testfiles/sudocode3.c +++ b/testfiles/sudocode3.c @@ -1,6 +1,7 @@ #include #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