Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2159162
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: C/C++

2011-03-29 15:58:04

栈的应用:

点击(此处)折叠或打开

  1. #include "stdafx.h"
  2. #define MAXSIZE 8
  3. typedef int datatype;
  4. typedef struct
  5. {
  6.     datatype data[MAXSIZE];
  7.  int top;
  8. }SeqStack;
  9. SeqStack *s;
  10. #include"stdio.h"
  11. #include"stdlib.h"
  12. #include"string.h"
  13. SeqStack *Init_SeqStack();
  14. int Empty_SeqStack(SeqStack *s);
  15. int Push_SeqStack(SeqStack *S,datatype x);
  16. int Pop_SeqStack(SeqStack *s,datatype *x);
  17. int _tmain(int argc, _TCHAR* argv[])
  18. {
  19.   //int k,N,M=0,kk,ch;
  20.   int m,n,sum=0,temp,k;
  21.   SeqStack *s;
  22.   datatype x;
  23.   s=Init_SeqStack();
  24.   printf("请任意输入数字:");
  25.   scanf("%d",&m);
  26.   temp=m;
  27.   while(m!=0)
  28.   {
  29.    Push_SeqStack(s,m%10);
  30.    printf("%d\n",m);
  31.    m=m/10;
  32.   }
  33.   k=1;
  34.    while(!Empty_SeqStack(s))
  35.   {
  36.       Pop_SeqStack(s,&x);

  37.     sum=sum+x*k;
  38.     k*=10;
  39.   }
  40.    printf("sum=%d\n",sum);
  41.   if(sum==temp)
  42.   printf("是回文数 \n");
  43.  else printf("不是回文数\n");
  44.   return 0;
  45. }
  46. SeqStack *Init_SeqStack()
  47. {
  48.    SeqStack *s;
  49.    s=(SeqStack*)malloc(sizeof(SeqStack));
  50.    s->top=-1;
  51.    return s;
  52. }
  53. int Empty_SeqStack(SeqStack *s)
  54. {
  55.  if(s->top==-1)return 1;
  56.  else return 0;
  57. }
阅读(1219) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~