summaryrefslogtreecommitdiff
path: root/37/CH2/EX2.4/2solved4.sci
blob: 66e870e704a6c3d0fb3271990dd5a70a05f2e3b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//Solved Example 5:
//Convering an infix expression to a Postfix Expression:
function[sta]=push(stac,ele)
  sta=0;
  if(stac.top==0)
    stac.a=ele;
    stac.top=stac.top+1;
  else
    stac.a=[stac.a(:,:) ele]
    stac.top=stac.top+1;
  end
  disp(stac);
  sta=stac;
endfunction

function[ele,sta]=pop(stack)
  ele='-1';
  if(stack.top==0)
    disp("Stack Underflow");
    break;
  else
    ele=stack.a(stack.top);
    stack.top=stack.top-1;
    if(stack.top~=0)
      b=stack.a(1);
    for i2=2:stack.top
      b=[b(:,:) stack.a(i2)];
    end
    stack.a=b;
  else
    stack.a='0';
  end
  end
  sta=stack;
endfunction
function[l]=strlen(x)
  i=1;
  l=0;
  [j,k]=size(x)
  for  i=1:k
    l=l+length(x(i));
  end
endfunction
function[p]=pre(s1,s2)
     i1=0;
    select s1,
    case '+' then i1=5;
    case '-' then i1=5;
    case '*' then  i1=9;
    case '/' then i1=9;
    end
    i2=0;
    select s2,
    case '+' then i2=5;
    case '-' then i2=5;
    case '*' then  i2=9;
    case '/' then i2=9;
    end
    p=0;
    p=i1-i2;
    if(s1=='(')
      p=-1;
    end
    if(s2=='('&s1~=')')
      p=-1;
    end
    if(s1~='('&s2==')')
      p=1;
    end
    
  endfunction
function[a2]=intopo(a1,n)
  stack=struct('a',0,'top',0);
  l1=1;
  l2=strlen(a1(1))
  for i=2:n
    l2=l2+strlen(a1(i))
  end
  a2=list();
  while(l1<=l2)
    symb=a1(l1); 
    if(isalphanum(string(a1(l1))))
      a2=list(a2,symb);
    else
      while(stack.top~=0&(pre(stack.a(stack.top),symb)>=0))
        [topsymb,stack]=pop(stack);
        if(topsymb==')'|topsymb=='(')
          a2=a2;
        else
          a2=list(a2,topsymb);
        end
      end
      if(stack.top==0|symb~=')')
        stack=push(stack,symb);
      else
        [ele,stack]=pop(stack);
      end
    end
    l1=l1+1;
  end
  while(stack.top~=0)
    [topsymb,stack]=pop(stack);
    if(topsymb==')'|topsymb=='(')
      a2=a2;
    else
      a2=list(a2,topsymb);
    end
  end
  disp(a2);
endfunction
//Calling Routine:
a1=['(' '2' '+' '3' ')' '*' '(' '5' '-' '4' ')']
a2=intopo(a1,11)