//Task1 stak
#include<bits/stdc++.h>
#define MAXSIZE 100
using namespace std;
class Stak{
int items[MAXSIZE];
int top;
public:
Stak(){
top=-1;
}
bool is_empty();
bool is_full();
void push(int item);
int pop();
int min_element();
int max_element();
int count();
int top_elemant();
float eval_postfix(string s) ;
string infix_postfix(string s) ;
void desplay();
}integer,chr,obj;
bool Stak::is_empty() {
if(top==-1)return 1;
return 0;
}
bool Stak::is_full() {
if(top==MAXSIZE-1)return 1;
return 0;
}
void Stak::push(int item) {
if(is_full()){
cout<<"over flow";
return;
}
top++;
items[top]=item;
}
int Stak::count() {
return top + 1;
}
int Stak::pop(){
if(is_empty()){
cout<<"under flow";
return 0;
}
int item =items[top];
top--;
return item;
}
int Stak::top_elemant(){
if(is_empty()){
cout<<"Stack is empty\n";
return 0;
}
return items[top];
}
int Stak::min_element(){
if(is_empty()){
cout<<"Stack is empty\n";
return 0;
}
int min = INT16_MAX;
for(int i=0;i<=top;i++){
if(items[i]<min){
min=items[i];
}
}
return min;
}
int Stak:: max_element() {
if (is_empty()) {
cout << "Stack is empty!" << endl;
return 0;
} else {
int max_elem = INT16_MIN;
for (int i = 0; i <= top; ++i) {
if (items[i] > max_elem) {
max_elem = items[i];
}
}
return max_elem;
}
}
void Stak::desplay() {
for(int i=0;i<=top;i++){
cout<<items[i]<<" ";
}
cout<<"\n";
}
float Stak::eval_postfix(string s) {
for(int i=0;i<s.size();i++){
if(isdigit(s[i])){
integer.push(s[i]-48);
}
else{
float opr2=integer.pop();
float opr1=integer.pop();
float val;
switch (s[i]) {
case '+': val=opr1+opr2;break;
case '/': val=opr1/opr2;break;
case '*': val=opr1*opr2;break;
case '-': val=opr1-opr2;break;
case '^': val=pow(opr1,opr2);break;
}
integer.push(val);
}
}
return integer.pop();
}
int precedence(char op) {
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return 0;
}
string Stak::infix_postfix(string s){
string postfix="";
for(int i=0;i<s.size();i++){
if(isalnum(s[i])){
postfix+=s[i];
}
else if(s[i]=='('){
chr.push(s[i]);
}
else if(s[i]==')'){
while(!chr.is_empty()&&chr.top_elemant()!='('){
postfix+=chr.pop();
}
}
else{
while(!chr.is_empty()&&chr.top_elemant()!='('&& precedence(s[i])<=precedence(chr.top_elemant())){
postfix+=chr.pop();
}
chr.push(s[i]);
}
}
while(!chr.is_empty()){
postfix+=chr.pop();
}
return postfix;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("out.out", "w", stdout);
freopen("in.in", "r", stdin);
#else
#endif
obj.push(5);
obj.push(6);
obj.push(7);
obj.push(8);
cout<<obj.is_full()<<"\n";
obj.desplay();
obj.pop();
obj.desplay();
cout<< obj.max_element()<<"\n";
cout<< obj.min_element()<<"\n";
cout<< obj.top_elemant()<<"\n";
obj.pop();
obj.pop();
obj.pop();
cout<<obj.is_empty()<<"\n";
string infix;
cout << "Enter an infix expression: ";
cin >> infix;
string postfix = obj.infix_postfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}
/*******************************************/