Skip to content

Commit

Permalink
Doing stage 8 grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyk5 committed Mar 15, 2020
1 parent 7282d40 commit a94c720
Show file tree
Hide file tree
Showing 25 changed files with 11,098 additions and 0 deletions.
2 changes: 2 additions & 0 deletions progs/stage8_inheritance_polymorphism/Run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cat tmp.xsm >> input.xsm
./a.out
Binary file added progs/stage8_inheritance_polymorphism/a.out
Binary file not shown.
109 changes: 109 additions & 0 deletions progs/stage8_inheritance_polymorphism/bst.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
type
bst{
int a;
bst left;
bst right;
}

endtype

decl
int in,opt;
bst insert(bst h, int key);
int inOrder(bst h);
int preOrder(bst h);
int postOrder(bst h);
enddecl

bst insert(bst h, int key)
{
begin
if (h == null) then
h = alloc();
h.a = key;
h.left = null;
h.right = null;

else if (key < h.a) then
h.left = insert(h.left, key);

else if (key > h.a) then
h.right = insert(h.right, key);
endif;
endif;
endif;

return h;
end
}

int inOrder(bst h){
decl
enddecl
begin

if(h!=null) then

in=inOrder(h.left);
write(h.a);
in=inOrder(h.right);
endif;
return 1;
end
}

int preOrder(bst h){

decl
enddecl

begin
if(h!=null) then
write(h.a);
in=preOrder(h.left);

in=preOrder(h.right);
endif;
return 1;
end
}

int postOrder(bst h){

begin

if(h!=null) then

in=postOrder(h.left);

in=postOrder(h.right);
write(h.a);
endif;
return 1;
end
}

int main()
{
decl
int val,flag;
bst Root;
enddecl

begin
initialize();
Root = null;
read(val);

while(val!=0) do
Root = insert(Root,val);
read(val);
endwhile;

in = inOrder(Root);
in = preOrder(Root);
in = postOrder(Root);

return 9;
end
}
131 changes: 131 additions & 0 deletions progs/stage8_inheritance_polymorphism/bst_class.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
type
bst{
int a;
bst left;
bst right;
}
endtype
class
bstclass{
decl
bst root;
int init();
bst getroot();
int setroot(bst n1);
bst getnode(int key);
bst insert(bst h, int key);
int inOrder_fun(bst h);
int preOrder_fun(bst h);
int postOrder_fun(bst h);
enddecl
int init(){
begin
self.root=null;
return 1;
end
}
bst getroot(){
begin
return self.root;
end
}
int setroot(bst n1){
begin
self.root=n1;
return 1;
end
}
bst getnode(int key){
decl
bst temp;
enddecl
begin
temp=alloc();
temp.a=key;
temp.left=null;
temp.right=null;
return temp;
end
}
bst insert(bst h, int key){
begin
if (h == null) then
h = self.getnode(key);
else
if (key < h.a) then
h.left = self.insert(h.left, key);
else
if (key > h.a) then
h.right = self.insert(h.right, key);
endif;
endif;
endif;
return h;
end
}
int inOrder_fun(bst h){
decl
int in;
enddecl
begin
if(h!= null) then
in=self.inOrder_fun(h.left);
write(h.a);
in=self.inOrder_fun(h.right);
endif;
return 1;
end
}
int preOrder_fun(bst h){
decl
int in;
enddecl
begin
if(h!= null) then
write(h.a);
in=self.preOrder_fun(h.left);
in=self.preOrder_fun(h.right);
endif;
return 1;
end
}
int postOrder_fun(bst h){
decl
int in;
enddecl
begin
if(h!= null) then
in=self.postOrder_fun(h.left);
in=self.postOrder_fun(h.right);
write(h.a);
endif;
return 1;
end
}
}
endclass
decl
bstclass obj;
enddecl
int main(){
decl
bst Root;
int x,in,val;
enddecl
begin
initialize();
obj = new(bstclass);
x=obj.init();
read(val);
Root = obj.getroot();
while(val!=0) do
Root = obj.insert(Root,val);
read(val);
endwhile;
x = obj.setroot(Root);
in = obj.inOrder_fun(obj.getroot());
in = obj.preOrder_fun(obj.getroot());
in = obj.postOrder_fun(obj.getroot());
return 0;
end
}
55 changes: 55 additions & 0 deletions progs/stage8_inheritance_polymorphism/bubble_sort.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
decl
int n,arr[10],i,j,dup, BubbleSort(int first, int last);
enddecl

int BubbleSort(int first, int last)
{
decl
int temp;
enddecl

begin
if((first < last)) then

if((last > 0)) then
if(arr[first] > arr[first+1]) then
temp = arr[first];
arr[first] = arr[first+1];
arr[first+1] = temp;
endif;

dup = BubbleSort(first+1, last);
dup = BubbleSort(first, last-1);
endif;
endif;

return 0;
end
}

int main()
{
decl
int r;
enddecl

begin
read(n);

i=0;
while(i<n) do
read(arr[i]);
i = i+1;
endwhile;

r = BubbleSort(0,n-1);

i=0;
while(i<n) do
write(arr[i]);
i = i+1;
endwhile;

return 0;
end
}
57 changes: 57 additions & 0 deletions progs/stage8_inheritance_polymorphism/euclid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
type
node
{
int d;
int s;
int t;
}
endtype

decl
node y,z,gcd(int a,int b);
enddecl

node gcd(int a,int b)
{
decl
int q,r,temp;
enddecl

begin
if(b==0) then
y.d = a;
y.s = 1;
y.t = 0;
else
q = a/b;
r = a%b;
z = gcd(b,r);
temp = z.s;
y.s = z.t;
y.t = temp - (q*z.t);
endif;

return y;
end
}

int main()
{
decl
node res;
int a,b,c;
enddecl

begin
initialize();
y = alloc();
read(a);
read(b);
res = gcd(a,b);
write(res.d);
write(res.s);
write(res.t);

return 0;
end
}
Loading

0 comments on commit a94c720

Please sign in to comment.