论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

栈的操作演示程序

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:45:20

#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<math.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -1

typedef int Status;
typedef int ElemType;
typedef int bool;

/* WIN-TC BGI 图形编程模板 */

#include "Conio.h"
#include "graphics.h"
#include "dos.h"
#define closegr closegraph

void initgr(void) /* BGI初始化 */
{
  int gd = DETECT, gm = 0; /* 和gd = VGA,gm = VGAHI是同样效果 */
  registerbgidriver(EGAVGA_driver);/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */
  initgraph(&gd, &gm, "");
}
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{int i,j,k,n;
 n=(matsize-1)/8+1;
 for(j=0;j<matsize;j++)
  for(i=0;i<n;i++)
   for(k=0;k<8;k++)
    if(mat[j*n+i]&(0x80>>k))  /*测试为1的位则显示*/
     putpixel(x+i*8+k,y+j,color);
}

char zhan64H[]={
/* 以下是 '栈' 的 64点阵华文隶书 字模,512 byte */
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x30,0x00,0x1C,0x00,0x00,0x00,
  0x00,0x00,0x38,0x00,0x1C,0x01,0x80,0x00,
  0x00,0x00,0x78,0x00,0x1C,0x03,0xC0,0x00,
  0x00,0x00,0x7C,0x00,0x1C,0x03,0xC0,0x00,
  0x00,0x01,0xFF,0x80,0x3F,0xFF,0xF0,0x00,
  0x00,0x0F,0xFF,0xE3,0xFF,0xFF,0xFF,0x00,
  0x01,0xFF,0xFF,0xF7,0xFF,0xFF,0xFF,0x00,
  0x01,0xFF,0xFF,0xF7,0xFF,0xFF,0xFE,0x00,
  0x03,0xFF,0xFF,0xE7,0xFF,0xFF,0xFC,0x00,
  0x03,0xFF,0xFF,0xC7,0xFF,0x00,0x00,0x00,
  0x03,0xFF,0xFF,0x80,0x0F,0x00,0x00,0x00,
  0x03,0xFF,0xFC,0x00,0x0F,0x00,0x00,0x00,
  0x03,0xFE,0x78,0x00,0x0F,0x9F,0xF8,0x00,
  0x01,0xE0,0x70,0x00,0x3F,0xFF,0xFE,0x00,
  0x00,0x00,0x78,0x07,0xFF,0xFF,0xFE,0x00,
  0x00,0x00,0xFC,0x0F,0xFF,0xFF,0xFC,0x00,
  0x00,0x01,0xFE,0x0F,0xFF,0xFF,0xF8,0x00,
  0x00,0x03,0xFF,0x0F,0xFF,0xC0,0x00,0x00,
  0x00,0x07,0xFF,0xCF,0xE3,0xC0,0x00,0x00,
  0x00,0x0F,0xFF,0xE6,0x01,0xC0,0x00,0x00,
  0x00,0x1F,0xFF,0xE0,0x01,0xE3,0xFC,0x00,
  0x01,0x7F,0xF7,0xC0,0x01,0xFF,0xFC,0x00,
  0x03,0xFF,0xF0,0xC0,0x01,0xFF,0xFC,0x00,
  0x03,0xFE,0x70,0x00,0x0F,0xFF,0xF8,0x00,
  0x03,0xFC,0x70,0x00,0x3F,0xFF,0x00,0x00,
  0x03,0xF8,0x70,0x1B,0xFF,0xFC,0x00,0x00,
  0x03,0xF0,0x70,0x3F,0xFF,0xFC,0x00,0x00,
  0x03,0xC0,0x70,0x7F,0xFF,0xFC,0x00,0x00,
  0x03,0x00,0xF0,0x7F,0xFC,0x3E,0x00,0x00,
  0x00,0x00,0xF0,0xFF,0xF0,0x3F,0x00,0x00,
  0x00,0x00,0xF0,0xFF,0xC0,0x1F,0x80,0x00,
  0x00,0x00,0xF0,0x7F,0x00,0x1F,0x80,0x00,
  0x00,0x00,0xF0,0x7C,0x00,0x0F,0xE0,0x00,
  0x00,0x00,0xF0,0x30,0x00,0x0F,0xF0,0x00,
  0x00,0x00,0xE0,0x00,0x00,0x0F,0xFE,0x00,
  0x00,0x00,0xE0,0x00,0x00,0x07,0xFF,0xF8,
  0x00,0x00,0xE0,0x00,0x00,0x07,0xFF,0xF8,
  0x00,0x00,0xE0,0x00,0x00,0x07,0xFF,0xF8,
  0x00,0x00,0xE0,0x00,0x00,0x03,0xFF,0xE0,
  0x00,0x00,0x60,0x00,0x00,0x01,0xFF,0xC0,
  0x00,0x00,0x40,0x00,0x00,0x00,0xFF,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

char cao32H[]={
/* 以下是 '操' 的 32点阵华文琥珀 字模,128 byte */
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x07,0xC7,0xFF,0xF0,0x0F,0xC7,0xFF,0xF8,
  0x0F,0xC7,0xFF,0xF8,0x0F,0xC7,0xFF,0xF8,
  0x0F,0xC7,0xC0,0xF8,0x7F,0xF7,0xFF,0xF8,
  0x7F,0xFB,0xFF,0xF8,0x7F,0xFB,0xFF,0xF8,
  0x7F,0xE0,0x08,0x00,0x7F,0xE7,0xF7,0xFC,
  0x3F,0xCF,0xF7,0xFC,0x0F,0xCF,0xF7,0xFC,
  0x0F,0xCE,0x77,0xBC,0x0F,0xE6,0x77,0xBC,
  0x3F,0xF7,0xF7,0xFC,0x7F,0xF3,0xC1,0xFC,
  0x7F,0xF3,0xBE,0xF8,0x7F,0xF0,0x3E,0x00,
  0x7F,0xE7,0xFF,0xFC,0x3F,0xCF,0xFF,0xFE,
  0x0F,0xDF,0xFF,0xFE,0x0F,0xDF,0xFF,0xFC,
  0x0F,0xC1,0xFF,0xE0,0x1F,0x8F,0xFF,0xF8,
  0x3F,0x3F,0xFF,0xFE,0x7F,0x7F,0xBE,0xFE,
  0x7F,0x3F,0x3E,0x7E,0x7F,0xBC,0x3E,0x1C,
  0x3F,0x80,0x3E,0x00,0x00,0x00,0x00,0x00,
};
char zuo32H[]={
/* 以下是 '作' 的 32点阵华文琥珀 字模,128 byte */
  0x00,0x00,0x00,0x00,0x00,0x18,0x70,0x00,
  0x00,0x7E,0xFC,0x00,0x00,0xFC,0xFC,0x00,
  0x00,0xFD,0xFC,0x00,0x00,0xFD,0xFF,0xFE,
  0x01,0xFD,0xFF,0xFF,0x01,0xFB,0xFF,0xFF,
  0x03,0xFB,0xFF,0xFF,0x03,0xF3,0xFF,0xFF,
  0x07,0xF7,0xFF,0xFF,0x07,0xF7,0xFF,0xFE,
  0x0F,0xEF,0xEF,0x00,0x1F,0xEF,0xCF,0xFF,
  0x1F,0xEF,0xDF,0xFF,0x3F,0xEF,0xDF,0xFF,
  0x3F,0xE7,0xBF,0xFF,0x1F,0xF0,0x7F,0xFF,
  0x0F,0xF8,0x7F,0x00,0x03,0xF8,0x7F,0xFE,
  0x01,0xF8,0x7F,0xFF,0x01,0xF8,0x7F,0xFF,
  0x01,0xF8,0x7F,0xFF,0x01,0xF8,0x7F,0xFF,
  0x01,0xF8,0x7F,0xFF,0x01,0xF8,0x7F,0x00,
  0x01,0xF8,0x7F,0x00,0x01,0xF8,0x7F,0x00,
  0x01,0xF8,0x7F,0x00,0x01,0xF8,0x7F,0x00,
  0x01,0xF0,0x3E,0x00,0x00,0x00,0x00,0x00,
};
char yan32H[]={
/* 以下是 '演' 的 32点阵华文琥珀 字模,128 byte */
  0x00,0 x00,0x00,0x00,0x00,0x00,0x0C,0x00,
  0x0E,0x00,0x7F,0x00,0x1F,0x80,0x7F,0x00,
  0x3F,0x9F,0xFF,0xFC,0x3F,0xBF,0xFF,0xFE,
  0x1F,0xBF,0xFF,0xFE,0x0F,0xBF,0xFF,0xFE,
  0x03,0xBC,0x00,0x1E,0x08,0xB8,0x00,0x0E,
  0x1E,0x3B,0xFF,0xEE,0x3F,0xBF,0xFF,0xEE,
  0x3F,0xBF,0xFF,0xEE,0x3F,0xC7,0xFF,0xE0,
  0x3F,0xFF,0xFF,0xF8,0x0F,0xFF,0xFF,0xFC,
  0x07,0xFF,0xFF,0xFC,0x01,0x7F,0xFF,0xFC,
  0x00,0xBE,0x3C,0x7C,0x03,0xFF,0xFF,0xFC,
  0x03,0xFF,0xFF,0xFC,0x07,0xFF,0xFF,0xFC,
  0x07,0xFE,0x3C,0x7C,0x0F,0xFF,0xFF,0xFC,
  0x1F,0xFF,0xFF,0xFC,0x3F,0xBF,0xFF,0xFC,
  0x3F,0x7F,0xFF,0xFE,0x3F,0x7F,0xE7,0xFE,
  0x3F,0x7F,0xE3,0xFE,0x3F,0x7F,0x81,0xFE,
  0x0E,0x3E,0x00,0x3C,0x00,0x00,0x00,0x00,
};
char shi32H[]={
/* 以下是 '示' 的 32点阵华文琥珀 字模,128 byte */
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x07,0xFF,0xFF,0xE0,0x0F,0xFF,0xFF,0xF0,
  0x0F,0xFF,0xFF,0xF0,0x0F,0xFF,0xFF,0xF0,
  0x0F,0xFF,0xFF,0xF0,0x0F,0xFF,0xFF,0xF0,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x3F,0xFF,0xFF,0xFC,0x7F,0xFF,0xFF,0xFE,
  0x7F,0xFF,0xFF,0xFE,0x7F,0xFF,0xFF,0xFE,
  0x7F,0xFF,0xFF,0xFE,0x3C,0x3F,0xFC,0x3C,
  0x01,0x8F,0xFB,0x80,0x03,0xE7,0xF7,0xE0,
  0x07,0xF7,0xEF,0xE0,0x07,0xF7,0xEF,0xF0,
  0x0F,0xF7,0xEF,0xF8,0x0F,0xF7,0xF7,0xF8,
  0x1F,0xE7,0xF3,0xFC,0x3F,0xEF,0xF3,0xFC,
  0x3F,0x8F,0xF1,0xFE,0x7F,0x3F,0xF1,0xFE,
  0x7F,0x7F,0xF0,0xFE,0x3F,0x7F,0xF0,0x7C,
  0x0E,0x7F,0xF0,0x70,0x00,0x7F,0xE0,0x00,
  0x00,0x7F,0xE0,0x00,0x00,0x00,0x00,0x00,
};

typedef struct StackNode{
    ElemType data;
    struct StackNode *next;
}StackNode,*PointerNode;

typedef struct LinkStack{
    PointerNode top;
}LinkStack;

Status InitStack();
bool EmptyStack();
Status Push();
Status Pop();
Status GetTop();
int StackSize();
void Initialization();
void ReadCommand();
void ScrWin();
void Interpret();
void ReScr();
void ScrStr();


Status InitStack(LinkStack *S)
{
    S->top=NULL;

}

bool EmptyStack(LinkStack S)
{
    return S.top==NULL;
}

Status Push(LinkStack *S,ElemType e)
{
    PointerNode p;

    p=(PointerNode)malloc(sizeof(StackNode));
    if(!p) exit(OVERFLOW);

    p->data=e;
    p->next=S->top;
    S->top=p;
    return OK;
}/*Push*/

Status Pop(LinkStack *S,ElemType *e)
{
    PointerNode p;
    if(EmptyStack(*S)) return ERROR;

    p=S->top;
    *e=p->data;
    S->top=p->next;
    free(p);
    return OK;
}/*Pop*/

Status GetTop(LinkStack S,ElemType *e)
{
    if(EmptyStack(S)) return ERROR;
    *e=(S.top)->data;
    return OK;
}/*GetTop */


int StackSize(LinkStack S)
{
    PointerNode p;
    int i;
    for(p=S.top,i=0;p!=NULL;p=p->next) ++i;
    return i;
}


void Initialization()
{
    textbackground(LIGHTGRAY);
    textcolor(BLACK);
    clrscr();

    printf("\n");
    printf("\t\t      Stack Operate DEMO                   \n");
    printf("\t\t                                          \n");
    printf("\t\t                          author:zcsunt     \n");
    printf("\t\t                         version:1.0      \n");
    printf("\n");
    printf("   -----------------------------------------------------------------------");
    printf("\n");
    printf("1:Push.\n");
    printf("2:Pop.\n");
    printf("3:GetTop.\n");
    printf("4:Initialization:\n");
    printf("5:exit.\n");
    printf("Please choose a item:");


}

void ReadCommand(char *cmd)
{
    do{
        *cmd=getchar();

    }while(*cmd!='1'&&*cmd!='2'&&*cmd!='3'&&*cmd!='4'&&*cmd!='5');
}

void ScrWin(LinkStack S)
{
    PointerNode p;
    gotoxy(44,10);
    printf("S.Top%c",16);
    window(50,10,54,10+2*StackSize(S));
    textbackground(BLUE);
    textcolor(YELLOW);
    clrscr();
    if(EmptyStack(S))
        cputs("Empty");
    else
    {
        for(p=S.top;p!=NULL;p=p->next){
            cprintf("%-4d",p->data);
            cprintf(" \r\031\n\r");
            if(p->next==NULL) cprintf("NULL");
        }
    }
}

void ScrStr(char *str)
{
    window(44,8,76,11);
    printf("%s",str);
}
   
void Interpret(LinkStack *S,char cmd)
{
    ElemType a;
    long b;
    switch(cmd){
        case '1':if(StackSize(*S)>6) {ScrStr("overflow screen");break;}
                 printf("\nPlease input a element:");
                 scanf("%ld",&b);
                 if(b>32767||b<-32768){ScrStr("input overflow");break;}
                 a=(int)b;
                 Push(S,a);
                 ScrWin(*S);
                 ScrStr("pushing element is:");
                 cprintf("%d",a);
                 break;
        case '2':if(Pop(S,&a)){
                    ScrWin(*S);
                    ScrStr("poping element is:");
                    cprintf("%d",a);
                 }
                 else
                 {
                    ScrWin(*S);
                 }
                 break;
        case '3':if(GetTop(*S,&a)){
                    ScrWin(*S);
                    ScrStr("stack top element is:");
                    cprintf("%d",a);
                 }
                 else
                 {
                     ScrWin(*S);
                 }
                 break;
        case '4':if(InitStack(S)) {
                    ScrWin(*S);
                    ScrStr("Stack have been Initialed");
                }
                 break;

    }

}

void ReScr()
{
    window(1,1,80,25);
    gotoxy(22,13);
    getch();
    Initialization();

}

void Cover()
{
  int i;
  initgr();             /* BGI初始化 */

          setbkcolor(BLUE);
          cleardevice();
          setviewport(100, 100, 540, 380, 0);
          setfillstyle(1, 2);
          setcolor(YELLOW);
          rectangle(0, 0, 439, 279);
          floodfill(50, 50, 14);
          setcolor(12);

          settextstyle(4, 0, 8);
          outtextxy(50, 20, "LINK STACK OPERATER DEMO");
          setcolor(DARKGRAY);
          settextstyle(3, 0, 5);
          outtextxy(60, 220, "\3 Tel:1234567");
          setcolor(5);
          settextstyle(2, 0, 8);
          outtextxy(30, 200, "\n Author: zcsunt");

          line(60, 60, 140, 140);
          circle(100, 100, 40);
          settextstyle(3,0,8);

    drawmat(zhan64H,64,100,42,11);
    drawmat(cao32H,32,170,72,3);
    drawmat(zuo32H,32,220,72,4);
    drawmat(yan32H,32,270,72,5);
    drawmat(shi32H,32,320,72,6);

  setfillstyle(1,8);
  for(i=0;i<4;i++){

     bar3d(260,220-(24*i),350,240-(24*i),6,1);

     sleep(1);
  }
  outtextxy(120, 300, "Press any key to continues. . .");
  getch();
  closegr();
}


void main()
{
    char cmd;
    LinkStack S;
    InitStack(&S);
    Cover();
    Initialization();
    do{
        ReadCommand(&cmd);
        Interpret(&S,cmd);
        ReScr();
    }while(cmd!='5');
}

上一篇:{应用}最小耗费生成树 人气:6417
下一篇:{应用}栈的行编辑 人气:4837
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058