栈练习 杭电1002 栈实现大数运算

本文介绍了一个使用栈数据结构实现字符串表示的两个整数相加的程序。通过定义栈结构体并实现初始化、压栈、输出及两数相加等功能,文章详细展示了如何用C/C++语言进行栈的操作,并提供了完整的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>

#define STACK_INT_SIZE 5
#define  STACKINCREMENT   10

typedef int Status ;
typedef  int  Elemtype;

using namespace std;

typedef struct stack
{
    Elemtype *top;
    Elemtype *base;
    int stacksize;
} Sqstack;
void initStack(Sqstack &S)
{
    S.base=(Elemtype *)malloc(STACK_INT_SIZE*sizeof(Elemtype));
    S.top=S.base;
    S.stacksize=STACK_INT_SIZE;

}

Status pushstack(Sqstack &S,char e)
{
    //int l=a.length();
    //for(int i=0;i<l;i++)
    // { //char  e=a[i];

    // int q=STACK_INT_SIZE;
    //int p=STACKINCREMENT;

    if(S.top-S.base>=S.stacksize)
    {
        S.base=(Elemtype *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(Elemtype));
        S.top=S.base+S.stacksize;
        S.stacksize=S.stacksize+STACKINCREMENT ;



    }
    S.top++;
    *(S.top-1)=e;

    //}

}
Status output(Sqstack S)
{
    while(S.base!=S.top)
    {
        S.top--;
        int a=*S.top;
        cout<<a;
        //S.base++;
    }
    /*Elemtype *p;
    p=S.top;

    while(p>S.base)
    {
        p--;
        cout<<*p;
    }*/
}
void sumStack(Sqstack A,Sqstack B,Sqstack &C)
{   int he=0;
    int jw=0;
    int sum=0;

    int i=0;
    while((A.top>A.base)&&(B.top>B.base))
    {
        A.top--;
        B.top--;
        sum=(*A.top-96+*B.top+jw);
        he=sum%10;
        jw=sum/10;
        pushstack(C,he);


    }
    if(B.top!=B.base)
    {
        while(B.top!=B.base)
        {
            B.top--;
          sum=(*B.top-48+jw);
            he=sum%10;
            jw=sum/10;
            pushstack(C,he);


        }

    }
    else if(A.top!=A.base)
    {
        while(A.top!=A.base)
        {
            A.top--;
            sum=(*A.top-48+jw);
            he=sum%10;
            jw=sum/10;
            pushstack(C,he);



        }
    }
    if(jw!=0)
    {
        pushstack(C,jw);
    }

}
/*
Status  sortStack(Sqstack &A,Sqstack &B,Sqstack &C)
{
    while(A.base!=A.top)
    {
        A.top--;
        if(*A.top>=0)
        {
            pushstack(B,*A.top);
        }else pushstack(C,*A.top);
    }
}*/
int main()
{
    Sqstack A,B,C;

    int n;
    cin>>n;
    for(int j=1; j<=n; j++)
    {
        string a,b;
        cin>>a>>b;
        initStack(A);
        initStack(B);
        initStack(C);
        char e;
        for(int i=0; i<a.length(); i++)
        {
            e=a[i];

            pushstack(A,e);
        }
        for(int i=0; i<b.length(); i++)
        {
            e=b[i];
            pushstack(B,e);
        }
        //
        //output(A);cout<<endl;
        //output(B);cout<<endl;

        sumStack(A,B,C);
        cout<<"Case "<<j<<":"<<endl;
        cout<<a<<" + "<<b<<" = ";
        output(C);
        cout<<endl;
       if(j<n)
         cout<<endl;

        //output(B);
    }
    /*while(cin>>x)
    {


    pushstack(A,x);

    }
    */
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值