Code:
/*Implementirajte a.t.p. STACK pomoću pointera i napišite potprogram koji
logički izraz iz infix oblika prebacuje u postfix oblik. Problem trebate
riješiti pomoću stoga.
Ulazni podaci: string koji predstavlja logički izraz u infix obliku
Izlazni podaci: prikaz istog izraza u postfix obliku
Na primjer, za ulazne podatke:
A|B&(C^E|D)
treba ispisati:
ABCE^D|&|
Napomena: &=AND, |=OR, ^=XOR, -=NOT; obratite pažnju na prioritete */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Operator (-10)
#define Operand (-20)
#define Lijeva (-30)
#define Desna (-40)
typedef char elementtype;
typedef struct _celltype {
elementtype element;
struct _celltype *next;
} celltype;
typedef celltype *STACK;
void error(char s[255]){
printf("%s", s); exit(1);
}
void MAKE_NULL(STACK *S){
*S=NULL;
}
int EMPTY(STACK S){
if(S==NULL) return 1;
return 0;
}
void PUSH(elementtype x, STACK *S){
celltype *novi=(celltype *)malloc(sizeof(celltype));
novi->element=x;
novi->next=*S;
*S=novi;
}
elementtype TOP(STACK S){
if(EMPTY(S)) error("Stog je antipun.");
return S->element;
}
void POP(STACK *S){
if(EMPTY(*S)) error("Stog je antiprazan.");
celltype *temp=*S;
*S=temp->next;
free(temp);
}
int nadji_tip(char c)
{
if ((c=='&')||(c=='-')||(c=='^')||(c=='|'))
return Operator;
if (c=='(')
return Lijeva;
if (c==')')
return Desna;
else
return Operand;
}
int prioritet(char c)
{
if (c=='&')
return 2;
if (c=='-')
return 3;
if (c=='|')
return 1;
if (c=='^')
return 1;
}
int main(){
char infix[20], postfix[20];
int len, tip, i, p=0;
STACK pom;
MAKE_NULL(&pom);
printf ("Unesi izraz.\n");
scanf ("%s",&infix);
len=strlen(infix);
for (i=0; i<len; i++){
tip=nadji_tip(infix[i]);
if (tip==Lijeva) PUSH( infix[i], &pom );
if (tip==Desna){ while ( (TOP(pom)) != '(' ){
postfix[p++] = TOP(pom);
POP(&pom);}}
if (tip==Operand) postfix[p++] = infix[i];
if (tip==Operator) {
if (EMPTY(pom)==1) PUSH (infix[i], &pom);
while (prioritet(infix[i]) <= prioritet(TOP(pom))){
postfix[p++]=TOP(pom);
POP(&pom);}
if (prioritet(infix[i])>prioritet(TOP(pom)))
postfix[p++]=infix[i];
}}
while (EMPTY(pom)!= 1){postfix[p++]=TOP(pom);
POP(&pom);}
printf ("Izlaz: %s", postfix);
scanf("%%");
return 0;
}
/*Implementirajte a.t.p. STACK pomoću pointera i napišite potprogram koji
logički izraz iz infix oblika prebacuje u postfix oblik. Problem trebate
riješiti pomoću stoga.
Ulazni podaci: string koji predstavlja logički izraz u infix obliku
Izlazni podaci: prikaz istog izraza u postfix obliku
Na primjer, za ulazne podatke:
A|B&(C^E|D)
treba ispisati:
ABCE^D|&|
Napomena: &=AND, |=OR, ^=XOR, -=NOT; obratite pažnju na prioritete */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Operator (-10)
#define Operand (-20)
#define Lijeva (-30)
#define Desna (-40)
typedef char elementtype;
typedef struct _celltype {
elementtype element;
struct _celltype *next;
} celltype;
typedef celltype *STACK;
void error(char s[255]){
printf("%s", s); exit(1);
}
void MAKE_NULL(STACK *S){
*S=NULL;
}
int EMPTY(STACK S){
if(S==NULL) return 1;
return 0;
}
void PUSH(elementtype x, STACK *S){
celltype *novi=(celltype *)malloc(sizeof(celltype));
novi->element=x;
novi->next=*S;
*S=novi;
}
elementtype TOP(STACK S){
if(EMPTY(S)) error("Stog je antipun.");
return S->element;
}
void POP(STACK *S){
if(EMPTY(*S)) error("Stog je antiprazan.");
celltype *temp=*S;
*S=temp->next;
free(temp);
}
int nadji_tip(char c)
{
if ((c=='&')||(c=='-')||(c=='^')||(c=='|'))
return Operator;
if (c=='(')
return Lijeva;
if (c==')')
return Desna;
else
return Operand;
}
int prioritet(char c)
{
if (c=='&')
return 2;
if (c=='-')
return 3;
if (c=='|')
return 1;
if (c=='^')
return 1;
}
int main(){
char infix[20], postfix[20];
int len, tip, i, p=0;
STACK pom;
MAKE_NULL(&pom);
printf ("Unesi izraz.\n");
scanf ("%s",&infix);
len=strlen(infix);
for (i=0; i<len; i++){
tip=nadji_tip(infix[i]);
if (tip==Lijeva) PUSH( infix[i], &pom );
if (tip==Desna){ while ( (TOP(pom)) != '(' ){
postfix[p++] = TOP(pom);
POP(&pom);}}
if (tip==Operand) postfix[p++] = infix[i];
if (tip==Operator) {
if (EMPTY(pom)==1) PUSH (infix[i], &pom);
while (prioritet(infix[i]) <= prioritet(TOP(pom))){
postfix[p++]=TOP(pom);
POP(&pom);}
if (prioritet(infix[i])>prioritet(TOP(pom)))
postfix[p++]=infix[i];
}}
while (EMPTY(pom)!= 1){postfix[p++]=TOP(pom);
POP(&pom);}
printf ("Izlaz: %s", postfix);
scanf("%%");
return 0;
}
p.s. nemojte se smijati mojim if-ovima =)