Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cstdlib>
- #include <cmath>
- #include <utility>
- using namespace std;
- // Klasa stosu
- class Stack{
- private:
- struct element{
- element * next, *prev;
- long long value;
- };
- element * first, * last;
- unsigned int counter;
- public:
- Stack(){
- first = NULL;
- last = NULL;
- counter = 0;
- }
- ~Stack(){
- element * p;
- while(first){
- p = first->next;
- delete first;
- first = p;
- }
- }
- bool empty(){
- if (counter == 0)
- return true;
- else
- return false;
- }
- int size(){
- return counter;
- }
- long long top(){
- return first->value;
- }
- void push(long long val){
- element * p = new element;
- p->next = first;
- p->prev = NULL;
- p->value = val;
- if(first)
- first->prev = p;
- first = p;
- if(!last)
- last = first;
- counter++;
- }
- void pop(){
- element * p;
- if(first){
- p = first;
- first = first->next;
- if(!first)
- last = NULL;
- else
- first->prev = NULL;
- counter--;
- delete p;
- }
- }
- void show_values(){
- element * p;
- p = first;
- while(p){
- cout << p->value << " ";
- p = p->next;
- }
- }
- };
- // Stos do zamiany na inflix
- class StackSubexpressions {
- private:
- struct element{
- element * next, *prev;
- string expr;
- string oper;
- };
- element * first, * last;
- unsigned int counter;
- public:
- StackSubexpressions(){
- first = NULL;
- last = NULL;
- counter = 0;
- }
- ~StackSubexpressions(){
- element * p;
- while(first){
- p = first->next;
- delete first;
- first = p;
- }
- }
- string top_expr(){
- return first->expr;
- }
- string top_oper(){
- return first->oper;
- }
- void push(string e, string o){
- element * p = new element;
- p->next = first;
- p->prev = NULL;
- p->expr = e;
- p->oper = o;
- if(first)
- first->prev = p;
- first = p;
- if(!last)
- last = first;
- counter++;
- }
- void pop(){
- element * p;
- if(first){
- p = first;
- first = first->next;
- if(!first)
- last = NULL;
- else
- first->prev = NULL;
- counter--;
- delete p;
- }
- }
- bool empty(){
- if (counter == 0)
- return true;
- else
- return false;
- }
- int size(){
- return counter;
- }
- };
- // Funkcja obliczajaca wartosc wyrazenia podangego w onp
- long long calcRpn(string &expr){
- istringstream iss(expr);
- Stack stack;
- string token;
- while (iss >> token) {
- long long token_digit;
- if (istringstream(token) >> token_digit) {
- stack.push(token_digit);
- }
- else {
- int b = stack.top();
- stack.pop();
- int a = stack.top();
- stack.pop();
- if (token == "+")
- stack.push(a + b);
- else if (token == "-")
- stack.push(a - b);
- else if (token == "*")
- stack.push(a * b);
- else if (token == "/")
- stack.push(a / b);
- else {
- return 0;
- }
- }
- }
- return stack.top();
- }
- // Licznik slow w stringu
- int wordCounter(string str){
- int words = 1;
- for(int i = 0; i <= str.size(); i++){
- if(isspace(str[i + 1]))
- words++;
- }
- return words;
- }
- // Funkcja dzielaca string na tokeny i zapisujaca je do tablicy
- string * Split(string str){
- string * arr = new string[str.size()];
- int i = 0;
- istringstream iss(str);
- while (iss.good()){
- iss >> arr[i];
- ++i;
- }
- return arr;
- }
- // Funkcja przeksztalcajaca postfix -> infix
- string PostfixToInfix(string postfix){
- // Tablica przechowujaca podzielone wyrazenie w postfix
- string * postfixTokens = new string[wordCounter(postfix)];
- postfixTokens = Split(postfix);
- // Stos do przechowywania podwyrazen inflix
- StackSubexpressions stack;
- for(int i = 0; i < wordCounter(postfix); i++){
- string token = postfixTokens[i];
- if (token == "+" || token == "-"){
- pair<string, string> rightSubExpr;
- rightSubExpr.first = stack.top_expr();
- rightSubExpr.second = stack.top_oper();
- stack.pop();
- pair<string, string> leftSubExpr;
- leftSubExpr.first = stack.top_expr();
- leftSubExpr.second = stack.top_oper();
- stack.pop();
- string newExpr = leftSubExpr.first + token + rightSubExpr.first;
- stack.push(newExpr, token);
- }
- else if (token == "*" || token == "/"){
- string leftExpr, rightExpr;
- pair<string, string> rightSubExpr;
- rightSubExpr.first = stack.top_expr();
- rightSubExpr.second = stack.top_oper();
- stack.pop();
- if (rightSubExpr.second == "+" || rightSubExpr.second == "-" ){
- rightExpr = "(" + rightSubExpr.first + ")";
- }
- else{
- rightExpr = rightSubExpr.first;
- }
- pair<string, string> leftSubExpr;
- leftSubExpr.first = stack.top_expr();
- leftSubExpr.second = stack.top_oper();
- stack.pop();
- if (leftSubExpr.second == "+" || leftSubExpr.second == "-"){
- leftExpr = "(" + leftSubExpr.first + ")";
- }
- else{
- leftExpr = leftSubExpr.first;
- }
- string newExpr = leftExpr + token + rightExpr; cin.ignore();
- stack.push(newExpr, token);
- }
- else
- {
- stack.push(token, "");
- }
- }
- delete [] postfixTokens;
- return stack.top_expr();
- }
- // Struktura do przechowywania wynikow
- struct results{
- string expr;
- long long result;
- bool isUsed;
- int bracketsNum;
- };
- // Licznik nawiasow
- int bracketsCounter(string str){
- int bracketsNum = 0;
- for(int i = 0; i < str.size(); i++)
- if(str[i] == '(' || str[i] == ')')
- bracketsNum++;
- return bracketsNum;
- }
- // Funkcja sortujaca wyniki wedlug nawiasow
- void Sort(results arr[], int size){
- results temp;
- int j;
- for(int i = 1; i < size; i++)
- if (arr[i].bracketsNum < arr[i - 1].bracketsNum){
- temp = arr[i];
- for(j = i - 1; (j >= 0)&& (arr[j].bracketsNum > temp.bracketsNum); j--)
- arr[j + 1] = arr[j];
- arr[j + 1] = temp;
- }
- }
- int main(){
- int operations_num; // Liczba operacji
- char operation_type; // Rodzaj operacji
- string expr; // Wyrazenie w ONP
- int s_counter = 0;
- cin >> operations_num;
- results arr[operations_num]; // Tablica przechowujaca wyrazenia z wynikiem
- int arr_index = 0; // Indeksy tablicy
- for(int i = 0; i < operations_num; i++){
- cin >> operation_type;
- // Dodawanie wyrazenia
- if(operation_type == 'D'){
- cin.ignore();
- getline(cin, expr);
- arr[arr_index].result = calcRpn(expr);
- arr[arr_index].expr = PostfixToInfix(expr);
- arr[arr_index].bracketsNum = bracketsCounter(arr[arr_index].expr);
- arr[arr_index].isUsed = false;
- arr_index++;
- }
- // Przeszukanie wyrazen
- else if(operation_type == 'S'){
- long long res_search;
- string result_expr;
- bool result_found = false;
- cin >> res_search;
- Sort(arr, arr_index);
- for(int i = 0; i < arr_index; i++){
- if(arr[i].result == res_search && arr[i].isUsed != true){
- result_expr = arr[i].expr;
- arr[i].isUsed = true;
- result_found = true;
- break;
- }
- }
- if(result_found){
- cout << "TAK" << endl;
- cout << result_expr << endl;
- }
- else{
- cout << "NIE" << endl;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement