mardi 4 octobre 2016

Any Suggestion to make my this c++ Program more effective and Fast?

I'm noob in c++, could anyone suggest me how can i make this program for reliable is it too me so much time in making this program: Like dynamically allocation and deallocation should i use where i use and how i use.

#include<iostream>
#include<conio.h>
using namespace std;
int top = -1;
int size = 5;
int stack[5];
//UNDERFLOW
int isempty()
{
    if (top == -1) 
        return 1;
    else
        return 0;

}
//OVERFLOW
int isfull()
{
    if (top == ::size)
        return 1;
    else
        return 0;


}
//INSERTION FUNCTION
void push(int *data)
{
    if (!isfull()) {
        top = top + 1;
        stack[top] = *data;
    }
    else
        cout << endl << "OVERFLOW";
    delete data;
}
// DELETION FUNCTION
void pop()
{
    if (!isempty()) {
        //*data = stack[top];
        top = top - 1;
    //  data = 0;
    }
    else
        cout << "UNDERFLOW"<<endl;
} 
//DISPLAY FUNCTION
void out()
{
    for (int i = 0;i <= top;i++)
    {
        cout << stack[i] <<"\t";
    } cout << endl;
}
// SWITCH CASE
void sw(int ch)
{
    int *data;
    switch (ch)
    {
    case 1: 
        data = new int;
        cout << "Enter Data: ";
        cin >> *data;
        push(data);
        cout << endl;
        break;
    case 2: 
        cout << "Deleting DATA: " << endl; 
    pop(); break;
    case 3: cout << endl << "Displaying: " << endl; out(); break;
    case 4: cout << endl << "-------->EXITING"; break;
    default: cout << "Invalid Input, Try Again" << endl;
    }
}
// MAIN FUNCTION
int main()
{
    int ch, x = 1;
    do {
        cout << "1. PUSH" << endl;
        cout << "2. POP" << endl;
        cout << "3. DISPLAY" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter Your Choice: ";
        cin >> ch; cout<< endl;
        sw(ch);
    } while (ch!= 4);

    _getch();
}

Aucun commentaire:

Enregistrer un commentaire