#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);
}
*/
}
栈练习 杭电1002 栈实现大数运算
最新推荐文章于 2022-10-14 16:13:20 发布