Um exemplo de criptografia simples usando pilha, o objetivo era criar um decriptador com esquema similar da calculadora pós fixa ao inseri a frase UTA*EC**R**O** .
Sempre que encontrar um operando, remove a letra da pilha e imprime, no final dará ACERTOU.
Sempre que encontrar um operando, remove a letra da pilha e imprime, no final dará ACERTOU.
Código:
>>>>>>>>>>>>>>>>>>>>>>>>>>main.cpp #include <iostream> #include <pilha.h> #include <stdio.h> #include <string.h> using namespace std; int main() { Pilha P; char mensagem[100]; init(&P); scanf(" %s", mensagem); for(int i=0;i<strlen(mensage);++i){ if(mensagem[i] == '*') printf("%c", pop($P)); else push(&P, mensagem[i]); } return 0; } >>>>>>>>>>>>>>>>>>>>>>>>>>Arquivo pilha.h #ifndef PILHA_H_INCLUDED #define PILHA_H_INCLUDED #define MAX_PILHA 1000 typedef struct Pilha{ int vet[MAX_PILHA]; int topo; }Pilha; void init(Pilha *P); void push(Pilha *P, int N); int pop(Pilha *P); int peek(Pilha *P); bool full(Pilha *P); bool empty(Pilha *P); #endif // PILHA_H_INCLUDED >>>>>>>>>>>>>>>>>>>>>>>>>>Arquivo pilha.cpp #include "pilha.h" void init(Pilha *P){ P->topo = -1; } void push(Pilha *P, int N){ if(P->topo != MAX_PILHA - 1) P->vet[++(P->topo)]=N; } int pop(Pilha *P){ if(P->topo!=-1) return P->vet[P->topo--]; return 0; } int peek(Pilha *P){ if(P->topo!=-1) return P->vet[P->topo]; return 0; } bool full(Pilha *P){ return P->topo == MAX_PILHA -1; } bool empty(Pilha *P){ return P->topo == -1; }