Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- template<class T>
- struct Node
- {
- T data;
- Node* next=nullptr;
- };
- template<class T>
- class Stack
- {
- private:
- Node<T>* first=nullptr;
- public:
- void add_element(T data)
- {
- if(first==nullptr)
- {
- Node<T>* a = new Node<T>;
- a->data=data;
- first=a;
- }
- else
- {
- Node<T>* current=first;
- while(current->next!=nullptr)
- {
- current=current->next;
- }
- Node<T>* a = new Node<T>;
- a->data=data;
- current->next=a;
- }
- }
- T peak()
- {
- Node<T>* current=first;
- while(current->next!=nullptr)
- {
- current=current->next;
- }
- return current->data;
- }
- void pop()
- {
- Node<T>* current=first;
- if(current->next==nullptr)
- {
- delete current;
- first=nullptr;
- }
- else
- {
- while(current->next->next!=nullptr)
- {
- current=current->next;
- }
- delete current->next;
- current->next=nullptr;
- }
- }
- void print()
- {
- Node<T>* current=first;
- while(current!=nullptr)
- {
- cout<<current->data;
- current=current->next;
- }
- }
- bool empt()
- {
- return first==nullptr;
- }
- };
- int main()
- {
- Stack<char> s;
- string sentence;
- cout<<"Enter equazion: ";
- cin>>sentence;
- //sentence="1+(3+2-[2+3]*4-{<3+1>*(4-2)})";
- for(int i=0;i<sentence.size();i++)
- {
- if(sentence[i]=='('||sentence[i]=='{'||sentence[i]=='['||sentence[i]=='<')
- {
- s.add_element(sentence[i]);
- }
- if(sentence[i]==')'||sentence[i]=='}'||sentence[i]==']'||sentence[i]=='>')
- {
- if(sentence[i]==')'&&s.peak()=='(')
- {
- s.pop();
- }
- else if(sentence[i]=='}'&&s.peak()=='{')
- {
- s.pop();
- }
- else if(sentence[i]==']'&&s.peak()=='[')
- {
- s.pop();
- }
- else if(sentence[i]=='>'&&s.peak()=='<')
- {
- s.pop();
- }
- else
- {
- cout<<"Error"<<endl;
- break;
- }
- }
- }
- if(s.empt()==1)
- {
- cout<<"Correct"<<endl;
- return s.empt()==1;
- }
- else
- {
- cout<<"Sorry"<<endl;
- return s.empt()==1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement