[CODE]../* Program function: Simulate the stack using a stack limit of 10. Display
a menu for the the following.
[C] Create a stack
[i] Insert an item in the stack
[P] Pop an item from the stack
[E] Peep/view the item at the top of the stack
[V] Current value of top
[W] View all items stored in the stack
[Q] Quit
*the stack accepts lowercase & uppercase characters only from A-Z
*any other symbol (e.g. punctuation marks, numbers, etc. should not be accepted by the program
*the stack should not accept DUPLICATE ENTRIES
Note:
*[C]create a stack
-deletes all items in the stack after a confirmation if
there are existing items stored in it.
*Display appropriate messages esp when
* trying to insert an item in a stack that is already full
* trying to pop items from the stack that is empty
* duplicate items in the stack ( note: lowercase and
uppercase letters are accepted as two different entries)
*insertion of invalid values
*/
#include<stdio. h>
#include<conio. h>
#include<ctype. h>
#include<string .h>
#define stacklimit 10
int top,i;
typedef enum boolean{FALSE, TRUE} boolean;
wrong(int error)
{
printf("\n");
switch(error)
{
case 0: printf("Incorre ct input\n"); break;
case 1: printf("Please enter only Y or N\n"); break;
case 2: printf("Please enter a letter from A-Z\n"); break;
case 3: printf("Input is already in stack\n"); break;
}
}
createstack(cha r stack[])
{
for(i=0; i<stacklimit; i++)
stack[i]=NULL;
return stack[];
}
char initialize_stac k(char stack[])
{
int error=0;
char choice= 'Y';
clrscr();
if(top!=-1)
{
do{
printf("Do you want to reintialize the stack? [Y/N]");
choice=toupper( getch());
if((choice!='Y' )||(choice!='N' ))
{ error=1;
wrong(error);
}
else;
}while(error!=0 );
if(choice=='Y')
{ top=-1;
createstack(sta ck);
return stack[];
}
else;
}
else
{ top=-1;
createstack(sta ck);
return stack[];
}
}
int FULLSTACK()
{ int x;
if(top==stackli mit-1)
{ x=1;
return x;
}
else
{ x=0;
return x;
}
}
int checker(char input, char stack[])
{ int flag=0;
for(i=0;i<stack limit;i++)
{ if(stack[i]==input)
flag=1;
}
return flag;
}
push(char input, char stack[])
{
char temp_stack[stacklimit];
int x=1;
top+=1;
for(i=0;i<stack limit;i++)
temp_stack[i]=stack[i];
stack[0]=input;
for(i=0;i<stack limit;i++)
{ stack[x]=temp_stack[i];
x++;
}
return stack[];
}
input_a(char stack[])
{
int error=0, full;
char input,check;
full=FULLSTACK( );
if(full==1)
{ printf("Stack is already full");
return;
}
else
{
do{
printf("Please enter a letter from A-Z: ");
scanf("%c", &input);
check=toupper(i nput);
if(check!='A'|| check!='B'||che ck!='C'||check! ='D'||
check!='E'||che ck!='F'||check! ='G'||check!='H '||check
!='I'||check!=' J'||check!='K'| |check!='L'||ch eck!='M'
||check!='N'||c heck!='O'||chec k!='P'||check!= 'Q'||
check!='R'||che ck!='S'||check! ='T'||check!='U '||check
!='V'||check!=' W'||check!='X'| |check!='Y'||
check!='Z')
{ error=2;
wrong(error);
}
else if(checker(inpu t, stack))
{
error=3;
wrong(error);
}
else;
}while(error!=0 );
push(input, stack);
}
}
boolean EMPTYSTACK()
{
if(top==-1)
return TRUE;
else
return FALSE;
}
char pop(char stack[])
{
char x=0;
boolean empty;
empty=EMPTYSTAC K();
if(empty==TRUE)
{
printf("Stack is empty");
getch();
return x;
}
else
{
x=stack[top];
top-=1;
return x;
}
}
view(char stack[])
{
for(i=0;i<stack limit;i++)
printf("\n %d", stack[i]);
}
main()
{
int quit=0,error;
char choice , stack[stacklimit];
createstack(sta ck);
do{
clrscr();
printf("Main Menu");
printf("\n[C] Create a stack");
printf("\n[i] Insert an item in the stack");
printf("\n[P] Pop an item from the stack");
printf("\n[E] Peep/View the item at the top of the stack");
printf("\n[V] Current value of top");
printf("\n[W] View all items stored in the stack");
printf("\n[Q] Quit\n");
choice=toupper( getch());
switch(choice)
{
case 'C': initialize_stac k(stack); break;
case 'I': input_a(stack); break;
case 'P': pop(stack); break;
case 'E': printf("\n%d", stack[0]); break;
case 'V': printf("\n%i", top); break;
case 'W': view(stack); break;
case 'Q': quit=1; break;
default: error=0; wrong(error); getch(); break;
}
}while(quit==0) ;
}..[\CODE]
3 2749
That is a good question. Does your code compile? If not what errors does it give you? If it does what kind of runtime problems do you have? This is information you should supply if you expect people to actually look at your code and help you fix it.
Just glancing at your code I can say this.
Several of your functions are missing return types. If you don't include a return type in the function signature the compiler will assume "int". On a poorly implemented compiler you might even get undefined behavior. To say the least it is poor style to not include a return type. If you are returning nothing then the return type should be "void".
if you aren't including a block of code in an "else" statement then you don't need it (ie "else;" is not necessary).
You can't return arrays in the manner you are trying to. Actually, you don't need to return the array. Unless you want to start dealing with pointers. Arrays in C are past by reference. So, any changes you make in your function effect the original array you passed into the function.
Finally, why not use "isalpha" which is defined in "ctype.h" for testing for a-zA-z instead of that ugly if condition you are using?
the problem is that it wont execute due to the ff. errors:
the return array thing
and something about the function prototypes of the boolean FULLSTACK and EMPTYSTACK not being defined in other functions
as for the isalpha, our teacher didn't discuss that at all...
never mind, figured it out by myself :), thanks for the tip about isalpha though, it really helped
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from software giant such as Microsoft SQL
Server, Oracle, and Sybase?
Is PostgreSQL reliable enough to be used for high-end commercial
application? Thanks
|
by: titan0111 |
last post by:
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
using namespace std;
class snowfall
{
private:
int ft;
|
by: E. Robert Tisdale |
last post by:
What makes a good C/C++ programmer?
Would you be surprised if I told you that
it has almost nothing to do with your knowledge of C or C++?
There isn't much difference in productivity, for example,
between a C/C++ programmers with a few weeks of experience
and a C/C++ programmer with years of experience.
You don't really need to understand the subtle details
or use the obscure features of either language
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode support IDEs are DreamWeaver 8 and
Zend PHP Studio.
DreamWeaver provides full support for Unicode. However, DreamWeaver is
a web editor rather than a PHP IDE. It only supports basic IntelliSense
(or code completion) and doesn't have anything...
|
by: Madhur |
last post by:
Hello
what about this nice way to open a file in single line rather than using
if and else.
#include<stdio.h>
void main()
{
FILE *nd;
clrscr();
fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
| |
by: Cherrish Vaidiyan |
last post by:
Frinds,
Hope everyone is doing fine.i feel pointers to be the most toughest
part in C. i have just completed learning pointers & arrays related
portions. I need to attend technical interview on C. wat type of
questions should be expected? Which part of C language do the staff
give more concern?
The interviewers have just mentioned that .. i will have interview on
C.
Also can anyone can help me with sites where i can go thru sample
|
by: Keith K |
last post by:
Having developed with VB since 1992, I am now VERY
interested in C#. I've written several applications with
C# and I do enjoy the language.
What C# Needs:
There are a few things that I do believe MSFT should do to
improve C#, however.
I know that in the "Whidbey" release of VS.NET currently
|
by: Jason Huang |
last post by:
Hi,
Would someone explain the following coding more detail for me? What's the
( ) for?
CurrentText = (TextBox)e.Item.Cells.Controls;
Thanks.
Jason
|
by: Pyenos |
last post by:
import cPickle, shelve
could someone tell me what things are wrong with my code?
class progress:
PROGRESS_TABLE_ACTIONS=
DEFAULT_PROGRESS_DATA_FILE="progress_data"
PROGRESS_OUTCOMES=
|
by: Siong.Ong |
last post by:
Dear all,
my PHP aims to update a MySQL database by selecting record
one by one and modify then save.
Here are my PHP, but I found that it doesnt work as it supposed to be,
for example, when Record (i) is shown and modified, the change will
come to Record (i+1).
Can anyone provide suggestion?
thanks.
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |