Advertisement
NB52053

BalSal

Dec 4th, 2016
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. Assignment!!!
  2.  
  3. #include<stdio.h>
  4. const int max =100;
  5. //char arr[20]="{({})([])}}";
  6. //char arr[20]="[{{({})([])}}]";
  7. char arr[30]="(()){{{[]}{[][]}}";
  8. int s[max],top=-1;
  9. void push(int val);
  10. void pop();
  11. char top_val();2
  12. void showall();
  13. int main()
  14. {
  15.     int i=0,val;
  16.     while(arr[i]!=NULL)
  17.     {
  18.         if(arr[i]=='{'||arr[i]=='['||arr[i]=='(')
  19.         {
  20.             val=arr[i];
  21.             push(val);
  22.         }
  23.         else if(s[top]=='{'&&arr[i]=='}')
  24.         {
  25.             pop();
  26.         }
  27.         else if(s[top]=='('&&arr[i]==')')
  28.         {
  29.             pop();
  30.         }
  31.         else if(s[top]=='['&&arr[i]==']')
  32.         {
  33.             pop();
  34.         }
  35.         else if(top==-1)
  36.         {
  37.             if(arr[i]=='}'||arr[i]==')'||arr[i]==']')
  38.             push(arr[i]);
  39.             break;
  40.         }
  41.         i++;
  42.     }
  43.  
  44.     if(top_val()=='}')
  45.     {
  46.         printf("closing braket overflow\n");
  47.     }
  48.     else if(top_val()=='{')
  49.     {
  50.         printf("opening braket overflow\n");
  51.     }
  52.     else if(top==-1)
  53.     {
  54.         printf("Balanced\n");
  55.     }
  56.     else
  57.     {
  58.         printf("unmatched\n");
  59.     }
  60. }
  61. void push(int val)
  62. {
  63.     if(top+1>max)
  64.         printf("u cant push this value cz stack is over full\n");
  65.     else
  66.         s[++top]=val;
  67. }
  68. char top_val()
  69. {
  70.     //printf("%d\n",s[top]+48);
  71.     return s[top];
  72. }
  73. void pop()
  74. {
  75.     if(top<-1)
  76.         printf("this stack is empty\n");
  77.     else
  78.         --top;
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. // Assigbnment 2
  87.  
  88. #include<stdio.h>
  89. const int max =100;
  90. char arr[20]="23+6*";
  91. int s[max],top=-1;
  92. void push(int val);
  93. void pop();
  94. void top_val();
  95. void showall();
  96. void main()
  97. {
  98.     int i=0,val;
  99.     while(arr[i]!=NULL)
  100.     {
  101.         if(arr[i]>='0'&&arr[i]<='9')
  102.         {
  103.             val=arr[i]-48;
  104.             push(val);
  105.         }
  106.         else
  107.         {
  108.             if(arr[i]=='+')
  109.             {
  110.                 int temp;
  111.                 temp=s[top]+s[top-1];
  112.                 s[--top]=temp;
  113.             }
  114.             else if(arr[i]=='-')
  115.             {
  116.                 int temp;
  117.                 temp=s[top]-s[top-1];
  118.                 s[--top]=temp;
  119.             }
  120.             else if(arr[i]=='*')
  121.             {
  122.                 int temp;
  123.                 temp=s[top]*s[top-1];
  124.                 s[--top]=temp;
  125.             }
  126.             else if(arr[i]=='/')
  127.             {
  128.                 int temp;
  129.                 temp=s[top-1]/s[top];
  130.                 s[--top]=temp;
  131.             }
  132.         }
  133.         i++;
  134.     }
  135.     top_val();
  136. }
  137. void push(int val)
  138. {
  139.     if(top+1>max)
  140.         printf("u cant push this value cz stack is over full\n");
  141.     else
  142.         s[++top]=val;
  143. }
  144. void top_val()
  145. {
  146.     printf("%d\n",s[top]);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement