Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,6 +61,21 @@ Follow the following format:
Both the examples above are valid.<br>
Mentioning the type is optional. Incase you don't mention the type, the default type of the variable is float.

- <h3>Input a variable</h3>
`Input` helps you input variable on console. It can only be a variable.<br>
The best part about this is that it automatically initialises an uninitialised variable to float and then takes the input.<br>
The only drawback here being that, you can input only one particular variable in one input statement for now.<br>
Let's see how it works!

input <variable>

EXAMPLE: (1) initialise x
input x
(2) input b

<br>


- <h3>Print</h3>
`Print` helps you print things on console. It could be statements or variables.<br>
The best part about this application is that it understands whether you want to print a string or a variable.<br>
Expand Down
30 changes: 30 additions & 0 deletions libs/sudocode_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions testfiles/code0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>


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");
}
}
}
24 changes: 24 additions & 0 deletions testfiles/code0.txt
Original file line number Diff line number Diff line change
@@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having 2 lines for initializing and then taking input i feel it is better to do it in the same line.
Eg. input int a;
We then initialize the variable a and take the input. This way we can initialize any type of variable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to work on this issue after this PR is merged.There is no point of adding features if it is not merge at the end.
Btw, thanx for your suggestion and will look forward for your suggestions and reviews in future.

input a
Comment thread
shubham7298 marked this conversation as resolved.
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
6 changes: 4 additions & 2 deletions testfiles/sudocode3.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>


int test_3(int a,int temp,float test)
{

Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion testfiles/sudocode3.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

function test_3 returns int with args int a, int temp, float test

initialise b=10
Expand All @@ -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
Expand Down