shiyan2.1.cpp

来自「本程序利用静态存储方式实现栈的共享」· C++ 代码 · 共 376 行

CPP
376
字号
#include <stdio.h>
#include <stdlib.h>
#define ST stacker
typedef struct
{long *base1,*base2,*top1,*top2;
 long length1,length2;
}stacker;
/*/////////////////////////////////////////////////////////////////////////*/
void initstack(ST &,int &);  /*initialize the stack*/
void emp(ST );             /*return whether the stack is empty*/
void full(ST );            /*return whether the stack is full*/
void pushst(ST & ,int &);        /*push an integer to the stack*/
void popst(ST & );         /*pop an integer from the stack*/
void clearst(ST & );       /*clear the stack*/
void gettop(ST );          /*get the top element of the stack*/
void comp(ST );            /*compare the 2 stacks' length*/
void spacelf(ST ,int );    /*return the space left*/
void stlen(ST );           /*return the length of each stack*/
void quit();               /*quit the program*/
void infor();              /*show user the information*/
void goodbye();            /*print message to user*/
void print(ST ,int);           /*print the stack*/
int getchoice();           /*return the choice user choose*/
int getlength();           /*get the length of the shared stack*/
/*////////////////////////////////////////////////////////////////////////*/

void main()
{ST stack1;
int choice,length;
char ok;
do
{infor();                  /*show information*/
choice=getchoice();        /*get operator*/
switch(choice)
{case 1:initstack(stack1,length);break;
case 2: emp(stack1);break;
case 3: full(stack1);break;
case 4: pushst(stack1,length);break;
case 5: popst(stack1);break;
case 6: clearst(stack1);break;
case 7: gettop(stack1);break;
case 8: comp(stack1);break;
case 9: spacelf(stack1,length);break;
case 10:stlen(stack1);break;
case 11: print(stack1,length);break;
case 12: quit();break;
}
printf("\n\n>>>>  Do any choice again?(y or n)");
getchar();
scanf("%c",&ok);            /*get char for redo or not*/
}while(ok=='y'||ok=='Y');
goodbye();                  /*end of the program*/
} 

/*//////////////////////////////////////////////////////////////////////////////////////*/

void initstack(ST &stack1,int &length)
{
	if(stack1.base1!=stack1.base2) free(stack1.base1);
length=getlength();        /*get the shared length of the stack*/
stack1.base1=(long *)malloc(length*sizeof(long));           /*apply for some space*/
if(!stack1.base1) {puts("\nSorry,initialize stack failed!\n\n");exit(1);}
stack1.top1=stack1.base1;                                 /*initialize stack1*/
stack1.length1=stack1.length2=0;                          /*initialize the stacks' length*/
stack1.base2=stack1.base1+(length-1);         /*initialize stack2*/
stack1.top2=stack1.base2;
printf("\n>>>>  Congratulations! Suceed in initialize a stack with %d element space! <<<<\n\n",length-1);
}

/*//////////////////////////////////////////////////////////////////////////////////////*/

void emp(ST stack1)
{
if(stack1.top1==stack1.base1&&stack1.top2==stack1.base2)
puts("\n\n                      >>>>  STACK EMPTY!  <<<<");       /*stack empty*/
else
puts("\n\n                      >>>>  STACK IS NOT EMPTY!  <<<<");/*stack not empty*/
}
/*//////////////////////////////////////////////////////////////////////////////////////*/

