Mystery Algorithm
Posted by JanWan
Last Updated: March 05, 2012
/*Program designes from Mystery Algorithm
headerfiles*/
#include <stdio.h>
#include <ctype.h>
#include <conio.h> //for BORLAND remove slashes at the beginning of this line
//constants defined
#define MAXSIZE 100
#define MINSIZE 0

//variables for main program
char stack [MAXSIZE];
int length;
int checkcnt=1;
char check [15];
int will=1;
int i;
char ch;

//variables for stack
int top;

//functions
void push(char);
char pop();

//beginning of main
main()
{
    char data;
    //beginning of while loop
    while(will==1)
    {
    printf("\nPlease enter character:");
    scanf("%s", &check[checkcnt]);
    checkcnt++;

    //if loop to check if checkcnt is full
    if(checkcnt<=11)
        {
            //checks if the user has more characters to enter
            printf("\nPress 1 to enter another character...Press 2 if finished: ");
            scanf("%d", &will);
        }
    else
        {
            will=2;
        }
    }//end of while loop
    length = checkcnt;//length is determined by the amount of characters entered
    length--;/*length is counted down by 1 since checkcnt does
    not use the value 0*/
    printf("\nThe amount of characters entered was: %d\n\n\n\n", length);

    //beginning of for loop
    for(i=1; i<=length; i++)
    {
        ch = check[i];
        //if statement nested in for loop
        if(ch=='a')
        {
        push(ch);
        }
        else//nested if statement
        {
            if(top==MINSIZE)
            {
                puts("Failure 1");
                return 0;
            }
            else
            {
                data=pop();
            }
        }
    }//end of for loop

    //last if statement
    if(top!=MINSIZE)
    {
        puts("Failure 2");
    }
    else
    {
        puts("Success");
    }

    printf("\n\n");
    return 0;
}//end of main

//push function
void push(char pushed)
{
    if(top>MAXSIZE)
    {
        puts("STACK IS FULL");
        return;
    }
    else
    {
        top++;
        stack[top]=pushed;
        return;
    }
}

//pop function
char pop()
{
    char popped;
    if(top<=MINSIZE)
    {
        puts("STACK IS EMPTY");
        return 0;
    }
    else
    {
        popped = stack[top];
        top--;
        return (popped);
    }
}