MENGOVERSIKAN (INFIX MENJADI PREFIK DAN POSTFIK) MENGGUNAKAN DEV C++
Selamat datang di website ini semoga berguna untuk kamu yg sedang membaca ...
Enjoy copy and paste. jangan lupa berdoa
program dev c++
#include
#include
#include
#include
#define MAX 20
#include
#include
#include
#include
#include
using namespace std;
char stack1[MAX];
int top = -1;
char pop();
void push(char item);
int prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
case '$':
return 6;
case '(':
case ')':
case '#':
return 1;
}
}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':
return 1;
default:
return 0;
}
}
void convertip(char infix[],char prefix[])
{
int i,symbol,j=0;
char test[MAX];
infix=strrev(infix);
stack1[++top]='#';
for(i=0;i
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
prefix[j]=symbol;
j++;
}
else
{
if(symbol==')')
{
push(symbol);
}
else if(symbol=='(')
{
while(stack1[top]!=')')
{
prefix[j]=pop();
j++;
}
pop();//pop out (.
}
else
{
if(prcd(symbol)>prcd(stack1[top]))
{
push(symbol);
}
else
{
while(prcd(symbol)<=prcd(stack1[top]))
{
prefix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack1[top]!='#')
{
prefix[j]=pop();
j++;
}
prefix[j]='\0';//null terminate string.
prefix=strrev(prefix);
}
int priority(char a)
{
int temp;
if (a == '^')
temp = 1;
else if (a == '*' || a == '/')
temp = 2;
else if (a == '+' || a == '-')
temp = 3;
return temp;
}
void push(char item)
{
top++;
stack1[top]=item;
}
char pop()
{
char a;
a=stack1[top];
top--;
return a;
}
int main()
{
int menu;
char infix[20],prefix[20];
stack opr_stack;
stringstream postfix;
utama :
cout<<"=================tugas gue==============="<
cout<
cout<<"Pilihan Menu : \n";
cout<<"1. INFIX => PREFIX \n";
cout<<"2. INFIX => POSTFIX \n";
cout<<"3. Keluar \n";
cout<
cout<<"Masukan Pilihan Menu : ";
cin>>menu;
switch (menu)
{
case 1 : //case ini berfungsi untuk mengulang / untuk data yg anda mau pilih
system ("cls");
printf("Masukan Bilangan Infix:\n");
cin>>infix;
convertip(infix,prefix);
printf("\nHasil dari Infix ke Prefix :\n");
puts(prefix);
cout<<"\nTekan Enter Untuk Kembali Ke Menu Utama";
getch ();
system ("cls");
goto utama ;
break ;
case 2 :
system ("cls");
string infix;
cout << "Masukan Infix : ";
cin>>infix;
for (unsigned i = 0; i < infix.length(); i++) {
if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
while (!opr_stack.empty() && priority(opr_stack.top()) <= priority(infix[i])) {
postfix << opr_stack.top();
opr_stack.pop();
}
opr_stack.push(infix[i]);
} else if (infix[i] == '(') {
opr_stack.push(infix[i]);
} else if (infix[i] == ')') {
while (opr_stack.top() != '(') {
postfix << opr_stack.top();
opr_stack.pop();
}
opr_stack.pop();
} else {
postfix << infix[i];
}
}
while (!opr_stack.empty()) {
postfix << opr_stack.top();
opr_stack.pop();
}
cout << "Postfix : " << postfix.str() << endl;
cin.ignore(numeric_limits::max(), '\n'); //fungsi agar menampilkan numeric
cout<<"\nTekan Enter Untuk Kembali Ke Menu Utama";
getch ();
system ("cls");
goto utama ;
break ;
break;
}
}
Dibawah adalah hasil ketika di run...
Ini hasil dari program mengonversikan infix menjadi prefik dan postfix
Enjoy ..di copy dan jangan lupa di coment
catatan: .klao tidak bisa di run?silahkan di coment inshallah saya akan bantu :)
sekian dan terimaksii.....semoga bermanfaat
Tidak ada komentar:
Posting Komentar