void full(ST stack1)
{
if(stack1.top1==stack1.top2)
puts("\n\n                      >>>>  STACK FULL!  <<<<");         /*stack full*/
else
puts("\n\n                      >>>>  STACK IS NOT FULL!  <<<<");  /*stack not full*/
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void pushst(ST &stack1,int &length)
{
int k,i,j,m;
long *p1,*p2;
char yorn;
if(stack1.top1==stack1.top2)                                       /*the stack is full*/
{
   puts("\n\n                      >>>>  STACK FULL!  <<<<");
   puts("\n\n>>>>RESIZE THE STACK?(Y/N)");
   getchar();
   scanf("%c",&yorn);
   if(yorn=='y'||yorn=='Y')
   {printf("\n\n>>>>>Intput incremetsize! <<<<<<\n       >>>>>>>>>>  INCRESEMENT LENGTH=");
   scanf("%d",&m);                                                  /*get extra size*/
   length=length+m;
   stack1.base1=(long *)realloc(stack1.base1,length*sizeof(long));   /*reapply for space*/
   p1=stack1.base2;p2=stack1.top2;
   stack1.top2=stack1.base2=stack1.base1+(length-1);               /*move base2*/
   while(p1!=p2)
   {*stack1.top2--=*p1;p1--;} /*move elements*/
   }
   else quit();
}

printf("\n\n>>> Enter the stack you want to operate:");
do
{scanf("%d",&i);                                                   /*get operator object*/
if(i!=1&&i!=2)
{puts("\n>>> Input error! Input again!(1,2)");k=1;}
else
k=0;
}while(k==1);                                                      /*cycle of input error*/
printf("\n\n>>>> input an integer ! <<<<\n\n==>");
scanf("%d",&j);                                                    /*get the element to push*/
switch(i)
{                                                                  /*operate on what user choosed*/
case 1:*stack1.top1++=j;stack1.length1++;break;
case 2:*stack1.top2--=j;stack1.length2++;break;
}
printf("\n\n>>>You choose stack%d,and the element >>>>  %d  <<<< has been pushed!  <<<\n\n",i,j);

}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void popst(ST &stack1)
{
int i,j,k;
printf("\n\n>>> Enter the stack you want to operate:");
do
{scanf("%d",&i);                                                   /*get operator object*/
if(i!=1&&i!=2)
{puts("\n>>> Input error! Input again!(1,2)");k=1;}
else
k=0;
}while(k==1);   
switch(i)                                                          /*pop element from stack*/
{     
case 1:if(stack1.top1==stack1.base1) {printf("\n\n>>> Sorry,stack1 is null!   <<<");goto ending;}
       else {j=*--stack1.top1;stack1.length1--;break;}
case 2:if(stack1.top2==stack1.base2) {printf("\n\n>>> Sorry,stack2 is null!   <<<");goto ending;}
       else {j=*++stack1.top2;stack1.length2--;break;}
}
printf("\n\n>>>You choose stack%d  <<<<<<\n\n and the top element >>>>   %d   <<<<<has been poped!  \n\n",i,j);
ending: ;
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void gettop(ST stack1)
{
int i,j,k;
printf("\n\n>>> Enter the stack you want to operate:");
do
{scanf("%d",&i);                                                   /*get operator object*/
if(i!=1&&i!=2)
{puts("\n>>> Input error! Input again!(1,2)");k=1;}
else
k=0;
}while(k==1);
switch(i)                                                          /*get element from the stack*/
{
case 1:if(stack1.top1==stack1.base1) {printf("\n\n>>> Sorry,stack1 is null!   <<<");goto ending;}
	else {j=*--stack1.top1;break;}
case 2:if(stack1.top2==stack1.base2) {printf("\n\n>>> Sorry,stack2 is null!   <<<");goto ending;}
	else {j=*++stack1.top2;break;}
}
printf("\n\nYou choose stack%d ,and the top element is:  >>>>>   %d   <<<<\n\n",i,j);
ending:;
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void comp(ST stack1)
{
printf("\n\n %d  elements in stack1,%d elements in stack2.\n",stack1.length1,stack1.length2);
                                                                    /*print length of each stack*/
if(stack1.length1>stack1.length2) puts("\n>>> Stack1 is longer than stack2. <<<");
else                                                                /*Stack1 is longer*/
{
if(stack1.length1<stack1.length2) puts("\n>>> Stack2 is longer than stack1. <<<");
                                                                    /*Stack2 is longer*/
else  puts("\n>>> Stack1 and stack2 have the same length. <<<");    /*two stack have the same length*/
}
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void spacelf(ST stack1,int length)
{int k;
if(stack1.top1==stack1.top2)                                        /*stack full*/
{
	if(stack1.top1==stack1.base2)
		printf("\n\n         >>>>>  STACK EMPTY!   <<<<<");
	else
	printf("\n\n            >>>>  STACK FULL!  <<<<");}
else
{k=length-stack1.length1-stack1.length2-1;                           /*get left space*/
printf("\n\n>>>> There are %d space left!   <<<<",k);               /*print left space*/
}
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void stlen(ST stack1)
{
printf("\n\n>>>>  %d elements in stack1. <<<<\n",stack1.length1);   /*print stack length*/
printf("\n\n>>>>  %d elements in stack2. <<<<\n",stack1.length2);
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void quit()
{
goodbye();                                                          /*print message to user*/
exit(1);
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
void infor()
{
system("cls");
printf("\n\nWelcom use!Please choose:\n\n\n");
printf("             ==========================================================\n");
printf("             || >>>    1.  To initialize a stack.                <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    2.  To show if the stack is empty.        <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    3.  To show if the stack is full.         <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    4.  To push an element to the stack.      <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    5.  To pop an element from the stack.     <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    6.  To clear the stack.                   <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    7.  To get the top element .              <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    8.  To compare the two stacks'length.     <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    9.  To to get the left sapce of the stack.<<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    10. To get the stack length.              <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    11. To print the elements in stack.       <<< ||\n");
  puts("             ----------------------------------------------------------");
printf("             || >>>    12. To quit the program.                  <<< ||\n");
printf("             ==========================================================\n\n\n");
printf("\n\n\n >>>>  And your choice is:");
}

/*//////////////////////////////////////////////////////////////////////////////////////*/
int getchoice()
{
int i,k;
do
{
scanf("%d",&i);
if(i<1||i>12)
{printf("\n\n>>> Input error! Input again!(1~12)");k=1;}
else
k=0;
}while(k==1);                                                 /*cycle of input error*/

return i;                                                     /*return the choice of user*/
}  
/*//////////////////////////////////////////////////////////////////////////////////////*/
void goodbye()
{
puts("\n\n                      >>>>    Thank you for using!     <<<<\n\n");
}
/*//////////////////////////////////////////////////////////////////////////////////////*/
int getlength()
{
int i,k=0;
printf("\n\n>>> Enter the length of stack to be shared by 2 stacks!  <<<\n\n\n>>>>>>>  SHARE STACK LENGTH=");
do                                                             /*print message to user*/
{
scanf("%d",&i);
if(i<2||i>100)                                                 /*check input*/
{puts("\n\n>>> Invaild input! Input again(3 ~ 100) <<<");k=1;}
else
k=0;
}while(k==1);
return i+1;                                                    /*return input*/
} 
/*//////////////////////////////////////////////////////////////////////////////////////*/
void clearst(ST &stack1)
{stack1.top2=stack1.top1=stack1.base2=stack1.base1;            /*handle of 4 pointers*/
free(stack1.base1);                                            /*free space*/
puts("\n                    >>>>> Suceed in clear the stack! <<<<<");
}

/*//////////////////////////////////////////////////////////////////////////////////////*/

void print(ST stack1,int length)
{
long *p;
int i=length-1;
if(stack1.length1==0&&stack1.length2==0)
{  puts("\n>>>  STACK EMPTY! <<<");
	while(i>=1)
{puts("                  __________                  ");
 puts("                 |          |                  ");i--;
}
printf("                  __________                  \n");
}

if(stack1.length1!=0&&stack1.length2!=0)
{p=stack1.base2;
	while(i>=1)
{
	if(i>length-1-stack1.length2)
	{   printf("                  __________                  \n");
	if(i==length-1)
	{printf("    stack2.base  |%10d|                 \n",*p);p--;i--;}
	else  {printf("    stack2==>    |%10d|                 \n",*p);p--;i--;}
	}
	else
	{if(i>stack1.length1&&i<=length-1-stack1.length2)
	{   printf("                  __________                  \n");
        printf("                 |          |                  \n");p--;i--;	
	}
    if(i<=stack1.length1)
	{  if(i==stack1.length1) --p;
		 printf("                  __________                 \n ");
		if(i==1)
		{printf("   stack1.base  |%10d|                 \n",*p);p--;i--;}
		else {printf("   stack1==>    |%10d|                 \n",*p);p--;i--;}
	}
	}
}
        printf("                  __________                  \n");
}

if(stack1.length1==0&&stack1.length2!=0)
{p=stack1.base2;
while(i>=1)
{if(i>length-1-stack1.length2)
	{   printf("                  __________                  \n");
if(i==length-1)
{	    printf("    stack2.base  |%10d|                 \n",*p);p--;i--;}
else   {printf("    stack2==>    |%10d|                 \n",*p);p--;i--;}
	}
else
{ printf("                  __________                  \n");
 printf("                 |          |                  \n");i--;
}
}
printf("                  __________                 \n ");
}


if(stack1.length1!=0&&stack1.length2==0)
{
	p=--stack1.top1;
	while(i>=1)
{if(i>stack1.length1)
{printf("                  __________                  \n");
 printf("                 |          |                  \n");i--;
}
else
{  printf("                  __________                  \n");
if(i==1)
{	   printf("    stack1.base  |%10d|                 \n",*p);p--;i--;}
else  {printf("    stack1==>    |%10d|                 \n",*p);p--;i--;}
}
}
printf("                  __________                 \n ");
}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?