Sorry to be a nit-picker but this code is tough to read.
See below.
--- sairam ram <[EMAIL PROTECTED]> wrote:
> thanks to all .please send to me more programes
>
> --- dhaval shah <[EMAIL PROTECTED]> wrote:
>
Absolutely no newlines anywhere: between includes, defines, even
function declarations or start of main(). Why??
> > #include<stdio.h>
> > #include<conio.h>
> > #define N 5
> > int top=-1,i,x;
You really don't need x and i to be globals.
> > void push(int s[N]);
> > void pop(int s[N]);
Could be useful to have pop() return the value popped.
> > void peep(int s[N]);
> > void change(int s[N]);
Quite dubious to have these two functions.
> > void dis(int s[N]);
Why not void display(int s[N])?
I have to scratch my head to find out what "dis" means.
> > void main()
> > {
> > int x=1,s[N];
Indentation: Only one space. Please use one TAB or 4/8 spaces uniformly
throughout the code. This makes it much clearer to understand when one
new level of control structure (if/else,while,...) is entered.
> > clrscr();
> > while(x!=6)
> > {
> > clrscr();
> > printf("\n1: push");
> > printf("\n2: pop");
> > printf("\n3: peep");
> > printf("\n4: change");
If you are providing the user peep() and change() you can as well
provide an array directly!
> > printf("\n5: display");
> > printf("\n6: exit");
> > printf("\n\nEnter ur option: ");
> > scanf("%d",&x);
For garbage input, this code goes into a spin. See patch at end of mail
for changes using atoi().
> > switch (x)
> > {
> > case 1:
> > push(s);
> > break;
> > case 2: pop(s);
This almost got me confused that you called nothing for pop. Please put
actions for each case on a newline as you did for case 1 (push).
> > break;
> > case 3: peep(s);
> > break;
> > case 4: change(s);
> > break;
> > case 5: dis(s);
> > break;
> > case 6: break;
> > default:
> > printf("\nenter the right option");
> > getch();
I think this getch() will confuse the user a bit. Need to press a key
to get back to the main menu again: Why? The message "enter the right
option" remains right there.
> > }
> > }
> > }
> > void push(int s[])
> > {
> > clrscr();
Why clear screen for every operation?
> > if(top>=N-1)
> > {
> > printf("overflow");
> > getch();
> > }
Indentation? :((
> > else
> > {
> > top++;
> > printf("enter the no: ");
> > scanf("%d",&s[top]);
> > dis(s);
> > }
> > }
> > void pop(int s[])
> > {
> > clrscr();
> > if(top<0)
> > {
> > printf("underflow on pop");
> > }
> > else
> > {
> > x=s[top];
> > printf("the number poped is %d :",x);
> > top--;
> > dis(s);
> > }
> > getch();
> > }
> > void peep(int s[])
> > {
> > printf("enter the index u want :");
> > scanf("%d",&i);
> > if(top-i+1<0)
> > {
> > printf("stack underflow on peep");
> > }
> > else
> > {
> > printf("the peeped no is %d\n",s[top-i+1]);
> > }
> > dis(s);
> > getch();
> > }
> > void change(int s[])
> > {
> > printf("enter the index which u want to b
> > changed");
> > scanf("%d",&i);
> > if(top-i+1<0)
> > printf("stack underflow on change");
Please use braces for single statements too.
> > else
> > {
> > printf("\nenter the no:");
> > scanf("%d",&x);
> > s[top-i+1]=x;
> > printf("the changed no is %d",s[top-i+1]);
> > }
> > dis(s);
> > }
> > void dis(int s[])
> > {
> > for(i=top;i>=0;i--)
> > printf("\n%d",s[i]);
> > getch();
Why getch() everywhere?
> > }
See this diff below.
Most of the changes are for indentation. Done automatically by:
indent -kr -i4 -ts4
Removed use of clrscr() and getch().
[EMAIL PROTECTED]:~/src/c/trivia/mg/stack> diff -upb stack-orig.c stack.c
--- stack-orig.c 2007-09-09 20:52:09.000000000 +0530
+++ stack.c 2007-09-09 21:41:34.000000000 +0530
@@ -1,19 +1,24 @@
-#include<stdio.h>
-#include<conio.h>
+#include <stdio.h>
+#include <stdlib.h>
+
#define N 5
-int top=-1,i,x;
+
+int top = -1;
+
void push(int s[N]);
void pop(int s[N]);
void peep(int s[N]);
void change(int s[N]);
void dis(int s[N]);
-void main()
+
+int main(void)
{
- int x=1,s[N];
- clrscr();
- while(x!=6)
- {
- clrscr();
+ long int x;/* atoi() needs it */
+ int s[N];
+ char input[10]="";
+ // clrscr();
+ while (x != 6) {
+ // clrscr();
printf("\n1: push");
printf("\n2: pop");
printf("\n3: peep");
@@ -21,92 +26,85 @@ void main()
printf("\n5: display");
printf("\n6: exit");
printf("\n\nEnter ur option: ");
- scanf("%d",&x);
- switch (x)
- {
+ scanf("%2s", input); /* Take only two characters input
*/
+ x=atoi(input); /* Convert string to long */
+
+ switch (x) {
case 1:
push(s);
break;
- case 2: pop(s);
+ case 2:
+ pop(s);
break;
- case 3: peep(s);
+ case 3:
+ peep(s);
break;
- case 4: change(s);
+ case 4:
+ change(s);
break;
- case 5: dis(s);
+ case 5:
+ dis(s);
+ break;
+ case 6:
break;
- case 6: break;
default:
printf("\nenter the right option");
- getch();
}
}
+ return 0;
}
void push(int s[])
{
- clrscr();
- if(top>=N-1)
- {
+ if (top >= N - 1) {
printf("overflow");
- getch();
- }
- else
- {
+ } else {
top++;
printf("enter the no: ");
- scanf("%d",&s[top]);
+ scanf("%d", &s[top]);
dis(s);
}
}
void pop(int s[])
{
- clrscr();
- if(top<0)
- {
+ if (top < 0) {
printf("underflow on pop");
- }
- else
- {
- x=s[top];
- printf("the number poped is %d :",x);
+ } else {
+ x = s[top];
+ printf("the number poped is %d :", x);
top--;
dis(s);
}
- getch();
}
+
void peep(int s[])
{
printf("enter the index u want :");
- scanf("%d",&i);
- if(top-i+1<0)
- {
+ scanf("%d", &i);
+ if (top - i + 1 < 0) {
printf("stack underflow on peep");
- }
- else
- {
- printf("the peeped no is %d\n",s[top-i+1]);
+ } else {
+ printf("the peeped no is %d\n", s[top - i + 1]);
}
dis(s);
- getch();
}
+
void change(int s[])
{
printf("enter the index which u want to b changed");
- scanf("%d",&i);
- if(top-i+1<0)
+ scanf("%d", &i);
+ if (top - i + 1 < 0){
printf("stack underflow on change");
- else
- {
+ }else {
printf("\nenter the no:");
- scanf("%d",&x);
- s[top-i+1]=x;
- printf("the changed no is %d",s[top-i+1]);
+ scanf("%d", &x);
+ s[top - i + 1] = x;
+ printf("the changed no is %d", s[top - i + 1]);
}
dis(s);
}
+
void dis(int s[])
{
- for(i=top;i>=0;i--)
- printf("\n%d",s[i]);
- getch();
+ for (i = top; i >= 0; i--)
+ printf("\n%d", s[i]);
}
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play
Sims Stories at Yahoo! Games.
http://sims.yahoo.com/