summaryrefslogtreecommitdiff
path: root/1034/CH3/EX1.2.b/US1.sce
blob: f3caa7cfe9d14c0726535471a0b443f791aaf4de (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
//Unsolved Example 1
clear;
clc;
disp("example 3.7");
//To  determine the syntacticaly valid string
function[l]=strlen(x)
  i=1;
  l=0;
  [j,k]=size(x)
  for  i=1:k
    l=l+length(x(i));
  end
endfunction
function[]=stringvalid(str)
  str=string(str);
  stack=struct('a','0','top',0);
  l1=strlen(str);
  valid=1;
  l=1;
  while(l<=l1)
      if(str(l)=='('|str(l)=='['|str(l)=='{')
        if(stack.top==0)
          stack.a=str(l);
          stack.top=stack.top+1;
        else
          stack.a=[stack.a(:,:) str(l)];
          stack.top=stack.top+1;
        end
        disp(stack);
      end
      if(str(l)==')'|str(l)==']'|str(l)=='}')
        if(stack.top==0)
          valid=0;
          break;
        else
          i=stack.a(stack.top);
          b=stack.a(1);
          for i1=2:stack.top-1
            b=[b(:,:) stack.a(i1)]
          end
          stack.a=b;
         stack.top=stack.top-1;
         symb=str(l);
         disp(stack);
         if(((symb==')')&(i=='('))|((symb==']')&(i=='['))|((symb=='}')&(i=='{')))
        else
          valid=0;
          break;
        end
      end
    end
      l=l+1;
    end
    if(stack.top~=0)
      valid=0;
    end
    if(valid==0)
      disp("Invalid String");
    else
      disp("Valid String");
    end
  endfunction
  //Calling Routine:
  stringvalid(['(' 'A' '+' 'B' '}' ')'])
  stringvalid(['{' '[' 'A' '+' 'B' ']'  '-' '[' '(' 'C' '-'  'D' ')' ']'])
  stringvalid(['(' 'A' '+' 'B' ')' '-' '{' 'C' '+' 'D' '}' '-' '[' 'F' '+' 'G' ']'])
  stringvalid(['(' '(' 'H' ')' '*' '{' '(' '[' 'J' '+' 'K' ']' ')' '}' ')'])
  stringvalid(['(' '(' '(' 'A' ')' ')' ')'])