Translate

Selasa, 14 Maret 2017

soal2

#include <iostream>
#include <conio.h>
#include <stdlib.h>
#define max 10

using namespace std;

struct queue
    {
        int head;
        int tail;
        int data[max];
        int kumpul;
    };

queue antri;

    void awal()
    {
     antri.head=-1;
     antri.tail=-1;
    }

int kosong()  //variable untuk memastikan bahwa data masih kosong atau tidak
    {
        if (antri.tail==-1)
        {
        antri.head=-1;
        return 1;
        }
    else
    return 0;
    }

int penuh() //variable untuk memastikan bahwa data data sudah penuh atau tidak
    {
        if (antri.tail==max-1)
        return 1;
        else
        return 0;
    }


void input(int data)  //untuk memasukkan data
    {
        if(kosong()==1)
        {
            antri.head=0;
            antri.tail++;
            antri.data[antri.tail]=data;
            cout<<"data telah dimasukkan";
        }
        else if(penuh()==0)
        {
            antri.head=0;
            antri.tail++;
            antri.data[antri.tail]=data;
            cout<<"data telah dimasukkan";
        }

        else
            cout<<"data telah penuh";
    }

void ambil()  //untuk mengambil data yang telah diinputkan
    {
        if(kosong()==0)
        {
            cout<<"data yang terambil "<<antri.data[antri.head];
            for (int a=antri.head;a<=antri.tail;a++)
            antri.data[a]=antri.data[a+1];
            antri.tail--;
        }
   else
        cout<<"Data telah kosong";
   }

void tampil() //untuk menampilkan data yang di inputkan berupa ascending
    {
        int b;
        for (int a=0;a<=antri.tail;a++)
   {
    antri.kumpul=antri.data[a];
    b=a-1;
    while(antri.data[b]>antri.kumpul && b>=0)
{
    antri.data[b+1]=antri.data[b];
    b--;
}
    antri.data[b+1]=antri.kumpul;
   }

   for(int a=0;a<=antri.tail;a++)
   {
    cout<<antri.data[a]<<"  ";
   }

        if(kosong()==0)
        {
            cout<<endl;
            for (int a=0;a<=antri.tail;a++)
            {
                cout<<"\nIndex ke "<<a<<"="<<antri.data[a];
            }

        }
        else
            cout<<"\nData telah kosong";
    }


void bersih() //untuk membersihkan atau menghapus data yang telah di inputkan
    {
        antri.head=antri.tail=-1;

        cout<<"\nData telah kosong!";
    }

int main(){
int pilihan,data;
awal();
do
{
    cout<<"\t\tPROGRAM QUEUE\nMenu :\n1. Input (Enqueue) \n2. Ambil Data (Dequeue) \n3. Tampil\n4. Bersihkan (CLEAR)\n5. Keluar\nMasukkan pilihan :";
    cin>>pilihan;

switch(pilihan)
{
    case 1:cout<<"\nMasukkan data  = ";cin>>data;
    input(data);
        break;
    case 2:ambil();
       break;
    case 3:tampil();
       break;
    case 4:bersih();
       break;
}
getch();
system("cls");     }
while(pilihan!=5);
}



soal3

#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
#include <conio.h>


using namespace std;

int operasi(char a)
{
    int temp;
    if (a == '^')
        temp = 1;
    else if (a == '*' || a == '/')
        temp = 2;
    else if (a == '+' || a == '-')
        temp = 3;
    return temp;
}

int main()
{
    string infix;
    cout<<"\t\t\t\t\t\t\Infix Ke Postfix\n\n";
    cout<<"Masukkan nilai yang akan dikonversi\n";
    cout<<"contoh 1*2+3/4\n\n";
    cout << "Masukan Infix: ";
    getline(cin, infix);

    stack<char> opr_stack;

    stringstream postfix;

    for (unsigned a = 0; a < infix.length(); a++)
    {
        if (infix[a] == '+' || infix[a] == '-' || infix[a] == '*' || infix[a] == '/' || infix[a] == '^')
        {
            while (!opr_stack.empty() && operasi(opr_stack.top()) <= operasi(infix[a]))
            {
            postfix << opr_stack.top();
            opr_stack.pop();
            }
            opr_stack.push(infix[a]);
        }
        else if (infix[a] == '(')
        {
            opr_stack.push(infix[a]);
        }
        else if (infix[a] == ')')
        {
            while (opr_stack.top() != '(')
            {
                postfix << opr_stack.top();
                opr_stack.pop();
            }
            opr_stack.pop();
        }
        else
        {
            postfix << infix[a];
        }
    }

    while (!opr_stack.empty())
        {
            postfix << opr_stack.top();
            opr_stack.pop();
        }

    cout << "Hasil Postfix : " << postfix.str() << endl;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return 0;
    getch();
}


Tidak ada komentar:

Posting Komentar