Programa de conversão de funções infixas para pósfixas (pilhas)
#include "conversor.h"
#include <conio2.h>
int main()
{
tpilha p;
char n;
int m, i;
char expressao[MAX];
char teste[10];
do
{
scanf("%s", teste);
for (i=0;i<5;i++)
{
push(&p, teste*);*
mostrarelementos(p); /// testando a função problemática
}
system("cls");
puts("Para converter a expressao digite 1 para sair, 0");
printf("opcao: ");
scanf("%d", &m);
getchar();
switch (m)
{
case 1:
puts("Digite a expressao na forma infixa");
scanf("%[^\n]s", expressao);
getchar();
if (!expressaoInfixaValida(expressao))
{
system("cls");
puts("----------------------------------------------------------------------");
puts("Digite novamente a expressao, dessa vez uma valida");
puts("----------------------------------------------------------------------");
system("pause");
}
else
{
if (expressaoInfixaValida(expressao))
{
leroperandos(expressao);
do
{
puts("----------------------------------------------------------------------");
puts("Desejas ver o processo da expressao? Se sim digite 1 caso contrario 0");
scanf("%c", &n);
getchar();
if((n!='1') && (n!='0'))
{
puts("----------------------------------------------------------------------");
puts("Digite ou 1 ou 0");
}
} while ((n!='1') && (n!='0'));
if(n=='0')
{
printf("%s\n", converterInfixaParaPosfixa(expressao, n));
puts("----------------------------------------------------------------------");
system("pause");
system("cls");
}
else
{
converterInfixaParaPosfixa(expressao, n);
puts("----------------------------------------------------------------------");
system("pause");
system("cls");
}
}
}
break;
case 0:
exit(0);
default:
system("cls");
puts("----------------------------------------------------------------------");
printf( "\nDigite uma opcao valida! \n" );
}
}while(m!=0);
return 0;
Arquivo:funções.c
#include "conversor.h"
int expressaoInfixaValida (char expressao)*
{
int i, controleparental=0;
* for(i=0;expressao**!='\0';i++)*
{
* if(expressao**=='(')*
controleparental++;
else
{
* if(expressao**==')')*
controleparental--;
}
if(i==0)
{
if((!isalpha(expressao) && (expressao!='(')))
return 0;
}
else
{
* if(isalpha(expressao**) && (!ehoperador(expressao[i-1])) && (expressao[i-1]!=')') && (expressao[i-1]!='('))*
return 0;
* if((ehoperador(expressao**))&& (!isalpha(expressao[i-1])) && (expressao[i-1]!=')'))*
return 0;
}
}
if(controleparental!=0)
return 0;
else
return 1;
}
int ehoperador(char c)
{
switch©
{
case '+':
case '-':
case '':*
case '/':
case '^':
return 1;
}
return 0;
}
int converterInfixaParaPosfixa (char expressao, char mostrarExecucao)*
{
tpilha p, p2;
char saida[MAX], topo[MAX];
int i, j, controle=0, controle2=0, controle3=1;
criarpilha(&p);
if(mostrarExecucao=='0')
{
* for (i=0;expressao**!='\0';i++)*
{
* if (isalpha(expressao**))*
{
* saida[controle]=expressao**;*
controle++;
}
else
{
* if (ehoperador(expressao**))*
{
* while((!vazia(p)) && (priority(expressao**))<=(priority(elemtopo(p))))*
{
saida[controle]=elemtopo(p);
controle++;
pop(&p);
}
* push(&p,expressao**);*
* if(expressao**=='(')*
{
* push(&p,expressao**);*
saida[controle]=elemtopo(p);
}
* if(expressao**==')')*
{
while(elemtopo(p)!='(')
{
saida[controle]=elemtopo(p);
controle++;
pop(&p);
}
pop(&p);
}
}
}
}
for(j=0;!vazia(p);j++)
{
saida[controle]=elemtopo(p);
controle++;
pop(&p);
}
return (saida);
}
if(mostrarExecucao=='1')
{
* for (i=0;expressao**!='\0';i++)*
{
* if (isalpha(expressao**))*
{
* saida[controle]=expressao**;*
controle++;
if(!vazia(p))
{
topo[controle2]=elemtopo(p);
push(&p2, topo[controle2]);
controle2++;
* printf("[%d]%c Pilha[topo]:[",controle3, expressao**);*
mostrarelementos(&p2);
printf("] Saida:%s\n", saida);
controle3++;
}
else
{
* printf("[%d]%c Pilha[topo]:[] Saida:%s\n",controle3, expressao**,saida);*
controle3++;
}
}
else
{
* if (ehoperador(expressao**))*
{
* while((!vazia(p)) && (priority(expressao**))<=(priority(elemtopo(p))))*
{
saida[controle]=elemtopo(p);
controle++;
if(!vazia(p))
{
topo[controle2]=elemtopo(p);
push(&p2, topo[controle2]);
controle2++;
* printf("[%d]%c Pilha[topo]:[",controle3, expressao**);*
mostrarelementos(&p2);
printf("] Saida:%s\n", saida);
controle3++;
}
else
{
* printf("[%d]%c Pilha[topo]:[] Saida:%s\n",controle3, expressao**,saida);*
controle3++;
}
pop (&p);
}
* push(&p,expressao**);*
topo[controle2]=elemtopo(p);
push(&p2, topo[controle2]);
controle2++;
* printf("[%d]%c Pilha[topo]:[",controle3, expressao**);*
mostrarelementos(&p2);
printf("] Saida:%s\n", saida);
controle3++;
* if(expressao**=='(')*
{
* push(&p,expressao**);*
saida[controle]=elemtopo(p);
}
* if(expressao**==')')*
{
while(elemtopo(p)!='(')
{
saida[controle]=elemtopo(p);
controle++;
if(!vazia(p))
{
topo[controle2]=elemtopo(p);
push(&p2, topo[controle2]);
controle2++;
* printf("[%d]%c Pilha[topo]:[",controle3, expressao**);*
mostrarelementos(&p2);
printf("] Saida:%s\n", saida);
controle3++;
}
else
{
* printf("[%d]%c Pilha[topo]:[] Saida:%s\n",controle3, expressao**, saida);*
controle3++;
}
pop(&p);
}
pop(&p);
}
}
}
}
for(j=0;!vazia(p);j++)
{
saida[controle]=elemtopo(p);
controle++;
pop(&p);
}
}
saida[controle]='\0';
puts(" ");
puts("Evaziando a pilha!!!");
puts("----------------------------------------------------------------------");
printf("[%d] expressao final: %s\n", controle3, saida);
}
int priority (char c)
{
int prioridade;
switch ©
{
case '+':
prioridade=2;
break;
case '-':
prioridade=2;
break;
case '':*
prioridade=3;
break;
case '/':
prioridade=3;
break;
case '^':
prioridade=4;
break;
case '(':
prioridade=1;
}
return prioridade;
}
char mostraoperandos (char expressao)*
{
int i;
char stringsaida[strlen(expressao)];
* for (i=0;expressao**!='\0';i++)*
{
* if (isalpha(expressao**))*
{
stringsaida=expressao;
}
}
return (stringsaida);
}
int contaroperandos (char expressao)*
{
int i, contador=0;
* for (i=0;expressao**!='\0';i++)*
{
* if (isalpha(expressao**))*
{
contador++;
}
}
return (contador);
}
int leroperandos (char expressao)*
{
int i, j;
float total [MAX];
puts("----------------------------------------------------------------------");
puts("Digite os valores dos operandos respectivamente ");
for(i=0;i<contaroperandos(expressao);i++)
{
printf("valor %d: \n", i+1);
* scanf("%f", &total**);*
getchar();
}
return total;
}
/float executaExpressao (char expressao, char operandos, float valor)
{
int i, j, tamanho=strlen(expressao), tamanho2=strlen(mostraoperandos(expressao));
tpilha p;
criarpilha(p);*
for (i=0;i<tamanho;i++)
{
* push(&p, expressao**);*
}
for()
}/*
int stringcontrario (char topo)*
{
int i, tamanho=strlen(topo);
char topo2[tamanho];
for(i=tamanho-1;i>=0;i--)
{
* topo2**=topo[1];*
}
return(topo2);
}
Arquivo: funçõespilha.c
#include "pilha.h"
void criarpilha(tpilha p){*
p = NULL;*
}
int vazia(tpilha p){
return (p == NULL);
}
char elemtopo(tpilha p){
telem valor;
if(vazia(p)){
return 0;
}
valor = p->dado;
return (valor);
}
int push(tpilha p, telem dado){*
tpilha novo;
novo = (tno ) malloc(sizeof(tno));*
if(novo == NULL) return 0;
novo->dado = dado;
novo->prox = p;*
p = novo;*
return 1;
}
int pop(tpilha p){*
tpilha aux;
if(vazia(p)) return 0;*
aux = p;*
p = (p)->prox;
//p = aux->prox;*
free(aux);
return 1;
}
void mostrarelementos(tpilha p){ ///função problemática
tpilha aux;
aux=p;
while (aux!=NULL)
{
printf("%c", aux->dado);
aux=aux->prox;
}
}
Arquivo: conversor.h
#include "pilha.h"
int converterInfixaParaPosfixa (char expressao, char mostrarExecucao);*
int expressaoInfixaValida (char expressao);*
int obterOperandos (char expressao);*
float executaExpressao (char expressao, char operandos, float valor);*
int ehoperador(char c);
int priority (char c);
char mostraoperandos (char expressao);*
int contaroperandos (char expressao);*
int leroperandos (char expressao);*
int stringcontrario (char topo);*
Arquivo pilha.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
#define MOSTRAR 1
#define NAO_MOSTRAR 0
typedef char telem;
typedef struct no{
telem dado;
struct no prox;*
}tno;
typedef tno tpilha;*
void criarpilha(tpilha p);*
int vazia(tpilha p);
char elemtopo(tpilha p);
int push(tpilha p, telem dado);*
int pop(tpilha p);*
void mostrarelementos(tpilha p);
meu problema está na função mostraelementos, pode ser que hajam problemas em outras, mas por enquanto eu só localizei nessa, a função só imprime o primeiro elemento da pilha, depois ela dá um crash e feche o .exe.
Discussão (0)
Carregando comentários...