任意长度整数加法

使用动态栈结构
   1: #include <stdio.h>
   2: #include <math.h>
   3: #include <stdlib.h>
   4:  
   5: #define STACK_INIT_SIZE 20
   6: #define STACKINCREMENT 10
   7: /*定义堆栈*/
   8: typedef  char ElemType;
   9: typedef struct{
  10:     ElemType *base;
  11:     ElemType *top;
  12:     int stacksize;
  13: }sqStack;
  14: /*初始化栈*/
  15: void initStack(sqStack *s)
  16: {
  17:     /*内存中开辟一段连续空间作为栈空间,首地址赋值给s->base*/
  18:     s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  19:     if(!s->base) exit(0);                    /*分配空间失败*/
  20:     s->top = s->base;                    /*最开始,栈顶就是栈底*/
  21:     s->stacksize = STACK_INIT_SIZE;        /*最大容量为STACK_INIT_SIZE */
  22: }
  23: /*入栈操作,将e压入栈中*/
  24: void Push(sqStack *s, ElemType e){
  25:     if(s->top - s->base >= s->stacksize){
  26:     /*栈满,追加空间*/
  27:     s->base = (ElemType *)realloc(s->base, (s->stacksize +
  28:     STACKINCREMENT)*sizeof(ElemType));
  29:     if(!s->base) exit(0);                                /*存储分配失败*/
  30:     s->top = s->base + s->stacksize;
  31:     s->stacksize = s->stacksize + STACKINCREMENT;        /*设置栈的最大容量*/
  32:     }
  33:     *(s->top) = e;                                    /*放入数据*/
  34:         s->top++;
  35: }
  36: /*出栈操作,用e将栈顶元素返回*/
  37: void Pop(sqStack *s , ElemType *e){
  38:     if(s->top == s->base) return;
  39:     *e = *--(s->top);
  40: }
  41:  
  42: /*计算堆栈s当前的长度*/
  43: int StackLen(sqStack s){
  44:     return (s.top - s.base) ;
  45: }
  46:  
  47: void ADD(sqStack *s1,sqStack *s2,sqStack *s3)
  48: {
  49: char a1,a2,a3,c=0;                 /*a1,a2分别存放从堆栈s1,s2中取出的(数据)元素,
  50: a3=a1+a2,c中存放进位*/
  51:     while(StackLen(*s1)!=0 && StackLen(*s2)!=0)
  52:     {
  53:          Pop(s1,&a1);                    /*取出s1栈的栈顶元素给a1*/
  54:          Pop(s2,&a2);                    /*取出s2栈的栈顶元素给a2*/
  55:          a3 = (a1-48) + (a2-48) + c + 48;        /*相加*/
  56:          if(a3>'9')
  57:          {
  58:                a3 = a3 - '9' + 47;            /*产生进位的情况*/
  59:                c = 1;
  60:           }
  61:           else
  62:               c = 0;                    /*不产生进位*/
  63:          Push(s3,a3);                    /*将结果入栈s3*/
  64:      }
  65:      if(StackLen(*s1)!=0)                    /*栈s1不为空的情况*/
  66:      {
  67:         while(StackLen(*s1)!=0)
  68:         {
  69:            Pop(s1,&a1);                    /*取出s1栈的栈顶元素给a1*/
  70:            a3 = a1 + c ;                    /*与进位标志c相加*/
  71:            if(a3>'9')
  72:            {
  73:                a3 = a3 - '9' + 47;            /*产生进位的情况*/
  74:                c = 1;
  75:            }
  76:           else
  77:               c = 0;                    /*不产生进位*/
  78:            Push(s3,a3);                    /*将结果入栈s3*/
  79:         }
  80:      }
  81:      else if(StackLen(*s2)!=0)                /*栈s1不为空的情况*/
  82:      {
  83:         while(StackLen(*s2)!=0)
  84:         {
  85:            Pop(s2,&a2);                    /*取出s1栈的栈顶元素给a1*/
  86:            a3 = a2 + c;                    /*与进位标志c相加*/
  87:            if(a3>'9')
  88:            {
  89:                a3 = a3 - '9' + 47;            /*产生进位的情况*/
  90:                c = 1;
  91:            }
  92:           else
  93:               c = 0;                    /*不产生进位*/
  94:            Push(s3,a3);                    /*栈s1不为空的情况*/
  95:         }
  96:      }
  97:      if(c==1)
  98:         Push(s3,'1');                    /*如果最后有进位,将字符’1’入栈s3*/
  99: }
 100:  
 101: int main()
 102: {
 103:     char e;
 104:     sqStack s1,s2,s3;
 105:     initStack(&s1);                            /*初始化堆栈s1,存放加数*/
 106:     initStack(&s2);                            /*初始化堆栈s2,存放加数*/
 107:     initStack(&s3);                            /*初始化堆栈s3,存放结果*/
 108:     printf("Please input the first integer\n");        /*输入第一个任意长整数,按”#”结尾*/
 109:     scanf("%c",&e);
 110:     while(e!='#')
 111:     {
 112:        Push(&s1,e);                            /*将加数(字符串)入栈s1*/
 113:        scanf("%c",&e);
 114:     }
 115:     getchar();                                /*接收回车符*/
 116:     printf("Please input the second integer\n");        /*输入第二个任意长整数,按”#”结尾*/
 117:     scanf("%c",&e);
 118:     while(e!='#')
 119:     {
 120:        Push(&s2,e);                            /*将加数(字符串)入栈s2*/
 121:        scanf("%c",&e);
 122:     }
 123:     ADD(&s1,&s2,&s3);                        /*加法运算,将结果存放在s3中*/
 124:     printf("The result is\n");
 125:     while(StackLen(s3)!=0)                    /*输出结果,打印在屏幕上*/
 126:     {
 127:         Pop(&s3,&e);
 128:         printf("%c",e);
 129:     }
 130:     return 0;
 131: }
posted @ 2010-05-29 12:33  红脸书生  阅读(1813)  评论(0编辑  收藏  举报