Advertisement
madalinaradu

Stiva inversare sir caratere

May 5th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <string.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. class Stiva {
  8.     int max;
  9.     char *t;
  10.     int top;
  11. public:
  12.     Stiva(int max) {
  13.         this->max= max;
  14.         this->t = new char[max];
  15.         this->top = -1;
  16.     }
  17.  
  18.     void push(char c) {
  19.         if(top < max-1){
  20.             t[++top]=c;
  21.         } else {
  22.             cerr<<"Eroare. Depasire capacitate\n";
  23.             exit(1);
  24.         }
  25.  
  26.     }
  27.  
  28.     char pop() {
  29.         return t[top--];
  30.     }
  31.  
  32.     bool isEmpty() {
  33.         /*
  34.         if(top == -1)
  35.             return true;
  36.         else
  37.             return false;
  38.         */
  39.         return top == -1;
  40.  
  41.  
  42.     }
  43.  
  44.     bool isFull() {
  45.         return top == max-1;
  46.     }
  47.  
  48.  
  49. };
  50.  
  51. int main() {
  52.  
  53.     /* Varianta cu cu tablou de caractere */
  54.     /*
  55.      int i;
  56.      char input[100]="abcde";
  57.      char output[100];
  58.      Stiva stiva(strlen(input));
  59.  
  60.  
  61.      for(i=0;i<strlen(input);i++){
  62.         stiva.push(input[i]); // iau fiecare caracter din sirul de intrare si il pun in stiva.
  63.      }
  64.  
  65.      i=0;
  66.      while (!stiva.isEmpty())  {
  67.             output[i]= stiva.pop();
  68.             i++;
  69.      }
  70.      output[i]='\0';
  71.      cout<<output;
  72.  
  73.     */
  74.  
  75.     string input = "abcde";
  76.     string output;
  77.     Stiva stiva(input.length());
  78.     for(int i=0; i<input.length(); i++) {
  79.         stiva.push(input[i]); // iau fiecare caracter din sirul de intrare si il pun in stiva.
  80.     }
  81.  
  82.     output = "";
  83.     while (!stiva.isEmpty())  {
  84.         output = output + stiva.pop(); //caracterul scos din stiva cu pop() e adaugat la output
  85.     }
  86.     cout<<output;
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement