Selasa, 14 Maret 2017

Program C++ postfix infix

Berikut codingnya :

#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
using namespace std;
int a(char p)
{
    int temp;
    if (p=='^')
        {
            temp=1;
        }
    else if (p=='*'||p=='/')
        {
            temp=2;
        }
    else if (p=='+'||p=='/-')
        {
            temp=3;
        }
    return temp;
}
int main()
{
    string infix;
    cout<<"Input infix? = ";
    getline(cin,infix);
    stack<char>opr_stack;
    stringstream postfix;
    for (unsigned i=0;i<infix.length();i++)
        {
            if (infix[i]=='+'||infix[i]=='-'||infix[i]=='*'||infix[i]=='/'||infix[i]=='^')
                {
                    while (!opr_stack.empty()&&a(opr_stack.top())<=a(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<streamsize>::max(),'\n');
    return 0;
}

Preview :

Program C++ fungsi queue

Berikut  codingnya :

#include <iostream>
#define max_stack 15
#include <stdlib.h>
#include <conio.h>
using namespace std;
struct queue
{ int head;
  int tail;
  int data [15];
  int tampung;
};
 queue antri;
void enqueue(int d)
{
    antri.head=1;
    antri.tail++;
    antri.data[antri.tail]=d;
    cout<<"data berhasil dimasukkan";
 getch();
}
void dequeue()
{  cout<< "data "<<antri.data[antri.head]<<" terambil";
   for (int i=antri.head;i<=antri.tail;i++)
    {
        antri.data[i]=antri.data[i+1];
    }
    antri.tail--;
    getch();
}
int isEmpty()
{  if (antri.tail==0)
      {
          antri.head=0;   return 1;
      }
      else
        {
            return 0;
        }
getch();
}
int isFull()
{  if (antri.tail==max_stack)
return 1;
else
return 0;
getch();
}
void clear()
{
    antri.head=antri.tail=0;
    cout<<"semua data terhapus.";
}
void print()
{
    int j;
    for (int i=0;i<=antri.tail;i++)
        {
            antri.tampung=antri.data[i];
            j=i-1;
            while (antri.data[j]>antri.tampung&&j>=0)
                {
                    antri.data[j+1]=antri.data[j];
                    j--;
                }
                antri.data[j+1]=antri.tampung;
        }
    for (int i=1;i<=antri.tail;i++)
        {
                     cout<<antri.data[i]<<"\n";
        }
         getch();
}
int main()
{
    int a;
    int input;

    do
        {
            system("cls");
            cout<<"menu:\n1.enqueue\n2.dequeue\n3.clear\n4.print\n5.terminate\npilihan : ";
            cin>>a;
            switch (a)
            {
            case 1:if (isFull()==1)
                    {
                        cout<<"Stack Penuh"<<endl;
                        getch();
                    }
                    else
                    {
                        cout<<"Masukkan Data : ";
                        cin>>input;
                        enqueue(input);
                    }
                    break;
            case 2:if (isEmpty()==1)
                    {
                        cout<<"Kosong"<<endl;
                        getch();
                    }
                    else
                    {
                        dequeue();
                    }
                    break;
            case 3: clear();
                    cout<<"Stack Kosong";
                    getch();
                    break;
            case 4: if (isEmpty()==1)
                    {
                        cout<<"Stack Kosong";
                        getch();
                    }
                    else
                        {
                            print();
                        }
            }
        }while(a!=5);
        return 0;
}

Preview :